use of com.twitter.common.application.modules.LifecycleModule.LaunchException 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.LaunchException 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