public static Predicate<Iterable<?>> empty() { return sizeEquals(0); }
@Test public void testListSize() { Assert.assertTrue(CollectionFunctionals.sizeEquals(2).apply(ImmutableList.of("x", "y"))); Assert.assertFalse(CollectionFunctionals.sizeEquals(2).apply(null)); Assert.assertTrue(CollectionFunctionals.sizeEquals(0).apply(ImmutableList.of())); Assert.assertFalse(CollectionFunctionals.sizeEquals(0).apply(null)); }
public static Predicate<Iterable<?>> emptyOrNull() { return Predicates.or(Predicates.isNull(), sizeEquals(0)); }
protected void assertSize(int targetSize, int quarantineSize, final int deletedSize) { assertSize(targetSize, quarantineSize); Asserts.eventuallyOnNotify(entitiesRemoved, CollectionFunctionals.sizeEquals(deletedSize)); }
@Test public void testManagedLocationsSimpleCreateAndCleanup() { Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(0)); Location loc = mgmt.getLocationRegistry().getLocationManaged("localhost"); Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(1)); mgmt.getLocationManager().unmanage(loc); Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(0)); }
private void assertLocationRegistryCount(int size) { Asserts.assertThat(mgmt().getLocationRegistry().getDefinedLocations(true).keySet(), CollectionFunctionals.sizeEquals(size)); } private void assertLocationManagerInstancesCount(int size) {
@Test public void testManagedLocationsNamedCreateAndCleanup() { Asserts.assertThat(mgmt.getLocationRegistry().getDefinedLocations(true).keySet(), CollectionFunctionals.sizeEquals(0)); Asserts.assertThat(mgmt.getCatalog().getCatalogItems(), CollectionFunctionals.sizeEquals(0)); Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(0)); mgmt.getLocationRegistry().updateDefinedLocationNonPersisted( new BasicLocationDefinition("lh1", "localhost", null) ); Asserts.assertThat(mgmt.getLocationRegistry().getDefinedLocations(true).keySet(), CollectionFunctionals.sizeEquals(1)); Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(0)); // currently such defined locations do NOT appear in catalog -- see CatalogYamlLocationTest Asserts.assertThat(mgmt.getCatalog().getCatalogItems(), CollectionFunctionals.sizeEquals(0)); Location loc = mgmt.getLocationRegistry().getLocationManaged("lh1"); Asserts.assertThat(mgmt.getLocationRegistry().getDefinedLocations(true).keySet(), CollectionFunctionals.sizeEquals(1)); Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(1)); mgmt.getLocationManager().unmanage(loc); Asserts.assertThat(mgmt.getLocationRegistry().getDefinedLocations(true).keySet(), CollectionFunctionals.sizeEquals(1)); Asserts.assertThat(locationManager.getLocations(), CollectionFunctionals.sizeEquals(0)); }
private void assertCatalogCount(int size) { Asserts.assertThat(mgmt().getCatalog().getCatalogItems(), CollectionFunctionals.sizeEquals(size)); }
private void assertLocationManagerInstancesCount(int size) { Asserts.assertThat(mgmt().getLocationManager().getLocations(), CollectionFunctionals.sizeEquals(size)); } private void assertCatalogCount(int size) {
@Test public void defaultRemovalStrategyShutsDownNewestFirstWhenResizing() throws Exception { final List<Entity> creationOrder = Lists.newArrayList(); DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure("initialSize", 0) .configure("memberSpec", EntitySpec.create(TestEntity.class))); cluster.subscriptions().subscribe(cluster, AbstractEntity.CHILD_ADDED, new SensorEventListener<Entity>() { @Override public void onEvent(SensorEvent<Entity> event) { if (event.getValue() instanceof TestEntity) { creationOrder.add(event.getValue()); } }}); cluster.start(ImmutableList.of(loc)); cluster.resize(1); //Prevent the two entities created in the same ms //so that the removal strategy can always choose the //entity created next Thread.sleep(1); cluster.resize(2); Asserts.eventually(Suppliers.ofInstance(creationOrder), CollectionFunctionals.sizeEquals(2)); assertEquals(cluster.getCurrentSize(), (Integer)2); assertEquals(ImmutableSet.copyOf(cluster.getMembers()), ImmutableSet.copyOf(creationOrder), "actual="+cluster.getMembers()); // Now stop one cluster.resize(1); assertEquals(cluster.getCurrentSize(), (Integer)1); assertEquals(ImmutableList.copyOf(cluster.getMembers()), creationOrder.subList(0, 1)); }
@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()); }
.build()); Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(1)); SensorEvent<String> initialEvent = Iterables.getOnlyElement(record); assertEquals(initialEvent.getSensor(), STR2); Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(2)); EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myval"); Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(3)); EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myval2"); Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(5));
Asserts.assertThat((Iterable<?>)result, CollectionFunctionals.sizeEquals(1)); Asserts.assertThat(app.getChildren(), CollectionFunctionals.sizeEquals(1)); Entity child = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(child.getId(), Iterables.getOnlyElement((Iterable<?>)result));
mgmt.getExecutionManager().submit(overrideJob); Asserts.eventually(Suppliers.ofInstance(seenValues), CollectionFunctionals.sizeEquals(2)); Asserts.succeedsContinually(new Runnable() { @Override
/** * Enricher sees state in the wrong order -> running, starting, running * * Indeterministic, fails a couple of times per 100 invocations when run with "mvn test" in the * brooklyn-itest docker container. */ @Test(groups="Broken") public void testWrongSensorInitValue() { TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) .impl(TestApplicationNoEnrichersImpl.class) .enricher(EnricherSpec.create(RecodingChildSensorEnricher.class)) .child(EntitySpec.create(TestEntity.class) .impl(TestEntityNoEnrichersImpl.class))); Entity child = Iterables.get(app.getChildren(), 0); child.sensors().set(RecodingChildSensorEnricher.RECORDED_SENSOR, "first"); child.sensors().set(RecodingChildSensorEnricher.RECORDED_SENSOR, "second"); final RecodingChildSensorEnricher enricher = getFirstEnricher(app, RecodingChildSensorEnricher.class); // setEntity -> onUpdate // CHILD_ADDED -> onUpdate // set RECORDED_SENSOR=first -> onUpdate // set RECORDED_SENSOR=second -> onUpdate Asserts.eventually(Suppliers.ofInstance(enricher.seenValues), CollectionFunctionals.sizeEquals(4)); boolean isOrdered = Ordering.explicit(MutableList.of("first", "second")) .nullsFirst() .isOrdered(enricher.seenValues); assertTrue(isOrdered, "Unexpected ordering for " + enricher.seenValues); }
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)); }