Search in sources :

Example 1 with CapturedForward

use of org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward 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 2 with CapturedForward

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

the class MockProcessorContextTest method shouldCaptureOutputRecords.

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

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 3 with CapturedForward

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

the class MockProcessorContextTest method shouldCaptureOutputRecordsUsingTo.

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

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value, To.all());
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 4 with CapturedForward

use of org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward in project kafka by apache.

the class MockProcessorContextTest method shouldCaptureOutputRecords.

@Test
public void shouldCaptureOutputRecords() {
    final org.apache.kafka.streams.processor.AbstractProcessor<String, Long> processor = new org.apache.kafka.streams.processor.AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) Test(org.junit.jupiter.api.Test)

Example 5 with CapturedForward

use of org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward in project kafka by apache.

the class MockProcessorContextTest method shouldCaptureOutputRecordsUsingTo.

@Test
public void shouldCaptureOutputRecordsUsingTo() {
    final org.apache.kafka.streams.processor.AbstractProcessor<String, Long> processor = new org.apache.kafka.streams.processor.AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value, To.all());
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) Test(org.junit.jupiter.api.Test)

Aggregations

MockProcessorContext (org.apache.kafka.streams.processor.MockProcessorContext)6 CapturedForward (org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward)6 AbstractProcessor (org.apache.kafka.streams.processor.AbstractProcessor)3 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Iterator (java.util.Iterator)2 To (org.apache.kafka.streams.processor.To)2