@Override public void run() { try { SensorEventListener<Object> noopListener = new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { } }; app.subscriptions().subscribe(null, TestEntity.SEQUENCE, noopListener); while (!Thread.currentThread().isInterrupted()) { SubscriptionHandle handle = app.subscriptions().subscribe(null, TestEntity.SEQUENCE, noopListener); app.subscriptions().unsubscribe(null, handle); } } catch (Exception e) { threadException.set(e); } } };
@Test public void testUnsubscribeUsingHandleStopsEvents() { SubscriptionHandle handle1 = entity.subscriptions().subscribe(observedEntity, TestEntity.SEQUENCE, listener); SubscriptionHandle handle2 = entity.subscriptions().subscribe(observedEntity, TestEntity.NAME, listener); SubscriptionHandle handle3 = entity.subscriptions().subscribe(otherEntity, TestEntity.SEQUENCE, listener); entity.subscriptions().unsubscribe(observedEntity, handle2); observedEntity.sensors().set(TestEntity.SEQUENCE, 123); observedEntity.sensors().set(TestEntity.NAME, "myname"); otherEntity.sensors().set(TestEntity.SEQUENCE, 456); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertEquals(listener.getEvents(), ImmutableList.of( new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedEntity, 123), new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456))); }}); }
@Test public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() { entity.subscriptions().subscribe(observedEntity, TestEntity.SEQUENCE, listener); entity.subscriptions().subscribe(observedEntity, TestEntity.NAME, listener); entity.subscriptions().subscribe(observedEntity, TestEntity.MY_NOTIF, listener); entity.subscriptions().subscribe(otherEntity, TestEntity.SEQUENCE, listener); entity.subscriptions().unsubscribe(observedEntity); observedEntity.sensors().set(TestEntity.SEQUENCE, 123); observedEntity.sensors().set(TestEntity.NAME, "myname"); observedEntity.sensors().emit(TestEntity.MY_NOTIF, 123); otherEntity.sensors().set(TestEntity.SEQUENCE, 456); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertEquals(listener.getEvents(), ImmutableList.of( new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456))); }}); }