use of io.pravega.client.stream.notifications.notifier.SegmentNotifier in project pravega by pravega.
the class SegmentNotifierTest method segmentNotifierTest.
@Test
public void segmentNotifierTest() throws Exception {
AtomicBoolean listenerInvoked = new AtomicBoolean();
ReusableLatch latch = new ReusableLatch();
when(state.getOnlineReaders()).thenReturn(new HashSet<>(singletonList("reader1")));
when(state.getNumberOfSegments()).thenReturn(1).thenReturn(2);
when(sync.getState()).thenReturn(state);
Listener<SegmentNotification> listener1 = e -> {
log.info("listener 1 invoked");
listenerInvoked.set(true);
latch.release();
};
Listener<SegmentNotification> listener2 = e -> {
};
SegmentNotifier notifier = new SegmentNotifier(system, sync, executor);
notifier.registerListener(listener1);
verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class));
latch.await();
assertTrue(listenerInvoked.get());
notifier.registerListener(listener2);
verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class));
// duplicate listener
notifier.registerListener(listener1);
notifier.unregisterAllListeners();
verify(system, times(1)).removeListeners(SegmentNotification.class.getSimpleName());
}
use of io.pravega.client.stream.notifications.notifier.SegmentNotifier in project pravega by pravega.
the class NotificationFrameworkTest method multipleNotificationTest.
@Test
public void multipleNotificationTest() {
final AtomicBoolean segListenerInvoked = new AtomicBoolean();
final AtomicBoolean customListenerInvoked = new AtomicBoolean();
Observable<SegmentNotification> segmentNotifier = new SegmentNotifier(notificationSystem, sync, executor);
Listener<SegmentNotification> segmentListener = e -> segListenerInvoked.set(true);
segmentNotifier.registerListener(segmentListener);
Observable<CustomNotification> customNotifier = new CustomNotifier(notificationSystem, executor);
Listener<CustomNotification> customListener = e -> customListenerInvoked.set(true);
customNotifier.registerListener(customListener);
// trigger notifications
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
assertTrue(segListenerInvoked.get());
assertFalse(customListenerInvoked.get());
segListenerInvoked.set(false);
// trigger notifications
notificationSystem.notify(CustomNotification.builder().build());
assertFalse(segListenerInvoked.get());
assertTrue(customListenerInvoked.get());
customNotifier.unregisterAllListeners();
customListenerInvoked.set(false);
// trigger notifications
notificationSystem.notify(CustomNotification.builder().build());
assertFalse(segListenerInvoked.get());
assertFalse(customListenerInvoked.get());
}
use of io.pravega.client.stream.notifications.notifier.SegmentNotifier in project pravega by pravega.
the class NotificationFrameworkTest method notificationFrameworkTest.
@Test
public void notificationFrameworkTest() {
final AtomicBoolean segNotificationReceived = new AtomicBoolean(false);
// Application can subscribe to segment notifications in the following way.
Observable<SegmentNotification> notifier = new SegmentNotifier(notificationSystem, sync, executor);
notifier.registerListener(segmentNotification -> {
int numReader = segmentNotification.getNumOfReaders();
int segments = segmentNotification.getNumOfSegments();
if (numReader < segments) {
System.out.println("Scale up number of readers based on my capacity");
} else {
System.out.println("More readers available time to shut down some");
}
segNotificationReceived.set(true);
});
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(3).numOfReaders(4).build());
assertTrue("Segment Notification notification received", segNotificationReceived.get());
segNotificationReceived.set(false);
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
assertTrue("Segment Notification notification received", segNotificationReceived.get());
segNotificationReceived.set(false);
notifier.unregisterAllListeners();
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
Assert.assertFalse("Segment Notification notification should not be received", segNotificationReceived.get());
final AtomicBoolean listener1Invoked = new AtomicBoolean();
final AtomicBoolean listener2Invoked = new AtomicBoolean();
Listener<SegmentNotification> listener1 = e -> listener1Invoked.set(true);
Listener<SegmentNotification> listener2 = e -> listener2Invoked.set(true);
notifier.registerListener(listener1);
notifier.registerListener(listener2);
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
assertTrue("Segment Notification notification not received on listener 1", listener1Invoked.get());
assertTrue("Segment Notification notification not received on listener 2", listener2Invoked.get());
notifier.unregisterListener(listener1);
notifier.unregisterListener(listener2);
listener1Invoked.set(false);
listener2Invoked.set(false);
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
Assert.assertFalse("Segment Notification notification received on listener 1", listener1Invoked.get());
Assert.assertFalse("Segment Notification notification received on listener 2", listener2Invoked.get());
}
use of io.pravega.client.stream.notifications.notifier.SegmentNotifier in project pravega by pravega.
the class NotificationFrameworkTest method notifierFactoryTest.
@Test
public void notifierFactoryTest() {
final AtomicBoolean segmentNotificationReceived = new AtomicBoolean(false);
// Application can subscribe to segment notifications in the following way.
final Observable<SegmentNotification> notifier = new SegmentNotifier(notificationSystem, sync, executor);
notifier.registerListener(segmentNotification -> {
int numReader = segmentNotification.getNumOfReaders();
int segments = segmentNotification.getNumOfSegments();
if (numReader < segments) {
System.out.println("Scale up number of readers based on my capacity");
} else {
System.out.println("More readers available time to shut down some");
}
segmentNotificationReceived.set(true);
});
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(3).numOfReaders(4).build());
assertTrue("Segment Notification notification received", segmentNotificationReceived.get());
segmentNotificationReceived.set(false);
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
assertTrue("Segment Notification notification received", segmentNotificationReceived.get());
segmentNotificationReceived.set(false);
notifier.unregisterAllListeners();
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
Assert.assertFalse("Segment Notification notification should not be received", segmentNotificationReceived.get());
final AtomicBoolean listener1Invoked = new AtomicBoolean();
final AtomicBoolean listener2Invoked = new AtomicBoolean();
Listener<SegmentNotification> listener1 = e -> listener1Invoked.set(true);
Listener<SegmentNotification> listener2 = e -> listener2Invoked.set(true);
notifier.registerListener(listener1);
notifier.registerListener(listener2);
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
assertTrue("Segment Notification notification not received on listener 1", listener1Invoked.get());
assertTrue("Segment Notification notification not received on listener 2", listener2Invoked.get());
notifier.unregisterListener(listener1);
notifier.unregisterListener(listener2);
listener1Invoked.set(false);
listener2Invoked.set(false);
// Trigger notification.
notificationSystem.notify(SegmentNotification.builder().numOfSegments(5).numOfReaders(4).build());
Assert.assertFalse("Segment Notification notification received on listener 1", listener1Invoked.get());
Assert.assertFalse("Segment Notification notification received on listener 2", listener2Invoked.get());
}
Aggregations