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());
}
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());
}
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());
}
Aggregations