use of org.apache.kafka.streams.processor.Cancellable in project kafka by apache.
the class PunctuationQueueTest method testPunctuationIntervalCancelFromPunctuator.
@Test
public void testPunctuationIntervalCancelFromPunctuator() {
final PunctuationSchedule sched = new PunctuationSchedule(node, 0L, 100L, punctuator);
final long now = sched.timestamp - 100L;
final Cancellable cancellable = queue.schedule(sched);
final ProcessorNodePunctuator processorNodePunctuator = (node, timestamp, type, punctuator) -> {
punctuator.punctuate(timestamp);
// simulate scheduler cancelled from within punctuator
cancellable.cancel();
};
queue.mayPunctuate(now, PunctuationType.STREAM_TIME, processorNodePunctuator);
assertEquals(0, node.mockProcessor.punctuatedStreamTime().size());
queue.mayPunctuate(now + 100L, PunctuationType.STREAM_TIME, processorNodePunctuator);
assertEquals(1, node.mockProcessor.punctuatedStreamTime().size());
queue.mayPunctuate(now + 200L, PunctuationType.STREAM_TIME, processorNodePunctuator);
assertEquals(1, node.mockProcessor.punctuatedStreamTime().size());
}
use of org.apache.kafka.streams.processor.Cancellable in project kafka by apache.
the class WordCountTransformerTest method test.
@Test
public void test() {
final MockProcessorContext<String, String> context = new MockProcessorContext<>();
// Create and initialize the transformer under test; including its provided store
final WordCountTransformerDemo.MyTransformerSupplier supplier = new WordCountTransformerDemo.MyTransformerSupplier();
for (final StoreBuilder<?> storeBuilder : supplier.stores()) {
final StateStore store = storeBuilder.withLoggingDisabled().build();
store.init(context.getStateStoreContext(), store);
context.getStateStoreContext().register(store, null);
}
final Transformer<String, String, KeyValue<String, String>> transformer = supplier.get();
transformer.init(new org.apache.kafka.streams.processor.MockProcessorContext() {
@Override
public <S extends StateStore> S getStateStore(final String name) {
return context.getStateStore(name);
}
@Override
public <K, V> void forward(final K key, final V value) {
context.forward(new Record<>((String) key, (String) value, 0L));
}
@Override
public Cancellable schedule(final Duration interval, final PunctuationType type, final Punctuator callback) {
return context.schedule(interval, type, callback);
}
});
// send a record to the transformer
transformer.transform("key", "alpha beta\tgamma\n\talpha");
// note that the transformer does not forward during transform()
assertTrue(context.forwarded().isEmpty());
// now, we trigger the punctuator, which iterates over the state store and forwards the contents.
context.scheduledPunctuators().get(0).getPunctuator().punctuate(0L);
// finally, we can verify the output.
final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> capturedForwards = context.forwarded();
final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> expected = asList(new MockProcessorContext.CapturedForward<>(new Record<>("alpha", "2", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("beta", "1", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("gamma", "1", 0L)));
assertThat(capturedForwards, is(expected));
}
Aggregations