use of com.twitter.common.application.modules.LifecycleModule.ServiceRunner in project commons by twitter.
the class LifecycleModuleTest method testOrdering.
@Test
public void testOrdering() throws Exception {
final ServiceRunner runner = createMock(ServiceRunner.class);
Command shutdown = createMock(Command.class);
expect(runner.launch()).andReturn(LocalService.primaryService(100, shutdown));
shutdown.execute();
Module testModule = new AbstractModule() {
@Override
protected void configure() {
LifecycleModule.runnerBinder(binder()).addBinding().toInstance(runner);
}
};
Injector injector = Guice.createInjector(new SystemModule(), testModule);
LocalServiceRegistry registry = injector.getInstance(LocalServiceRegistry.class);
control.replay();
assertEquals(Optional.of(getLocalAddress(100)), registry.getPrimarySocket());
injector.getInstance(ShutdownRegistryImpl.class).execute();
}
use of com.twitter.common.application.modules.LifecycleModule.ServiceRunner in project commons by twitter.
the class LifecycleModuleTest method testFailedLauncher.
@Test(expected = IllegalStateException.class)
public void testFailedLauncher() throws Exception {
final ServiceRunner runner = createMock(ServiceRunner.class);
expect(runner.launch()).andThrow(new LaunchException("Injected failure."));
Module testModule = new AbstractModule() {
@Override
protected void configure() {
LifecycleModule.runnerBinder(binder()).addBinding().toInstance(runner);
}
};
Injector injector = Guice.createInjector(new SystemModule(), testModule);
LocalServiceRegistry registry = injector.getInstance(LocalServiceRegistry.class);
control.replay();
assertEquals(Optional.of(getLocalAddress(100)), registry.getPrimarySocket());
}
use of com.twitter.common.application.modules.LifecycleModule.ServiceRunner in project commons by twitter.
the class ServerSetModuleTest method mySetUp.
@Before
public void mySetUp() {
control = EasyMock.createControl();
serverSet = control.createMock(ServerSet.class);
shutdownRegistry = new ShutdownRegistryImpl();
zooKeeperClient = createZkClient();
Set<ServiceRunner> localServices = ImmutableSet.of();
localServiceRegistry = new LocalServiceRegistry(Providers.of(localServices), shutdownRegistry);
}
use of com.twitter.common.application.modules.LifecycleModule.ServiceRunner in project commons by twitter.
the class LocalServiceRegistry method ensureLaunched.
/**
* Launches the local services if not already launched, otherwise this is a no-op.
*/
void ensureLaunched() {
if (primarySocket == null) {
ImmutableList.Builder<LocalService> builder = ImmutableList.builder();
for (ServiceRunner runner : runnerProvider.get()) {
try {
LocalService service = runner.launch();
builder.add(service);
shutdownRegistry.addAction(service.shutdownCommand);
} catch (LaunchException e) {
throw new IllegalStateException("Failed to launch " + runner, e);
}
}
List<LocalService> localServices = builder.build();
Iterable<LocalService> primaries = Iterables.filter(localServices, IS_PRIMARY);
switch(Iterables.size(primaries)) {
case 0:
primarySocket = Optional.absent();
break;
case 1:
primarySocket = Optional.of(SERVICE_TO_SOCKET.apply(Iterables.getOnlyElement(primaries)));
break;
default:
throw new IllegalArgumentException("More than one primary local service: " + primaries);
}
Iterable<LocalService> auxSinglyNamed = Iterables.concat(FluentIterable.from(localServices).filter(Predicates.not(IS_PRIMARY)).transform(AUX_NAME_BREAKOUT));
Map<String, LocalService> byName;
try {
byName = Maps.uniqueIndex(auxSinglyNamed, GET_NAME);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Auxiliary services with identical names.", e);
}
auxiliarySockets = ImmutableMap.copyOf(Maps.transformValues(byName, SERVICE_TO_SOCKET));
}
}
Aggregations