Search in sources :

Example 1 with ServiceRunner

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();
}
Also used : ServiceRunner(com.twitter.common.application.modules.LifecycleModule.ServiceRunner) Command(com.twitter.common.base.Command) ShutdownRegistryImpl(com.twitter.common.application.ShutdownRegistry.ShutdownRegistryImpl) 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 ServiceRunner

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());
}
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 3 with ServiceRunner

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);
}
Also used : LocalServiceRegistry(com.twitter.common.application.modules.LocalServiceRegistry) ServerSet(com.twitter.common.zookeeper.ServerSet) ShutdownRegistryImpl(com.twitter.common.application.ShutdownRegistry.ShutdownRegistryImpl) ServiceRunner(com.twitter.common.application.modules.LifecycleModule.ServiceRunner) Before(org.junit.Before)

Example 4 with ServiceRunner

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));
    }
}
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

ServiceRunner (com.twitter.common.application.modules.LifecycleModule.ServiceRunner)4 AbstractModule (com.google.inject.AbstractModule)2 Injector (com.google.inject.Injector)2 Module (com.google.inject.Module)2 ShutdownRegistryImpl (com.twitter.common.application.ShutdownRegistry.ShutdownRegistryImpl)2 LaunchException (com.twitter.common.application.modules.LifecycleModule.LaunchException)2 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 LocalServiceRegistry (com.twitter.common.application.modules.LocalServiceRegistry)1 Command (com.twitter.common.base.Command)1 ServerSet (com.twitter.common.zookeeper.ServerSet)1 Before (org.junit.Before)1