@Test
  public void stopWithMultipleFailures() throws Exception {
    LifecycleManager underTest = new LifecycleManager();
    LifecycleSupport foo =
        new LifecycleSupport() {
          @Override
          protected void doStop() throws Exception {
            throw new TestException();
          }
        };
    LifecycleSupport bar =
        new LifecycleSupport() {
          @Override
          protected void doStop() throws Exception {
            throw new TestError();
          }
        };

    underTest.add(foo, bar);

    underTest.start();

    try {
      underTest.stop();
    } catch (MultipleFailuresException e) {
      List<Throwable> failures = e.getFailures();
      assertThat(failures.size(), is(2));
      assertThat(failures.get(0), instanceOf(TestError.class));
      assertThat(failures.get(1), instanceOf(TestException.class));

      assertState(foo, State.FAILED);
      assertState(bar, State.FAILED);
    }
  }
  @Test
  public void awareStartStopOrdering() throws Exception {
    final List<Lifecycle> started = new ArrayList<>();
    final List<Lifecycle> stopped = new ArrayList<>();

    LifecycleManager underTest = new LifecycleManager();
    final Lifecycle foo =
        new LifecycleSupport() {
          @Override
          protected void doStart() throws Exception {
            started.add(this);
          }

          @Override
          protected void doStop() throws Exception {
            stopped.add(this);
          }
        };
    final Lifecycle bar =
        new LifecycleSupport() {
          @Override
          protected void doStart() throws Exception {
            started.add(this);
          }

          @Override
          protected void doStop() throws Exception {
            stopped.add(this);
          }
        };

    underTest.add(
        new LifecycleAware() {
          @Nonnull
          @Override
          public Lifecycle getLifecycle() {
            return foo;
          }
        },
        new LifecycleAware() {
          @Nonnull
          @Override
          public Lifecycle getLifecycle() {
            return bar;
          }
        });

    assertThat(started.size(), is(0));
    assertThat(stopped.size(), is(0));

    underTest.start();
    assertThat(started.size(), is(2));
    assertThat(started, contains(foo, bar));

    underTest.stop();
    assertThat(stopped.size(), is(2));
    assertThat(stopped, contains(bar, foo));
  }
  @Test
  public void startStopOrdering() throws Exception {
    final List<LifecycleSupport> started = new ArrayList<>();
    final List<LifecycleSupport> stopped = new ArrayList<>();

    LifecycleManager underTest = new LifecycleManager();
    LifecycleSupport foo =
        new LifecycleSupport() {
          @Override
          protected void doStart() throws Exception {
            started.add(this);
          }

          @Override
          protected void doStop() throws Exception {
            stopped.add(this);
          }
        };
    Lifecycle bar =
        new LifecycleSupport() {
          @Override
          protected void doStart() throws Exception {
            started.add(this);
          }

          @Override
          protected void doStop() throws Exception {
            stopped.add(this);
          }
        };

    underTest.add(foo, bar);

    assertThat(started.size(), is(0));
    assertThat(stopped.size(), is(0));

    underTest.start();
    assertThat(started.size(), is(2));
    assertThat(started, contains(foo, bar));

    underTest.stop();
    assertThat(stopped.size(), is(2));
    assertThat(stopped, contains(bar, foo));
  }
Example #4
0
  public static LifecycleInjector createInjector(
      Stage stage, Collection<? extends Module> modules) {
    final LifecycleManager manager = new LifecycleManager();
    Injector injector;
    List<Module> l = new ArrayList<>();
    try {
      l.add(new LifecycleModule());
      l.add(
          new AbstractModule() {
            @Override
            protected void configure() {
              bind(LifecycleManager.class).toInstance(manager);
              requestInjection(manager);
            }
          });
      l.addAll(modules);

      injector = Guice.createInjector(stage, l);
    } catch (Exception e) {
      try {
        manager.notifyStartFailed(e);
      } catch (Exception e2) {
        System.err.println("Failed to notify LifecycleManager");
        e.printStackTrace();
      }
      throw e;
    }

    try {
      manager.notifyStarted();
      return new LifecycleInjector(injector, manager);
    } catch (Exception e) {
      manager.notifyShutdown();
      throw e;
    }
  }
Example #5
0
  protected void setUp(Injector injector) throws Exception {
    // calls all doSettings() for me and my parents
    PrivilegedAccessor.invokeAllStaticMethods(this.getClass(), "doSettings", null);

    this.injector = injector;
    // calls all doSettings() for me and my children

    //  rs=new RouterService(callback);
    assertEquals("unexpected port", SERVER_PORT, NetworkSettings.PORT.getValue());
    lifecycleManager = injector.getInstance(LifecycleManager.class);
    connectionServices = injector.getInstance(ConnectionServices.class);
    blockingConnectionFactory = injector.getInstance(BlockingConnectionFactory.class);
    pingReplyFactory = injector.getInstance(PingReplyFactory.class);
    headersFactory = injector.getInstance(HeadersFactory.class);

    lifecycleManager.start();
    connectionServices.connect();
    Thread.sleep(2000);
    assertEquals("unexpected port", SERVER_PORT, NetworkSettings.PORT.getValue());
  }
 @Inject
 LoggingProvisionMetricsLifecycleListener(LifecycleManager manager, ProvisionMetrics metrics) {
   this.metrics = metrics;
   manager.addListener(this);
 }
  @Test
  public void addStartStopRemove() throws Exception {
    LifecycleManager underTest = new LifecycleManager();
    LifecycleSupport foo = new LifecycleSupport();
    LifecycleSupport bar = new LifecycleSupport();

    assertState(underTest, State.NEW);
    assertState(foo, State.NEW);
    assertState(bar, State.NEW);

    underTest.add(foo);
    assertThat(underTest.size(), is(1));

    underTest.add(bar);
    assertThat(underTest.size(), is(2));

    underTest.start();
    assertState(underTest, State.STARTED);
    assertState(foo, State.STARTED);
    assertState(bar, State.STARTED);

    underTest.stop();
    assertState(underTest, State.STOPPED);
    assertState(foo, State.STOPPED);
    assertState(bar, State.STOPPED);

    underTest.remove(foo);
    assertThat(underTest.size(), is(1));

    underTest.remove(bar);
    assertThat(underTest.size(), is(0));
  }
Example #8
0
  private LifecycleInjector create() {
    // The logger is intentionally created here to avoid early static initialization
    // of SLF4J/LOG4J which may be customized using one of the bootstrap modules
    Logger LOG = LoggerFactory.getLogger(Governator.class);
    LOG.info("Using profiles : " + profiles);

    // Generate a single list of all discovered modules
    // TODO: Duplicates?
    final Set<Module> loadedModules = new HashSet<>();
    for (ModuleListProvider loader : moduleProviders) {
      loadedModules.addAll(loader.get());
    }

    final LifecycleManager manager = new LifecycleManager();

    Injector injector;
    try {
      injector =
          Guice.createInjector(
              stage,
              new LifecycleModule(),
              new AbstractModule() {
                @Override
                protected void configure() {
                  bind(LifecycleManager.class).toInstance(manager);
                  requestInjection(manager);
                }
              },
              create(
                  LOG,
                  manager,
                  loadedModules,
                  modules,
                  false,
                  // First, auto load the bootstrap modules (usually deal with configuration and
                  // logging) and
                  // use to load the main module.
                  create(
                      LOG,
                      manager,
                      loadedModules,
                      bootstrapModules,
                      true,
                      new DefaultModule() {
                        @Provides
                        PropertySource getPropertySource() {
                          return new DefaultPropertySource();
                        }
                      })));
    } catch (Throwable e) {
      try {
        manager.notifyStartFailed(e);
      } catch (Exception e2) {
        System.err.println("Failed to notify injector creation failure!");
        e2.printStackTrace(System.err);
      }
      throw new RuntimeException(e);
    }

    try {
      manager.notifyStarted();
      return new LifecycleInjector(injector, manager);
    } catch (Exception e) {
      manager.notifyShutdown();
      throw e;
    }
  }
Example #9
0
 @Override
 protected void tearDown() throws Exception {
   if (connectionServices != null) connectionServices.disconnect();
   if (lifecycleManager != null) lifecycleManager.shutdown();
 }