final List<Exception> exceptions = new CopyOnWriteArrayList<Exception>(); app.subscriptions().subscribe(group, DynamicGroup.MEMBER_ADDED, new SensorEventListener<Entity>() { @Override public void onEvent(SensorEvent<Entity> event) { app.subscriptions().subscribe(group, DynamicGroup.MEMBER_REMOVED, new SensorEventListener<Entity>() { @Override public void onEvent(SensorEvent<Entity> event) {
/** * @deprecated since 0.9.0; for internal use only */ @Deprecated protected synchronized SubscriptionTracker getSubscriptionTracker() { if (_subscriptionTracker!=null) return _subscriptionTracker; if (entity==null) return null; _subscriptionTracker = new SubscriptionTracker(((EntityInternal)entity).subscriptions().getSubscriptionContext()); return _subscriptionTracker; }
@Test public void testSubscribeToChildAttributeChange() throws Exception { final CountDownLatch latch = new CountDownLatch(1); app.subscriptions().subscribeToChildren(app, TestEntity.SEQUENCE, new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { latch.countDown(); }}); entity.setSequenceValue(1234); if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) { fail("Timeout waiting for Event on child TestEntity listener"); } }
final Semaphore semaphore = new Semaphore(0); app.subscriptions().subscribe(item1, Movable.CONTAINER, new SensorEventListener<Entity>() { @Override public void onEvent(SensorEvent<Entity> event) { long eventTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
app.subscriptions().subscribe(null, ServiceReplacer.ENTITY_REPLACEMENT_FAILED, eventListener);
app.subscriptions().subscribe(app, TEST_SENSOR, new SensorEventListener<String>() { @Override public void onEvent(SensorEvent<String> event) {
final AtomicBoolean addingMemberDoLatching = group2impl.addingMemberDoLatching; app.subscriptions().subscribe(group2, AbstractGroup.MEMBER_ADDED, new SensorEventListener<Entity>() { @Override public void onEvent(SensorEvent<Entity> event) { membersAdded.add(event.getValue());
@Test public void testSetsOnFireWhenFailToReplaceMember() throws Exception { app.subscriptions().subscribe(null, ServiceReplacer.ENTITY_REPLACEMENT_FAILED, eventListener);
app.subscriptions().subscribe(tc, maxSizeReachedSensor, new SensorEventListener<MaxPoolSizeReachedEvent>() { @Override public void onEvent(SensorEvent<MaxPoolSizeReachedEvent> event) { maxReachedEvents.add(event.getValue());
app.subscriptions().subscribe(app, TEST_SENSOR, new SensorEventListener<String>() { @Override public void onEvent(SensorEvent<String> event) {
app.subscriptions().subscribe(e, TestEntity.SEQUENCE, SensorEventListener.NOOP); e.sensors().set(TestEntity.SEQUENCE, 1);
e2.subscriptions().subscribe(e2, TestEntity.SEQUENCE, eventListener);
.configure(BUCKET_FUNCTION, bucketFromAttribute(SENSOR)) ); app.subscriptions().subscribeToChildren(group, SENSOR, new SensorEventListener<String>() { @Override public void onEvent(SensorEvent<String> event) { dmg.rescanEntities(); }
@Test(groups="Integration") // 1s wait public void testStopFailureOfOldEntityDoesNotSetClusterOnFire() throws Exception { app.subscriptions().subscribe(null, ServiceReplacer.ENTITY_REPLACEMENT_FAILED, eventListener);
app2.subscriptions().subscribe(null, EntityWithEmitter.MY_NOTIF, new SensorEventListener<String>() { @Override public void onEvent(SensorEvent<String> event) {
@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?) }
entity.subscriptions().subscribe(entity, SENSOR_STRING, listener);
@Test(groups={"Integration", "Broken"}) // has a 1 second wait public void testDoesNotOnFireWhenFailToReplaceMember() throws Exception { app.subscriptions().subscribe(null, ServiceReplacer.ENTITY_REPLACEMENT_FAILED, eventListener); final DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(FailingEntity.class) .configure(FailingEntity.FAIL_ON_START_CONDITION, predicateOnlyTrueForCallAtOrAfter(2))) .configure(DynamicCluster.INITIAL_SIZE, 1) .configure(DynamicCluster.QUARANTINE_FAILED_ENTITIES, true)); app.start(ImmutableList.<Location>of(loc)); cluster.policies().add(PolicySpec.create(ServiceReplacer.class) .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED) .configure(ServiceReplacer.SET_ON_FIRE_ON_FAILURE, false)); final Set<Entity> initialMembers = ImmutableSet.copyOf(cluster.getMembers()); final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 0); e1.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure")); // Configured to not mark cluster as on fire Asserts.succeedsContinually(new Runnable() { @Override public void run() { assertNotEquals(cluster.getAttribute(Attributes.SERVICE_STATE_ACTUAL), Lifecycle.ON_FIRE, "Unexpected ON_FIRE state"); }}); // And will have received notification event about it assertEventuallyHasEntityReplacementFailedEvent(cluster); }
@Test(groups={"Integration", "Acceptance"}) public void testUpdateAttributeWithNoopListeners() { final int numIterations = numIterations(); double minRatePerSec = 1000 * PERFORMANCE_EXPECTATION; final AtomicInteger i = new AtomicInteger(); final AtomicInteger lastVal = new AtomicInteger(); app.subscriptions().subscribe(entity, TestEntity.SEQUENCE, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { lastVal.set(event.getValue()); }}); measure(PerformanceTestDescriptor.create() .summary("EntityPerformanceTest.testUpdateAttributeWithNoopListeners") .iterations(numIterations) .minAcceptablePerSecond(minRatePerSec) .job(new Runnable() { @Override public void run() { entity.sensors().set(TestEntity.SEQUENCE, (i.getAndIncrement())); }})); Asserts.succeedsEventually(MutableMap.of("timeout", TIMEOUT_MS), new Runnable() { @Override public void run() { assertTrue(lastVal.get() >= numIterations, "lastVal="+lastVal+"; numIterations="+numIterations); }}); }
@Test public void testEffectorEmitsTransientSensor() throws Exception { HelloEntity h = app.createAndManageChild(EntitySpec.create(HelloEntity.class)); app.start(ImmutableList.of(loc)); final AtomicReference<SensorEvent<?>> evt = new AtomicReference<SensorEvent<?>>(); app.subscriptions().subscribe(h, HelloEntity.ITS_MY_BIRTHDAY, new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { evt.set(event); synchronized (evt) { evt.notifyAll(); } }}); long startTime = System.currentTimeMillis(); synchronized (evt) { h.setAge(5); evt.wait(5000); } assertNotNull(evt.get()); assertEquals(HelloEntity.ITS_MY_BIRTHDAY, evt.get().getSensor()); assertEquals(h, evt.get().getSource()); assertNull(evt.get().getValue()); assertTrue(System.currentTimeMillis() - startTime < 5000); //shouldn't have blocked for all 5s }