/** As {@link #eventually(Supplier, Predicate, Duration, Duration, String)} with default. */ public static <T> void eventually(Supplier<? extends T> supplier, Predicate<T> predicate, Duration timeout) { eventually(supplier, predicate, timeout, null, null); }
/** As {@link #eventually(Supplier, Predicate, Duration, Duration, String)} with defaults. */ public static <T> void eventually(Supplier<? extends T> supplier, Predicate<T> predicate) { eventually(supplier, predicate, null, null, null); }
/** @deprecated since 0.9.0 use {@link #eventually(Supplier, Predicate, Duration, Duration, String)} */ @Deprecated public static <T> void eventually(Map<String,?> flags, Supplier<? extends T> supplier, Predicate<T> predicate) { eventually(flags, supplier, predicate, (String)null); } /** @deprecated since 0.9.0 use {@link #eventually(Supplier, Predicate, Duration, Duration, String)} */ @Deprecated
/** @deprecated since 0.9.0 use {@link #eventually(Supplier, Predicate, Duration, Duration, String)} */ @Deprecated public static <T> void eventually(Map<String,?> flags, Supplier<? extends T> supplier, Predicate<T> predicate, String errMsg) { eventually(supplier, predicate, toDuration(flags.get("timeout"), null), toDuration(flags.get("period"), null), errMsg); }
private void waitHealthy(VanillaSoftwareProcess proc) { Asserts.eventually(Suppliers.ofInstance(proc), EntityPredicates.attributeEqualTo(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING), Duration.FIVE_MINUTES); } }
private void assertActiveFeedsEventually(List<Feed> knownFeeds, int expectedCount) { Asserts.eventually(new CountActiveSupplier(knownFeeds), Predicates.equalTo(expectedCount)); }
private void waitFailed(VanillaSoftwareProcess proc) { Asserts.eventually(Suppliers.ofInstance(proc), EntityPredicates.attributeEqualTo(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE), Duration.FIVE_MINUTES); }
protected void assertPathDataEventually(HostAndPort hostAndPort, final String path, String expected) throws Exception { try (ZooKeeperTestSupport zkts = new ZooKeeperTestSupport(hostAndPort)) { final Supplier<String> dataSupplier = new Supplier<String>() { @Override public String get() { try { return new String(zkts.get(path)); } catch (Exception e) { throw Exceptions.propagate(e); } } }; Asserts.eventually(dataSupplier, Predicates.equalTo(expected)); } }
private <T> void assertConfigEqualsEventually(Configurable obj, ConfigKey<T> running, T val) { Asserts.eventually(() -> obj.config().get(running), Predicates.equalTo(val)); }
@Test public void testSeveralItemsAddedAndRemovedAtOnce() throws Exception { testEntity.sensors().set(DEFAULT_SENSOR, ImmutableSet.of(1, 2, 3)); addSetChangePolicy(true, true); testEntity.sensors().set(DEFAULT_SENSOR, ImmutableSet.of(3, 4, 5)); // 1 and 2 were removed, 4 and 5 were added. Asserts.eventually(new ConfigBagValueKeySupplier(onRemovedParameters), Predicates.<Collection<Object>>equalTo(ImmutableSet.<Object>of(1, 2))); Asserts.eventually(new ConfigBagValueKeySupplier(onAddedParameters), Predicates.<Collection<Object>>equalTo(ImmutableSet.<Object>of(4, 5))); }
protected <T> void assertConfigEqualsEventually(Configurable obj, ConfigKey<T> running, T val) { Asserts.eventually(() -> obj.config().get(running), Predicates.equalTo(val)); }
@Test public void testPublishesIsBusySensor() { final List<Boolean> isBusyValues = new CopyOnWriteArrayList<>(); testEntity.subscriptions().subscribe(testEntity, IS_BUSY_SENSOR, new SensorEventListener<Boolean>() { @Override public void onEvent(SensorEvent<Boolean> event) { isBusyValues.add(event.getValue()); } }); addSetChangePolicy(true, false); testEntity.sensors().set(DEFAULT_SENSOR, ImmutableSet.of(1)); List<Boolean> expected = ImmutableList.of(false, true, false); Asserts.eventually(Suppliers.ofInstance(isBusyValues), Predicates.equalTo(expected)); }
@Test public void testCollectionsAreConvertedToSets() { final List<Integer> input1 = ImmutableList.of( 1, 1, 2, 3, 4, 5, 2, 3, 4, 5); final List<Integer> input2 = ImmutableList.of(6, 5, 4, 3, 3); addSetChangePolicy(true, true); testEntity.sensors().set(DEFAULT_SENSOR, input1); Asserts.eventually(new ConfigBagValueKeySupplier(onAddedParameters), Predicates.<Collection<Object>>equalTo(ImmutableSet.<Object>of(1, 2, 3, 4, 5))); Asserts.continually(CollectionFunctionals.sizeSupplier(onRemovedParameters), Predicates.equalTo(0)); onAddedParameters.clear(); testEntity.sensors().set(DEFAULT_SENSOR, input2); Asserts.eventually(new ConfigBagValueKeySupplier(onAddedParameters), Predicates.<Collection<Object>>equalTo(ImmutableSet.<Object>of(6))); Asserts.eventually(new ConfigBagValueKeySupplier(onRemovedParameters), Predicates.<Collection<Object>>equalTo(ImmutableSet.<Object>of(1, 2))); }
@Test public void testRebindsSubscriptions() throws Exception { MyEntity2 origE = origApp.createAndManageChild(EntitySpec.create(MyEntity2.class).configure("subscribe", true)); newApp = rebind(); MyEntity2 newE = (MyEntity2) Iterables.find(newApp.getChildren(), Predicates.instanceOf(MyEntity2.class)); newApp.sensors().set(TestApplication.MY_ATTRIBUTE, "mysensorval"); Asserts.eventually(Suppliers.ofInstance(newE.getEvents()), Predicates.<List<String>>equalTo(ImmutableList.of("mysensorval"))); Assert.assertEquals(newE, origE); }
@Test public void testPropagatingEnricher() throws Exception { Entity app = createAndStartApplication(loadYaml("test-propagating-enricher.yaml")); waitForApplicationTasks(app); Assert.assertEquals(app.getDisplayName(), "test-propagating-enricher"); log.info("App started:"); Entities.dumpInfo(app); TestEntity entity = (TestEntity)app.getChildren().iterator().next(); entity.sensors().set(TestEntity.NAME, "New Name"); Asserts.eventually(Entities.attributeSupplier(app, TestEntity.NAME), Predicates.<String>equalTo("New Name")); }
@Test public void testSupplierIsPolled() throws Exception { entity = app.createAndManageChild(EntitySpec.create(DataEntity.class) .configure(DataEntity.POLL_PERIOD, 50L) .configure(DataEntity.SENSOR_SUPPLIER_MAP, MutableMap.<AttributeSensor<?>, Supplier<?>>of(longSensor, currentTimeMillis))); app.start(ImmutableList.of(loc)); Asserts.eventually(Entities.attributeSupplier(entity, longSensor), Predicates.notNull()); final Long first = entity.getAttribute(longSensor); assertNotNull(first); Asserts.succeedsEventually(new Runnable() { @Override public void run() { Long second = entity.getAttribute(longSensor); assertNotNull(second); assertTrue(second.longValue() > first.longValue()); } }); }
@Test(groups="Integration") public void testSetsServiceLifecycle() { ControlledDynamicWebAppCluster cluster = app.createAndManageChild( EntitySpec.create(ControlledDynamicWebAppCluster.class) .configure("initialSize", 1) .configure(ControlledDynamicWebAppCluster.MEMBER_SPEC, EntitySpec.create(TestJavaWebAppEntity.class)) ); EntityAsserts.assertAttributeEqualsEventually(cluster, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STOPPED); RecordingSensorEventListener<Lifecycle> listener = new RecordingSensorEventListener<Lifecycle>(true); app.subscriptions().subscribe(cluster, Attributes.SERVICE_STATE_ACTUAL, listener); app.start(locs); Asserts.eventually(Suppliers.ofInstance(listener.getEventValues()), CollectionFunctionals.sizeEquals(2)); assertEquals(listener.getEventValues(), ImmutableList.of(Lifecycle.STARTING, Lifecycle.RUNNING), "vals="+listener.getEventValues()); listener.clearEvents(); app.stop(); EntityAsserts.assertAttributeEqualsEventually(cluster, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STOPPED); Asserts.eventually(Suppliers.ofInstance(listener), CollectionFunctionals.sizeEquals(2)); assertEquals(listener.getEventValues(), ImmutableList.of(Lifecycle.STOPPING, Lifecycle.STOPPED), "vals="+listener.getEventValues()); }
@Test public void testPropagatesAllSensorsIncludesDynamicallyAdded() { AttributeSensor<String> dynamicAttribute = Sensors.newStringSensor("test.dynamicsensor.strattrib"); BasicNotificationSensor<String> dynamicNotificationSensor = new BasicNotificationSensor<String>(String.class, "test.dynamicsensor.strnotif"); app.enrichers().add(Enrichers.builder() .propagatingAll() .from(entity) .build()); entity.sensors().set(dynamicAttribute, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, dynamicAttribute, "foo"); // notification-sensor propagated final AtomicReference<String> notif = new AtomicReference<String>(); app.subscriptions().subscribe(app, dynamicNotificationSensor, new SensorEventListener<String>() { @Override public void onEvent(SensorEvent<String> event) { notif.set(event.getValue()); }}); entity.sensors().emit(dynamicNotificationSensor, "mynotifval"); Asserts.eventually(AtomicReferences.supplier(notif), Predicates.equalTo("mynotifval")); }
@Test public void testPropagatesAllStaticSensors() { app.enrichers().add(Enrichers.builder() .propagatingAll() .from(entity) .build()); // all attributes propagated entity.sensors().set(TestEntity.NAME, "foo"); entity.sensors().set(TestEntity.SEQUENCE, 2); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.SEQUENCE, 2); // notification-sensor propagated final AtomicReference<Integer> notif = new AtomicReference<Integer>(); app.subscriptions().subscribe(app, TestEntity.MY_NOTIF, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { notif.set(event.getValue()); }}); entity.sensors().emit(TestEntity.MY_NOTIF, 7); Asserts.eventually(AtomicReferences.supplier(notif), Predicates.equalTo(7)); }
public void testTransformingSuppressDuplicates() { RecordingSensorEventListener<String> record = new RecordingSensorEventListener<>(); app.getManagementContext().getSubscriptionManager().subscribe(entity, STR2, record); entity.enrichers().add(Enrichers.builder() .transforming(STR1) .publishing(STR2) .computing(Functions.<String>identity()) .suppressDuplicates(true) .build()); entity.sensors().set(STR1, "myval"); Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(1)); EntityAsserts.assertAttributeEquals(entity, STR2, "myval"); entity.sensors().set(STR1, "myval2"); entity.sensors().set(STR1, "myval2"); entity.sensors().set(STR1, "myval3"); EntityAsserts.assertAttributeEqualsContinually(entity, STR2, "myval3"); Asserts.assertThat(record.getEvents(), CollectionFunctionals.sizeEquals(3)); }