@Override public boolean apply(@Nullable Entity input) { return (input != null) && Entities.isManaged(input); } @Override
@Override public void run() { log.debug("destroying app "+app+" (managed? "+isManaged(app)+"; mgmt is "+mgmt+")"); try { destroy(app); log.debug("destroyed app "+app+"; mgmt now "+mgmt); } catch (Exception e) { log.warn("problems destroying app "+app+" (mgmt now "+mgmt+", will rethrow at least one exception): "+e); error.compareAndSet(null, e); } }})); }
@Override public void run() { assertFalse(Entities.isManaged(node)); } });
@Test public void testStartAndStopWhenManagedCallsChildren() { TestEntity child = app.createAndManageChild(EntitySpec.create(TestEntity.class)); assertTrue(Entities.isManaged(app)); assertTrue(Entities.isManaged(child)); app.start(locs); assertEquals(child.getCallHistory(), ImmutableList.of("start")); app.stop(); assertEquals(child.getCallHistory(), ImmutableList.of("start", "stop")); assertFalse(Entities.isManaged(child)); assertFalse(Entities.isManaged(app)); }
/** convenience for starting an entity, esp a new Startable instance which has been created dynamically * (after the application is started) */ public static void start(Entity e, Collection<? extends Location> locations) { if (!isManaged(e) && !manage(e)) { log.warn("Using deprecated discouraged mechanism to start management -- Entities.start(Application, Locations) -- caller should create and use the preferred management context"); startManagement(e); } if (e instanceof Startable) Entities.invokeEffector(e, e, Startable.START, MutableMap.of("locations", locations)).getUnchecked(); }
@Override public void run() { Set<Entity> newMembers = Sets.difference(ImmutableSet.copyOf(cluster.getMembers()), initialMembers); Set<Entity> removedMembers = Sets.difference(initialMembers, ImmutableSet.copyOf(cluster.getMembers())); assertEquals(removedMembers, ImmutableSet.of(e1)); assertEquals(newMembers.size(), 1); assertEquals(((TestEntity)Iterables.getOnlyElement(newMembers)).getCallHistory(), ImmutableList.of("start")); assertEquals(e1.getCallHistory(), ImmutableList.of("start", "stop")); assertFalse(Entities.isManaged(e1)); }}); }
@Override public void run() { Set<Entity> newMembers = Sets.difference(ImmutableSet.copyOf(cluster.getMembers()), initialMembers); Set<Entity> removedMembers = Sets.difference(initialMembers, ImmutableSet.copyOf(cluster.getMembers())); assertEquals(removedMembers, ImmutableSet.of(e1)); assertEquals(newMembers.size(), 1); assertEquals(((TestEntity)Iterables.getOnlyElement(newMembers)).getCallHistory(), ImmutableList.of("start")); assertEquals(e1.getCallHistory(), ImmutableList.of("start", "stop")); assertFalse(Entities.isManaged(e1)); }});
@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)); }}); }
@Override public Integer call() { if (!Entities.isManaged(MyServiceWithFeedsImpl.this)) { feedCalledWhenNotManaged = true; throw new IllegalStateException("Entity "+MyServiceWithFeedsImpl.this+" is not managed in feed.call"); } Integer oldVal = sensors().get(COUNTER); return (oldVal == null ? 0 : oldVal) + 1; } }))
@Test public void testExpungeMembersWithoutStop() throws Exception { group.addMember(e1); group.addMember(e2); group.expungeMembers(false); assertFalse(Entities.isManaged(e1)); assertFalse(Entities.isManaged(e2)); assertEquals(e1.getCallHistory(), ImmutableList.of()); assertEquals(e2.getCallHistory(), ImmutableList.of()); }
@Test public void testStartManagementOfEntityIsNoop() throws Exception { Entity app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); assertTrue(Entities.isManaged(app2)); Entities.startManagement(app2); assertTrue(Entities.isManaged(app2)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, app2))); }
@Test public void testStopOnManagedAppDoesNotStopPremanagedChildren() { app.start(locs); // deliberately unmanaged TestEntity child = new TestEntityImpl(app); assertFalse(Entities.isManaged(child)); app.stop(); assertEquals(child.getCallHistory(), ImmutableList.of()); } }
@Test public void testAddedChildSpec() throws Exception { TestEntity e = app.addChild(EntitySpec.create(TestEntity.class)); assertTrue(Entities.isManaged(e)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); }
@Test public void testNewApp() throws Exception { TestApplication app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); assertTrue(Entities.isManaged(app2)); assertTrue(mgmt.getApplications().contains(app2), "app="+app2+"; apps="+mgmt.getApplications()); app.addChild(app2); assertTrue(Entities.isManaged(app2)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, app2))); }
@Test public void testNewEntityWithParent() throws Exception { TestEntity e = app.addChild(EntitySpec.create(TestEntity.class) .parent(app)); assertTrue(Entities.isManaged(e)); assertEquals(e.getParent(), app); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); }
@Test public void testNewOrphanedEntityCanBeAddedToChild() throws Exception { TestEntity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)); app.addChild(e); assertTrue(Entities.isManaged(e)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); }
@Test public void testAddingSameChildAgainIsNoop() throws Exception { TestEntity e = app.addChild(EntitySpec.create(TestEntity.class) .parent(app)); app.addChild(e); assertTrue(Entities.isManaged(e)); assertEquals(e.getParent(), app); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); }
@Test public void testNewOrphanedEntityIsManaged() throws Exception { TestEntity e = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)); assertTrue(Entities.isManaged(e)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, e))); // Check that orphaned entity doesn't interfere with getApplications Asserts.assertEqualsIgnoringOrder(mgmt.getApplications(), ImmutableList.of(app)); }
@Test public void testManageIsNoop() throws Exception { TestEntity child = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .parent(app)); Entities.manage(child); assertTrue(Entities.isManaged(child)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, child))); }