Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.function.Function;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

Expand Down Expand Up @@ -56,6 +57,17 @@ public abstract class KarafMinimalMonitoredTestSupport {

public static Logger LOG = LoggerFactory.getLogger(KarafMinimalMonitoredTestSupport.class);

protected static final String MAVEN_RESOLVER = "org.ops4j.pax.url.mvn.MavenResolver";

/**
* Service property set by pax-url-aether on each MavenResolver it registers: {@code bundlecontext}
* when built without ConfigAdmin properties (at bundle start, or for a {@code updated(null)} from
* ConfigAdmin), {@code configadmin} when built from the {@code org.ops4j.pax.url.mvn} configuration.
*/
protected static final String CONFIGURATION = "configuration";

private static final long SERVICE_TIMEOUT = 30000;

@Inject
protected ServiceMonitor serviceMonitor;

Expand Down Expand Up @@ -132,9 +144,53 @@ private InputStream createMonitorBundle() {
.build(TinyBundles.bndBuilder());
}

protected long numberOfServiceEventsFor(String serviceName) {
Function<ServiceEvent, String> getObjectClass = event -> ((String[])event.getServiceReference().getProperty(OBJECTCLASS))[0];
return serviceMonitor.getEvents().stream().map(getObjectClass).filter(v -> v.equals(serviceName)).count();
protected List<ServiceEvent> serviceEventsFor(String serviceName) {
return serviceMonitor.getEvents().stream()
.filter(event -> serviceName.equals(((String[]) event.getServiceReference().getProperty(OBJECTCLASS))[0]))
.collect(Collectors.toList());
}

/**
* Waits until a single service of the given class is left registered, and that service has
* {@code property=value}. The registrations happen asynchronously (ConfigAdmin calls
* ManagedService.updated() on its own thread), so the probe can run before the last one.
*
* @return the {@link ServiceEvent#REGISTERED} events of that service class, in order
*/
protected List<ServiceEvent> awaitFinalRegistration(String serviceName, String property, Object value) throws InterruptedException {
long deadline = System.currentTimeMillis() + SERVICE_TIMEOUT;
while (true) {
List<ServiceEvent> events = serviceEventsFor(serviceName);
List<ServiceEvent> registered = events.stream()
.filter(event -> event.getType() == ServiceEvent.REGISTERED)
.collect(Collectors.toList());
long unregistering = events.stream().filter(event -> event.getType() == ServiceEvent.UNREGISTERING).count();
if (registered.size() - unregistering == 1
&& value.equals(registered.get(registered.size() - 1).getServiceReference().getProperty(property))) {
return registered;
}
if (System.currentTimeMillis() > deadline) {
throw new AssertionError("No single " + serviceName + " left registered with " + property + "=" + value
+ " after " + SERVICE_TIMEOUT + " ms, events: " + describe(events, property));
}
Thread.sleep(100);
}
}

/**
* @return the type and the given property of each event, e.g. {@code [REGISTERED(bundlecontext), ...]}
*/
protected String describe(List<ServiceEvent> events, String property) {
return events.stream().map(event -> {
String type;
switch (event.getType()) {
case ServiceEvent.REGISTERED: type = "REGISTERED"; break;
case ServiceEvent.UNREGISTERING: type = "UNREGISTERING"; break;
case ServiceEvent.MODIFIED: type = "MODIFIED"; break;
default: type = Integer.toString(event.getType());
}
return type + "(" + event.getServiceReference().getProperty(property) + ")";
}).collect(Collectors.joining(", ", "[", "]"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;

import java.io.File;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -30,6 +31,7 @@
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.osgi.framework.ServiceEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -59,9 +61,13 @@ public Option[] config() throws Exception {

@Test
public void mavenResolverAvailable() throws Exception {
long count = numberOfServiceEventsFor("org.ops4j.pax.url.mvn.MavenResolver");
assertEquals("There should be two registrations/unregistrations of MavenResolver before final (ConfigAdmin based) registration",
5, count);
// The number of registrations isn't fixed: ConfigAdmin either calls updated(null) first (one more
// bundlecontext based resolver) or directly delivers the etc/org.ops4j.pax.url.mvn.cfg configuration,
// depending on whether FileInstall has created it yet.
List<ServiceEvent> registrations = awaitFinalRegistration(MAVEN_RESOLVER, CONFIGURATION, "configadmin");
assertEquals("The first MavenResolver should be registered before the ConfigAdmin configuration, events: "
+ describe(serviceEventsFor(MAVEN_RESOLVER), CONFIGURATION),
"bundlecontext", registrations.get(0).getServiceReference().getProperty(CONFIGURATION));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;

import java.io.File;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -29,12 +30,13 @@
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.osgi.framework.ServiceEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Test shows that without <code>org.ops4j.pax.url.mvn.requireConfigAdminConfig=true</code>,
* there are two instances of MavenResolver service being published.
* Test shows that with <code>org.ops4j.pax.url.mvn.requireConfigAdminConfig=true</code>,
* the MavenResolver service is published only once, from the ConfigAdmin configuration.
*/
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerMethod.class)
Expand All @@ -56,8 +58,10 @@ public Option[] config() throws Exception {

@Test
public void mavenResolverAvailable() throws Exception {
long count = numberOfServiceEventsFor("org.ops4j.pax.url.mvn.MavenResolver");
assertEquals("There should be only one MavenResolver registration - after non-INITIAL ConfigAdmin update", 1l, count);
List<ServiceEvent> registrations = awaitFinalRegistration(MAVEN_RESOLVER, CONFIGURATION, "configadmin");
assertEquals("There should be only one MavenResolver registration - after non-INITIAL ConfigAdmin update, events: "
+ describe(serviceEventsFor(MAVEN_RESOLVER), CONFIGURATION),
1, registrations.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
*/
package org.apache.karaf.itests.monitoring;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;

public class ServiceMonitor implements ServiceListener {

private List<ServiceEvent> events = new LinkedList<>();
// events are added by the registering threads while the test reads them
private final List<ServiceEvent> events = new CopyOnWriteArrayList<>();

@Override
public void serviceChanged(ServiceEvent event) {
Expand Down
Loading