/** As {@link #continually(Supplier, Predicate, Duration, Duration, String)} with defaults. */ public static <T> void continually(Supplier<? extends T> supplier, Predicate<T> predicate) { continually(ImmutableMap.<String,Object>of(), supplier, predicate); }
/** @deprecated since 0.9.0 use {@link #continually(Supplier, Predicate, Duration, Duration, String)} */ @Deprecated public static <T> void continually(Map<String,?> flags, Supplier<? extends T> supplier, Predicate<? super T> predicate) { continually(flags, supplier, predicate, null); }
/** @deprecated since 0.9.0 use {@link #continually(Supplier, Predicate, Duration, Duration, String)} */ @Deprecated public static <T> void continually(Map<String,?> flags, Supplier<? extends T> supplier, Predicate<T> predicate, String errMsg) { continually(supplier, predicate, toDuration(flags.get("timeout"), toDuration(flags.get("duration"), null)), toDuration(flags.get("period"), null), null); } /**
protected void assertCallHistoryNeverContinually(TestEntity entity, String effector) { Asserts.continually(() -> entity.getCallHistory(), l -> !l.contains(effector)); }
@Test(groups = "Integration") public void testEntitiesExistOnHotBackup() throws Exception { initHaCluster(HighAvailabilityMode.AUTO, HighAvailabilityMode.HOT_BACKUP); Asserts.continually( new ReturnCodeSupplier(), Predicates.or(Predicates.equalTo(200), Predicates.equalTo(403)), Duration.THIRTY_SECONDS, null, null); }
@Test(groups="Integration") // because slow public void testNothingHappensWhenSensorRepublishedUnchanged() { final ImmutableSet<Integer> input1 = ImmutableSet.of(1, 2, 3); testEntity.sensors().set(DEFAULT_SENSOR, input1); addSetChangePolicy(true, true); testEntity.sensors().set(DEFAULT_SENSOR, input1); // Neither effector should be invoked. Asserts.continually(CollectionFunctionals.sizeSupplier(onAddedParameters), Predicates.equalTo(0)); Asserts.continually(CollectionFunctionals.sizeSupplier(onRemovedParameters), Predicates.equalTo(0)); }
@Test public void testOnRemovedEffectorNotCalledWhenItemsAdded() throws Exception { addSetChangePolicy(false, true); final Set<Integer> values = ImmutableSet.of(1); testEntity.sensors().set(DEFAULT_SENSOR, values); Asserts.continually(CollectionFunctionals.sizeSupplier(onRemovedParameters), Predicates.equalTo(0)); }
@Test public void testOnAddedEffectorNotCalledWhenItemRemoved() throws Exception { testEntity.sensors().set(DEFAULT_SENSOR, ImmutableSet.of(1)); addSetChangePolicy(true, false); testEntity.sensors().set(DEFAULT_SENSOR, ImmutableSet.<Integer>of()); Asserts.continually(CollectionFunctionals.sizeSupplier(onRemovedParameters), Predicates.equalTo(0)); }
@Test public void testSendMultipleInOrderThenUnsubscribe() throws Exception { HelloEntity h = app.createAndManageChild(EntitySpec.create(HelloEntity.class)); app.start(ImmutableList.of(loc)); final List<Integer> data = Lists.newArrayList(); final CountDownLatch latch = new CountDownLatch(5); app.subscriptions().subscribe(h, HelloEntity.AGE, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { data.add(event.getValue()); Time.sleep((int)(20*Math.random())); log.info("Thread "+Thread.currentThread()+" notify on subscription received for "+event.getValue()+", data is "+data); latch.countDown(); }}); Stopwatch stopwatch = Stopwatch.createStarted(); for (int i = 1; i <= 5; i++) { h.setAge(i); } assertTrue(latch.await(5000, TimeUnit.MILLISECONDS)); app.subscriptions().unsubscribeAll(); h.setAge(6); long totalTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); // TODO guava util for (1..5) Asserts.continually(MutableMap.of("timeout", 50), Suppliers.ofInstance(data), Predicates.<Object>equalTo(ImmutableList.of(1,2,3,4,5))); assertTrue(totalTime < 2000, "totalTime="+totalTime); //shouldn't have blocked for anywhere close to 2s (Aled says TODO: too time sensitive for BuildHive?) }
@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))); }
newManagementContext.getSubscriptionManager().publish(new BasicSensorEvent<String>(TestApplication.MY_ATTRIBUTE, null, "myvaltooearly")); Asserts.continually(Suppliers.ofInstance(MyLatchingEntityImpl.events), Predicates.equalTo(Collections.emptyList())); Asserts.continually(Suppliers.ofInstance(events), Predicates.equalTo(Collections.emptyList())); assertTrue(MyLatchingEntityImpl.managingStartedLatch.getCount() > 0); Asserts.continually(Suppliers.ofInstance(events), Predicates.equalTo(Collections.emptyList())); Asserts.continually(Suppliers.ofInstance(MyLatchingEntityImpl.events), Predicates.equalTo(Collections.emptyList()));
@Test public void testCountReflectsNumberOfExecutingEffectors() { final CountDownLatch effectorLatch = new CountDownLatch(1); final AttributeSensor<Boolean> policyIsBusy = Sensors.newBooleanSensor( "policyIsBusy"); final Effector<Void> blockingEffector = Effectors.effector(Void.class, "abstract-invoke-effector-policy-test") .impl(new BlockingEffector(effectorLatch)) .build(); final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); final TestAbstractInvokeEffectorPolicy policy = entity.policies().add( PolicySpec.create(TestAbstractInvokeEffectorPolicy.class) .configure(AbstractInvokeEffectorPolicy.IS_BUSY_SENSOR_NAME, policyIsBusy.getName())); final Task<?> effectorTask = policy.invoke(blockingEffector, ImmutableMap.<String, Object>of()); // expect isbusy on entity, effector incomplete. Supplier<Boolean> effectorTaskDoneSupplier = new Supplier<Boolean>() { @Override public Boolean get() { return effectorTask.isDone(); } }; Asserts.continually(effectorTaskDoneSupplier, Predicates.equalTo(false)); EntityAsserts.assertAttributeEqualsEventually(entity, policyIsBusy, true); effectorLatch.countDown(); Asserts.eventually(effectorTaskDoneSupplier, Predicates.equalTo(true)); EntityAsserts.assertAttributeEqualsEventually(entity, policyIsBusy, false); }
@Test(groups="Integration") // marked integration as it takes a wee while public void testStartSuspended() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forSensor(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(HttpPollConfig.forSensor(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .suspended() .build(); Asserts.continually(MutableMap.of("timeout", 500), Entities.attributeSupplier(entity, SENSOR_INT), Predicates.<Integer>equalTo(null)); int countWhenSuspended = server.getRequestCount(); feed.resume(); Asserts.eventually(Entities.attributeSupplier(entity, SENSOR_INT), Predicates.<Integer>equalTo(200)); if (server.getRequestCount() <= countWhenSuspended) Assert.fail("Request count failed to increment when feed was resumed, from "+countWhenSuspended+", still at "+server.getRequestCount()); log.info("RUN: "+countWhenSuspended+" - "+server.getRequestCount()); }