@Test
  public void testServiceReplacerWorksAfterRebind() throws Exception {
    Location origLoc =
        origManagementContext
            .getLocationManager()
            .createLocation(LocationSpec.create(SimulatedLocation.class));
    DynamicCluster origCluster =
        origApp.createAndManageChild(
            EntitySpec.create(DynamicCluster.class)
                .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class))
                .configure(DynamicCluster.INITIAL_SIZE, 3));
    origApp.start(ImmutableList.<Location>of(origLoc));

    origCluster.addPolicy(
        PolicySpec.create(ServiceReplacer.class)
            .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED));

    // rebind
    TestApplication newApp = rebind();
    final DynamicCluster newCluster =
        (DynamicCluster)
            Iterables.find(newApp.getChildren(), Predicates.instanceOf(DynamicCluster.class));

    // stimulate the policy
    final Set<Entity> initialMembers = ImmutableSet.copyOf(newCluster.getMembers());
    final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 1);

    newApp
        .getManagementContext()
        .getSubscriptionManager()
        .subscribe(e1, HASensors.ENTITY_FAILED, eventListener);
    newApp
        .getManagementContext()
        .getSubscriptionManager()
        .subscribe(e1, HASensors.ENTITY_RECOVERED, eventListener);

    e1.emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure"));

    // Expect e1 to be replaced
    Asserts.succeedsEventually(
        new Runnable() {
          @Override
          public void run() {
            Set<Entity> newMembers =
                Sets.difference(ImmutableSet.copyOf(newCluster.getMembers()), initialMembers);
            Set<Entity> removedMembers =
                Sets.difference(initialMembers, ImmutableSet.copyOf(newCluster.getMembers()));
            assertEquals(removedMembers, ImmutableSet.of(e1));
            assertEquals(newMembers.size(), 1);
            assertEquals(
                ((TestEntity) Iterables.getOnlyElement(newMembers)).getCallHistory(),
                ImmutableList.of("start"));

            // TODO e1 not reporting "start" after rebind because callHistory is a field rather than
            // an attribute, so was not persisted
            Asserts.assertEqualsIgnoringOrder(e1.getCallHistory(), ImmutableList.of("stop"));
            assertFalse(Entities.isManaged(e1));
          }
        });
  }
  @BeforeMethod(alwaysRun = true)
  public void setUp() throws Exception {
    events = new CopyOnWriteArrayList<SensorEvent<FailureDescriptor>>();

    managementContext = Entities.newManagementContext();
    app = ApplicationBuilder.newManagedApp(TestApplication.class, managementContext);

    app.getManagementContext()
        .getSubscriptionManager()
        .subscribe(
            app,
            HASensors.CONNECTION_FAILED,
            new SensorEventListener<FailureDescriptor>() {
              @Override
              public void onEvent(SensorEvent<FailureDescriptor> event) {
                events.add(event);
              }
            });
    app.getManagementContext()
        .getSubscriptionManager()
        .subscribe(
            app,
            HASensors.CONNECTION_RECOVERED,
            new SensorEventListener<FailureDescriptor>() {
              @Override
              public void onEvent(SensorEvent<FailureDescriptor> event) {
                events.add(event);
              }
            });

    serverSocketAddress = startServerSocket();
  }
 @BeforeMethod(alwaysRun = true)
 public void setUp() throws Exception {
   for (int i = 0; i < 5; i++) System.gc();
   loc = new SimulatedLocation();
   app = ApplicationBuilder.newManagedApp(TestApplication.class);
   mgmt = app.getManagementContext();
 }
 private TestApplication rebind() throws Exception {
   RebindTestUtils.waitForPersisted(app);
   TestApplication result =
       (TestApplication) RebindTestUtils.rebind(mementoDir, getClass().getClassLoader());
   newManagementContext = result.getManagementContext();
   return result;
 }
 @AfterMethod(alwaysRun = true)
 public void tearDown() throws Exception {
   if (localManagementContext != null) Entities.destroyAll(localManagementContext);
   if (app != null) Entities.destroyAll(app.getManagementContext());
   if (persister != null) persister.stop();
   if (objectStore != null) objectStore.deleteCompletely();
   persister = null;
 }
 @AfterMethod(alwaysRun = true)
 @Override
 public void tearDown() throws Exception {
   super.tearDown();
   if (newApp != null) Entities.destroyAll(newApp.getManagementContext());
   if (newManagementContext != null) Entities.destroyAll(newManagementContext);
   if (mementoDir != null) RebindTestUtils.deleteMementoDir(mementoDir);
 }
  @Test
  public void testServiceFailureDetectorWorksAfterRebind() throws Exception {
    origEntity.addEnricher(EnricherSpec.create(ServiceFailureDetector.class));

    // rebind
    TestApplication newApp = rebind();
    final TestEntity newEntity =
        (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class));

    newApp
        .getManagementContext()
        .getSubscriptionManager()
        .subscribe(newEntity, HASensors.ENTITY_FAILED, eventListener);

    newEntity.setAttribute(TestEntity.SERVICE_UP, true);
    ServiceStateLogic.setExpectedState(newEntity, Lifecycle.RUNNING);

    // trigger the failure
    newEntity.setAttribute(TestEntity.SERVICE_UP, false);

    assertHasEventEventually(HASensors.ENTITY_FAILED, Predicates.<Object>equalTo(newEntity), null);
    assertEquals(events.size(), 1, "events=" + events);
  }
 @AfterMethod(alwaysRun = true)
 public void tearDown() throws Exception {
   if (app != null) Entities.destroyAll(app.getManagementContext());
 }
 @AfterMethod(alwaysRun = true)
 public void shutdown() {
   if (app != null) Entities.destroyAll(app.getManagementContext());
 }
 @AfterMethod(alwaysRun = true)
 public void tearDown() throws Exception {
   if (app != null) Entities.destroyAll(app.getManagementContext());
   if (tempDataDir != null) Os.deleteRecursively(tempDataDir);
 }
Example #11
0
 @BeforeMethod(alwaysRun = true)
 public void setUp() throws Exception {
   app = ApplicationBuilder.newManagedApp(TestApplication.class);
   entity = app.createAndManageChild(TestEntity.Spec.newInstance());
   managementContext = app.getManagementContext();
 }
 @BeforeMethod(alwaysRun = true)
 public void setUp() {
   app = ApplicationBuilder.newManagedApp(TestApplication.class);
   entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
   managementContext = app.getManagementContext();
 }
 @BeforeMethod(alwaysRun = true)
 public void setUp() throws Exception {
   app = TestApplication.Factory.newManagedInstanceForTests();
   mgmt = app.getManagementContext();
   entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
 }