Search in sources :

Example 1 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class WordCountProcessorTest method test.

@Test
public void test() {
    final MockProcessorContext context = new MockProcessorContext();
    // Create, initialize, and register the state store.
    final KeyValueStore<String, Integer> store = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("Counts"), Serdes.String(), Serdes.Integer()).withLoggingDisabled().build();
    store.init(context, store);
    context.register(store, false, null);
    // Create and initialize the processor under test
    final Processor<String, String> processor = new WordCountProcessorDemo.MyProcessorSupplier().get();
    processor.init(context);
    // send a record to the processor
    processor.process("key", "alpha beta gamma alpha");
    // note that the processor commits, but does not forward, during process()
    assertTrue(context.committed());
    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 Iterator<MockProcessorContext.CapturedForward> capturedForwards = context.forwarded().iterator();
    assertEquals(new KeyValue<>("alpha", "2"), capturedForwards.next().keyValue());
    assertEquals(new KeyValue<>("beta", "1"), capturedForwards.next().keyValue());
    assertEquals(new KeyValue<>("gamma", "1"), capturedForwards.next().keyValue());
    assertFalse(capturedForwards.hasNext());
}
Also used : MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 2 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldCapturePunctuator.

@Test
public void shouldCapturePunctuator() {
    final Processor<String, Long> processor = new Processor<String, Long>() {

        @Override
        public void init(final ProcessorContext context) {
            context.schedule(1000L, PunctuationType.WALL_CLOCK_TIME, new Punctuator() {

                @Override
                public void punctuate(final long timestamp) {
                    context.commit();
                }
            });
        }

        @Override
        public void process(final String key, final Long value) {
        }

        @Override
        public void punctuate(final long timestamp) {
        }

        @Override
        public void close() {
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    final MockProcessorContext.CapturedPunctuator capturedPunctuator = context.scheduledPunctuators().get(0);
    assertEquals(1000L, capturedPunctuator.getIntervalMs());
    assertEquals(PunctuationType.WALL_CLOCK_TIME, capturedPunctuator.getType());
    assertFalse(capturedPunctuator.cancelled());
    final Punctuator punctuator = capturedPunctuator.getPunctuator();
    assertFalse(context.committed());
    punctuator.punctuate(1234L);
    assertTrue(context.committed());
}
Also used : Processor(org.apache.kafka.streams.processor.Processor) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) Punctuator(org.apache.kafka.streams.processor.Punctuator) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 3 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldCaptureRecordsOutputToChildByName.

@Test
public void shouldCaptureRecordsOutputToChildByName() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        private int count = 0;

        @Override
        public void process(final String key, final Long value) {
            if (count == 0) {
                // broadcast
                context().forward("start", -1L, To.all());
            }
            final To toChild = count % 2 == 0 ? To.child("george") : To.child("pete");
            context().forward(key + value, key.length() + value, toChild);
            count++;
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    {
        final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
        final CapturedForward forward1 = forwarded.next();
        assertEquals(new KeyValue<>("start", -1L), forward1.keyValue());
        assertEquals(null, forward1.childName());
        final CapturedForward forward2 = forwarded.next();
        assertEquals(new KeyValue<>("foo5", 8L), forward2.keyValue());
        assertEquals("george", forward2.childName());
        final CapturedForward forward3 = forwarded.next();
        assertEquals(new KeyValue<>("barbaz50", 56L), forward3.keyValue());
        assertEquals("pete", forward3.childName());
        assertFalse(forwarded.hasNext());
    }
    {
        final Iterator<CapturedForward> forwarded = context.forwarded("george").iterator();
        assertEquals(new KeyValue<>("start", -1L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
        assertFalse(forwarded.hasNext());
    }
    {
        final Iterator<CapturedForward> forwarded = context.forwarded("pete").iterator();
        assertEquals(new KeyValue<>("start", -1L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
        assertFalse(forwarded.hasNext());
    }
    {
        final Iterator<CapturedForward> forwarded = context.forwarded("steve").iterator();
        assertEquals(new KeyValue<>("start", -1L), forwarded.next().keyValue());
        assertFalse(forwarded.hasNext());
    }
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) Iterator(java.util.Iterator) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) To(org.apache.kafka.streams.processor.To) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 4 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldThrowIfForwardedWithDeprecatedChildIndex.

@Test
public void shouldThrowIfForwardedWithDeprecatedChildIndex() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            // noinspection deprecation
            context().forward(key, value, 0);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    try {
        processor.process("foo", 5L);
        fail("Should have thrown an UnsupportedOperationException.");
    } catch (final UnsupportedOperationException expected) {
    // expected
    }
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 5 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldCaptureCommitsAndAllowReset.

@Test
public void shouldCaptureCommitsAndAllowReset() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        private int count = 0;

        @Override
        public void process(final String key, final Long value) {
            if (++count > 2)
                context().commit();
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    assertFalse(context.committed());
    processor.process("foobar", 500L);
    assertTrue(context.committed());
    context.resetCommit();
    assertFalse(context.committed());
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Aggregations

MockProcessorContext (org.apache.kafka.streams.processor.MockProcessorContext)11 Test (org.junit.Test)11 AbstractProcessor (org.apache.kafka.streams.processor.AbstractProcessor)9 CapturedForward (org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward)3 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 TaskId (org.apache.kafka.streams.processor.TaskId)2 File (java.io.File)1 Processor (org.apache.kafka.streams.processor.Processor)1 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)1 Punctuator (org.apache.kafka.streams.processor.Punctuator)1 To (org.apache.kafka.streams.processor.To)1 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)1 InMemoryKeyValueStore (org.apache.kafka.streams.state.internals.InMemoryKeyValueStore)1