### Question:
KernelServiceRegistry implements ServiceRegistry { @Override public <T> ServiceRegistration<T> register(Module module, ServiceDefinition<T> definition) { synchronized (registries) { val coordinate = module.getCoordinate(); var registry = registries.get(coordinate); if (registry == null) { registry = new ModuleServiceRegistry(module, this); registries.put(coordinate, registry); } val reference = new DefaultServiceReference<T>(module, definition); val registration = new DefaultServiceRegistration<T>(reference, registry, definition); registry.register(registration); module .getTaskQueue() .schedule( new ServiceEventDispatchTask(ServiceEvents.REGISTERED, registration.getReference())); return registration; } } KernelServiceRegistry(); @Override void close(); @Override void initialize(@NonNull Kernel kernel); @Override Kernel getKernel(); @Override ServiceRegistration<T> register(Module module, ServiceDefinition<T> definition); @Override void unregister(ServiceRegistration<T> definition); @Override ServiceRegistrationSet getRegistrations(Module module);  }### Answer:
@Test void ensureRegistryDispatchesCorrectEventForServiceRegistration() { registerListener(listener, ServiceEvents.REGISTERED); registry.register( module, new DefaultServiceDefinition<>(String.class, "hello-service", "whatever")); verify(taskQueue, times(1)).schedule(any(Runnable.class)); }
@Test void ensureRegistryDispatchesCorrectEventForServiceRegistrationAndUnregistration() { registerListener(listener, ServiceEvents.REGISTERED, ServiceEvents.UNREGISTERED); val registration = registry.register( module, new DefaultServiceDefinition<>(String.class, "hello-service", "whatever")); registration.dispose(); verify(taskQueue, times(2)).schedule(any(Runnable.class)); }
@Test void ensureRegistryIsEmptyAfterFinalServiceUnregistration() { registerListener(listener, ServiceEvents.REGISTERED, ServiceEvents.UNREGISTERED); val registration = registry.register( module, new DefaultServiceDefinition<>(String.class, "hello-service", "whatever")); val kserviceReg = (KernelServiceRegistry) registry; assertFalse(kserviceReg.registries.isEmpty(), "Must have at least one service"); registration.dispose(); assertTrue(kserviceReg.registries.isEmpty(), "must have no registered services"); }
@Test void ensureModuleServiceRegistryRemovesServiceUponDisposal() { registerListener(listener, ServiceEvents.REGISTERED, ServiceEvents.UNREGISTERED); val registration = registry.register( module, new DefaultServiceDefinition<>(String.class, "hello-service", "whatever")); registry.register( module, new DefaultServiceDefinition<>(String.class, "world-service", "whatever2")); val kserviceReg = (KernelServiceRegistry) registry; val moduleRegistry = kserviceReg.registries.values().iterator().next(); assertEquals(2, moduleRegistry.registrations.size(), "must have 2 registrations"); registration.dispose(); assertEquals(1, moduleRegistry.registrations.size(), "must have 1 registration"); }
@Test void ensureModuleDisposalRemovesCorrectService() { registerListener(listener, ServiceEvents.REGISTERED, ServiceEvents.UNREGISTERED); val registration = registry.register( module, new DefaultServiceDefinition<>(String.class, "hello-service", "whatever")); val reg2 = registry.register( module, new DefaultServiceDefinition<>(String.class, "world-service", "whatever2")); val kserviceReg = (KernelServiceRegistry) registry; val moduleRegistry = kserviceReg.registries.values().iterator().next(); assertEquals(2, moduleRegistry.registrations.size(), "must have 2 registrations"); registration.dispose(); assertTrue(moduleRegistry.registrations.contains(reg2), "must remove correct registration"); }