Search in sources :

Example 1 with EndOfDataNotifier

use of io.pravega.client.stream.notifications.notifier.EndOfDataNotifier in project pravega by pravega.

the class ReaderGroupNotificationTest method testEndOfStreamNotifications.

@Test(timeout = 40000)
public void testEndOfStreamNotifications() throws Exception {
    final String streamName = "stream2";
    StreamConfiguration config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build();
    Controller controller = controllerWrapper.getController();
    controllerWrapper.getControllerService().createScope(SCOPE, 0L).get();
    controller.createStream(SCOPE, streamName, config).get();
    @Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().controllerURI(URI.create("tcp://localhost")).build());
    @Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(SCOPE, controller, connectionFactory);
    @Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().build());
    writer.writeEvent("0", "data1").get();
    // scale
    Stream stream = new StreamImpl(SCOPE, streamName);
    Map<Double, Double> map = new HashMap<>();
    map.put(0.0, 0.5);
    map.put(0.5, 1.0);
    Boolean result = controller.scaleStream(stream, Collections.singletonList(0L), map, executorService()).getFuture().get();
    assertTrue(result);
    writer.writeEvent("0", "data2").get();
    // seal stream
    assertTrue(controller.sealStream(SCOPE, streamName).get());
    @Cleanup ReaderGroupManager groupManager = new ReaderGroupManagerImpl(SCOPE, controller, clientFactory);
    groupManager.createReaderGroup("reader", ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(SCOPE, streamName)).groupRefreshTimeMillis(0).build());
    @Cleanup ReaderGroup readerGroup = groupManager.getReaderGroup("reader");
    @Cleanup EventStreamReader<String> reader1 = clientFactory.createReader("readerId", "reader", new JavaSerializer<>(), ReaderConfig.builder().initialAllocationDelay(0).build());
    // Add segment event listener
    Listener<EndOfDataNotification> l1 = notification -> {
        listenerInvoked.set(true);
        listenerLatch.release();
    };
    EndOfDataNotifier endOfDataNotifier = (EndOfDataNotifier) readerGroup.getEndOfDataNotifier(executorService());
    endOfDataNotifier.registerListener(l1);
    EventRead<String> event1 = reader1.readNextEvent(10000);
    assertEquals("data1", event1.getEvent());
    EventRead<String> emptyEvent = reader1.readNextEvent(0);
    assertNull(emptyEvent.getEvent());
    assertFalse(emptyEvent.isCheckpoint());
    readerGroup.initiateCheckpoint("cp", executorService());
    EventRead<String> cpEvent = reader1.readNextEvent(10000);
    assertTrue(cpEvent.isCheckpoint());
    EventRead<String> event2 = reader1.readNextEvent(10000);
    assertEquals("data2", event2.getEvent());
    emptyEvent = reader1.readNextEvent(0);
    assertNull(emptyEvent.getEvent());
    assertFalse(emptyEvent.isCheckpoint());
    emptyEvent = reader1.readNextEvent(0);
    assertNull(emptyEvent.getEvent());
    assertFalse(emptyEvent.isCheckpoint());
    readerGroup.initiateCheckpoint("cp2", executorService());
    cpEvent = reader1.readNextEvent(10000);
    assertTrue(cpEvent.isCheckpoint());
    emptyEvent = reader1.readNextEvent(0);
    assertNull(emptyEvent.getEvent());
    assertFalse(emptyEvent.isCheckpoint());
    endOfDataNotifier.pollNow();
    listenerLatch.await();
    assertTrue("Listener invoked", listenerInvoked.get());
}
Also used : EventStreamWriter(io.pravega.client.stream.EventStreamWriter) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) Cleanup(lombok.Cleanup) ReaderGroup(io.pravega.client.stream.ReaderGroup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) EventRead(io.pravega.client.stream.EventRead) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) After(org.junit.After) URI(java.net.URI) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) EndOfDataNotifier(io.pravega.client.stream.notifications.notifier.EndOfDataNotifier) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Slf4j(lombok.extern.slf4j.Slf4j) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert.assertFalse(org.junit.Assert.assertFalse) TestUtils(io.pravega.test.common.TestUtils) ControllerWrapper(io.pravega.test.integration.demo.ControllerWrapper) Controller(io.pravega.client.control.impl.Controller) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) StreamImpl(io.pravega.client.stream.impl.StreamImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SegmentNotification(io.pravega.client.stream.notifications.SegmentNotification) TestingServerStarter(io.pravega.test.common.TestingServerStarter) TestingServer(org.apache.curator.test.TestingServer) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) ReusableLatch(io.pravega.common.util.ReusableLatch) Before(org.junit.Before) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Listener(io.pravega.client.stream.notifications.Listener) Assert.assertNotNull(org.junit.Assert.assertNotNull) lombok.val(lombok.val) SegmentNotifier(io.pravega.client.stream.notifications.notifier.SegmentNotifier) Assert.assertTrue(org.junit.Assert.assertTrue) EventStreamReader(io.pravega.client.stream.EventStreamReader) Test(org.junit.Test) Assert.assertNull(org.junit.Assert.assertNull) ReaderConfig(io.pravega.client.stream.ReaderConfig) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) HashMap(java.util.HashMap) ReaderGroup(io.pravega.client.stream.ReaderGroup) Controller(io.pravega.client.control.impl.Controller) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) EndOfDataNotifier(io.pravega.client.stream.notifications.notifier.EndOfDataNotifier) StreamImpl(io.pravega.client.stream.impl.StreamImpl) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Stream(io.pravega.client.stream.Stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) Test(org.junit.Test)

Example 2 with EndOfDataNotifier

use of io.pravega.client.stream.notifications.notifier.EndOfDataNotifier in project pravega by pravega.

the class EndOfDataNotifierTest method endOfStreamNotifierTest.

@Test(timeout = 10000)
public void endOfStreamNotifierTest() throws Exception {
    AtomicBoolean listenerInvoked = new AtomicBoolean();
    when(state.isEndOfData()).thenReturn(false).thenReturn(true);
    when(sync.getState()).thenReturn(state);
    Listener<EndOfDataNotification> listener1 = notification -> {
        log.info("listener 1 invoked");
        listenerInvoked.set(true);
    };
    Listener<EndOfDataNotification> listener2 = notification -> {
    };
    EndOfDataNotifier notifier = new EndOfDataNotifier(system, sync, executor);
    notifier.registerListener(listener1);
    verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), eq(0L), anyLong(), any(TimeUnit.class));
    notifier.pollNow();
    verify(state, times(2)).isEndOfData();
    assertTrue(listenerInvoked.get());
    notifier.registerListener(listener2);
    verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), eq(0L), anyLong(), any(TimeUnit.class));
    notifier.unregisterAllListeners();
    verify(system, times(1)).removeListeners(EndOfDataNotification.class.getSimpleName());
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) EndOfDataNotifier(io.pravega.client.stream.notifications.notifier.EndOfDataNotifier) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) After(org.junit.After) Spy(org.mockito.Spy) InlineExecutor(io.pravega.test.common.InlineExecutor) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EndOfDataNotifier(io.pravega.client.stream.notifications.notifier.EndOfDataNotifier) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Example 3 with EndOfDataNotifier

use of io.pravega.client.stream.notifications.notifier.EndOfDataNotifier in project pravega by pravega.

the class EndOfDataNotifierTest method endOfStreamNotifierWithEmptyState.

@Test(timeout = 10000)
public void endOfStreamNotifierWithEmptyState() throws Exception {
    AtomicBoolean listenerInvoked = new AtomicBoolean();
    when(state.isEndOfData()).thenReturn(false).thenReturn(true);
    when(sync.getState()).thenReturn(null).thenReturn(state);
    Listener<EndOfDataNotification> listener1 = notification -> {
        log.info("listener 1 invoked");
        listenerInvoked.set(true);
    };
    EndOfDataNotifier notifier = new EndOfDataNotifier(system, sync, executor);
    notifier.registerListener(listener1);
    verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), eq(0L), anyLong(), any(TimeUnit.class));
    notifier.pollNow();
    verify(state, times(1)).isEndOfData();
    notifier.pollNow();
    verify(state, times(2)).isEndOfData();
    assertTrue(listenerInvoked.get());
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) EndOfDataNotifier(io.pravega.client.stream.notifications.notifier.EndOfDataNotifier) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) After(org.junit.After) Spy(org.mockito.Spy) InlineExecutor(io.pravega.test.common.InlineExecutor) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EndOfDataNotifier(io.pravega.client.stream.notifications.notifier.EndOfDataNotifier) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Aggregations

EndOfDataNotifier (io.pravega.client.stream.notifications.notifier.EndOfDataNotifier)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 StateSynchronizer (io.pravega.client.state.StateSynchronizer)2 ReaderGroupState (io.pravega.client.stream.impl.ReaderGroupState)2 ExecutorServiceHelpers (io.pravega.common.concurrent.ExecutorServiceHelpers)2 InlineExecutor (io.pravega.test.common.InlineExecutor)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Slf4j (lombok.extern.slf4j.Slf4j)2 After (org.junit.After)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)2 Mock (org.mockito.Mock)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2