Search in sources :

Example 1 with LaunchException

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());
}
Also used : LaunchException(com.twitter.common.application.modules.LifecycleModule.LaunchException) ServiceRunner(com.twitter.common.application.modules.LifecycleModule.ServiceRunner) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test)

Example 2 with LaunchException

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));
    }
}
Also used : LaunchException(com.twitter.common.application.modules.LifecycleModule.LaunchException) ServiceRunner(com.twitter.common.application.modules.LifecycleModule.ServiceRunner) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

LaunchException (com.twitter.common.application.modules.LifecycleModule.LaunchException)2 ServiceRunner (com.twitter.common.application.modules.LifecycleModule.ServiceRunner)2 ImmutableList (com.google.common.collect.ImmutableList)1 AbstractModule (com.google.inject.AbstractModule)1 Injector (com.google.inject.Injector)1 Module (com.google.inject.Module)1 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)1 Test (org.junit.Test)1