use of org.eclipse.ecf.discovery.IServiceEvent in project ecf by eclipse.
the class DiscoveryServiceTest method testAddServiceListenerWithRefresh.
public void testAddServiceListenerWithRefresh() {
IServiceInfo[] services = discoveryLocator.getServices();
assertTrue("No Services must be registerd at this point " + (services.length == 0 ? "" : services[0].toString()), services.length == 0);
final ThreadTestServiceListener tsl = new ThreadTestServiceListener(eventsToExpect, discoveryLocator, getName(), getTestId());
// OSGi
final Properties props = new Properties();
props.put(IDiscoveryLocator.CONTAINER_NAME, containerUnderTest);
final BundleContext ctxt = Activator.getDefault().getContext();
// Lock the listener first so that we won't miss any discovery event
synchronized (tsl) {
registerService();
// Check that services have been registered successfully
services = discoveryLocator.getServices();
assertTrue(eventsToExpect + " services must be registerd at this point " + (services.length > 0 ? "" : services.toString()), services.length == eventsToExpect);
// Register listener with OSGi
ServiceRegistration registration = ctxt.registerService(IServiceListener.class.getName(), tsl, props);
// register a service which we expect the test listener to get notified of
try {
tsl.wait(waitTimeForProvider);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
fail("Some discovery unrelated threading issues?");
} finally {
registration.unregister();
}
}
final IContainerEvent[] event = tsl.getEvent();
assertNotNull("Test listener didn't receive any discovery event", event);
// Diff the expected ids with what actually has been discovered. The
// remainig events are those missing.
final Set ids = new HashSet(idsToExpect);
ids.removeAll(getContainerIds(event));
assertTrue("Test misses " + ids.size() + " event(s) from container(s) out of " + idsToExpect + ". Those Ids are: " + ids, ids.size() == 0);
assertEquals("Test listener received unexpected amount of discovery events: \n\t" + Arrays.asList(event), eventsToExpect, event.length);
assertTrue("Discovery event must have originated from backend thread. Thread is: " + tsl.getCallingThread(), Thread.currentThread() != tsl.getCallingThread() && tsl.getCallingThread() != null);
IServiceInfo serviceInfo2 = ((IServiceEvent) event[eventsToExpect - 1]).getServiceInfo();
assertTrue("IServiceInfo should match, expected:\n\t" + serviceInfo + " but was \n\t" + serviceInfo2, comparator.compare(serviceInfo2, serviceInfo) == 0);
}
Aggregations