Search in sources :

Example 1 with SegmentNotifier

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StateSynchronizer(io.pravega.client.state.StateSynchronizer) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BeforeClass(org.junit.BeforeClass) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collections.singletonList(java.util.Collections.singletonList) Mockito.verify(org.mockito.Mockito.verify) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) After(org.junit.After) Spy(org.mockito.Spy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InlineExecutor(io.pravega.test.common.InlineExecutor) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) ReusableLatch(io.pravega.common.util.ReusableLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReusableLatch(io.pravega.common.util.ReusableLatch) TimeUnit(java.util.concurrent.TimeUnit) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test)

Example 2 with SegmentNotifier

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());
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) ReaderGroupImpl(io.pravega.client.stream.impl.ReaderGroupImpl) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test) Assert.assertFalse(org.junit.Assert.assertFalse) Spy(org.mockito.Spy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InlineExecutor(io.pravega.test.common.InlineExecutor) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test)

Example 3 with SegmentNotifier

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());
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) ReaderGroupImpl(io.pravega.client.stream.impl.ReaderGroupImpl) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test) Assert.assertFalse(org.junit.Assert.assertFalse) Spy(org.mockito.Spy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InlineExecutor(io.pravega.test.common.InlineExecutor) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test)

Example 4 with SegmentNotifier

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());
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) ReaderGroupImpl(io.pravega.client.stream.impl.ReaderGroupImpl) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test) Assert.assertFalse(org.junit.Assert.assertFalse) Spy(org.mockito.Spy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InlineExecutor(io.pravega.test.common.InlineExecutor) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Test(org.junit.Test)

Aggregations

StateSynchronizer (io.pravega.client.state.StateSynchronizer)4 ReaderGroupState (io.pravega.client.stream.impl.ReaderGroupState)4 SegmentNotifier (io.pravega.client.stream.notifications.notifier.SegmentNotifier)4 InlineExecutor (io.pravega.test.common.InlineExecutor)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Test (org.junit.Test)4 RunWith (org.junit.runner.RunWith)4 Mock (org.mockito.Mock)4 Spy (org.mockito.Spy)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 ReaderGroupImpl (io.pravega.client.stream.impl.ReaderGroupImpl)3 Assert (org.junit.Assert)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 ReusableLatch (io.pravega.common.util.ReusableLatch)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 Slf4j (lombok.extern.slf4j.Slf4j)1