Search in sources :

Example 1 with Cancellable

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());
}
Also used : Cancellable(org.apache.kafka.streams.processor.Cancellable) MockProcessorNode(org.apache.kafka.test.MockProcessorNode) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) Punctuator(org.apache.kafka.streams.processor.Punctuator) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) Cancellable(org.apache.kafka.streams.processor.Cancellable) Test(org.junit.Test)

Example 2 with Cancellable

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));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Cancellable(org.apache.kafka.streams.processor.Cancellable) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Record(org.apache.kafka.streams.processor.api.Record) Punctuator(org.apache.kafka.streams.processor.Punctuator) StateStore(org.apache.kafka.streams.processor.StateStore) Duration(java.time.Duration) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) Test(org.junit.jupiter.api.Test)

Aggregations

Cancellable (org.apache.kafka.streams.processor.Cancellable)2 PunctuationType (org.apache.kafka.streams.processor.PunctuationType)2 Punctuator (org.apache.kafka.streams.processor.Punctuator)2 Duration (java.time.Duration)1 KeyValue (org.apache.kafka.streams.KeyValue)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 MockProcessorContext (org.apache.kafka.streams.processor.api.MockProcessorContext)1 Record (org.apache.kafka.streams.processor.api.Record)1 MockProcessorNode (org.apache.kafka.test.MockProcessorNode)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1