Search in sources :

Example 76 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KStreamTransformTest method testTransform.

@Test
public void testTransform() {
    StreamsBuilder builder = new StreamsBuilder();
    TransformerSupplier<Number, Number, KeyValue<Integer, Integer>> transformerSupplier = new TransformerSupplier<Number, Number, KeyValue<Integer, Integer>>() {

        public Transformer<Number, Number, KeyValue<Integer, Integer>> get() {
            return new Transformer<Number, Number, KeyValue<Integer, Integer>>() {

                private int total = 0;

                @Override
                public void init(ProcessorContext context) {
                }

                @Override
                public KeyValue<Integer, Integer> transform(Number key, Number value) {
                    total += value.intValue();
                    return KeyValue.pair(key.intValue() * 2, total);
                }

                @Override
                public KeyValue<Integer, Integer> punctuate(long timestamp) {
                    return KeyValue.pair(-1, (int) timestamp);
                }

                @Override
                public void close() {
                }
            };
        }
    };
    final int[] expectedKeys = { 1, 10, 100, 1000 };
    MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
    KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(intSerde, intSerde));
    stream.transform(transformerSupplier).process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, expectedKey * 10);
    }
    driver.punctuate(2);
    driver.punctuate(3);
    assertEquals(6, processor.processed.size());
    String[] expected = { "2:10", "20:110", "200:1110", "2000:11110", "-1:2", "-1:3" };
    for (int i = 0; i < expected.length; i++) {
        assertEquals(expected[i], processor.processed.get(i));
    }
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Transformer(org.apache.kafka.streams.kstream.Transformer) TransformerSupplier(org.apache.kafka.streams.kstream.TransformerSupplier) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 77 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KStreamTransformValuesTest method testTransformWithKey.

@Test
public void testTransformWithKey() {
    StreamsBuilder builder = new StreamsBuilder();
    ValueTransformerWithKeySupplier<Integer, Number, Integer> valueTransformerSupplier = new ValueTransformerWithKeySupplier<Integer, Number, Integer>() {

        public ValueTransformerWithKey<Integer, Number, Integer> get() {
            return new ValueTransformerWithKey<Integer, Number, Integer>() {

                private int total = 0;

                @Override
                public void init(final ProcessorContext context) {
                }

                @Override
                public Integer transform(final Integer readOnlyKey, final Number value) {
                    total += value.intValue() + readOnlyKey;
                    return total;
                }

                @Override
                public void close() {
                }
            };
        }
    };
    final int[] expectedKeys = { 1, 10, 100, 1000 };
    KStream<Integer, Integer> stream;
    MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
    stream = builder.stream(topicName, Consumed.with(intSerde, intSerde));
    stream.transformValues(valueTransformerSupplier).process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, expectedKey * 10);
    }
    String[] expected = { "1:11", "10:121", "100:1221", "1000:12221" };
    assertArrayEquals(expected, processor.processed.toArray());
}
Also used : ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) ValueTransformerWithKeySupplier(org.apache.kafka.streams.kstream.ValueTransformerWithKeySupplier) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 78 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KStreamWindowAggregateTest method testJoin.

@Test
public void testJoin() {
    final StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    String topic2 = "topic2";
    KStream<String, String> stream1 = builder.stream(topic1, Consumed.with(strSerde, strSerde));
    KTable<Windowed<String>, String> table1 = stream1.groupByKey(Serialized.with(strSerde, strSerde)).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, TimeWindows.of(10).advanceBy(5), strSerde, "topic1-Canonized");
    MockProcessorSupplier<Windowed<String>, String> proc1 = new MockProcessorSupplier<>();
    table1.toStream().process(proc1);
    KStream<String, String> stream2 = builder.stream(topic2, Consumed.with(strSerde, strSerde));
    KTable<Windowed<String>, String> table2 = stream2.groupByKey(Serialized.with(strSerde, strSerde)).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, TimeWindows.of(10).advanceBy(5), strSerde, "topic2-Canonized");
    MockProcessorSupplier<Windowed<String>, String> proc2 = new MockProcessorSupplier<>();
    table2.toStream().process(proc2);
    MockProcessorSupplier<Windowed<String>, String> proc3 = new MockProcessorSupplier<>();
    table1.join(table2, new ValueJoiner<String, String, String>() {

        @Override
        public String apply(String p1, String p2) {
            return p1 + "%" + p2;
        }
    }).toStream().process(proc3);
    driver.setUp(builder, stateDir);
    setRecordContext(0, topic1);
    driver.process(topic1, "A", "1");
    driver.flushState();
    setRecordContext(1, topic1);
    driver.process(topic1, "B", "2");
    driver.flushState();
    setRecordContext(2, topic1);
    driver.process(topic1, "C", "3");
    driver.flushState();
    setRecordContext(3, topic1);
    driver.process(topic1, "D", "4");
    driver.flushState();
    setRecordContext(4, topic1);
    driver.process(topic1, "A", "1");
    driver.flushState();
    proc1.checkAndClearProcessResult("[A@0/10]:0+1", "[B@0/10]:0+2", "[C@0/10]:0+3", "[D@0/10]:0+4", "[A@0/10]:0+1+1");
    proc2.checkAndClearProcessResult();
    proc3.checkAndClearProcessResult();
    setRecordContext(5, topic1);
    driver.process(topic1, "A", "1");
    driver.flushState();
    setRecordContext(6, topic1);
    driver.process(topic1, "B", "2");
    driver.flushState();
    setRecordContext(7, topic1);
    driver.process(topic1, "D", "4");
    driver.flushState();
    setRecordContext(8, topic1);
    driver.process(topic1, "B", "2");
    driver.flushState();
    setRecordContext(9, topic1);
    driver.process(topic1, "C", "3");
    driver.flushState();
    proc1.checkAndClearProcessResult("[A@0/10]:0+1+1+1", "[A@5/15]:0+1", "[B@0/10]:0+2+2", "[B@5/15]:0+2", "[D@0/10]:0+4+4", "[D@5/15]:0+4", "[B@0/10]:0+2+2+2", "[B@5/15]:0+2+2", "[C@0/10]:0+3+3", "[C@5/15]:0+3");
    proc2.checkAndClearProcessResult();
    proc3.checkAndClearProcessResult();
    setRecordContext(0, topic1);
    driver.process(topic2, "A", "a");
    driver.flushState();
    setRecordContext(1, topic1);
    driver.process(topic2, "B", "b");
    driver.flushState();
    setRecordContext(2, topic1);
    driver.process(topic2, "C", "c");
    driver.flushState();
    setRecordContext(3, topic1);
    driver.process(topic2, "D", "d");
    driver.flushState();
    setRecordContext(4, topic1);
    driver.process(topic2, "A", "a");
    driver.flushState();
    proc1.checkAndClearProcessResult();
    proc2.checkAndClearProcessResult("[A@0/10]:0+a", "[B@0/10]:0+b", "[C@0/10]:0+c", "[D@0/10]:0+d", "[A@0/10]:0+a+a");
    proc3.checkAndClearProcessResult("[A@0/10]:0+1+1+1%0+a", "[B@0/10]:0+2+2+2%0+b", "[C@0/10]:0+3+3%0+c", "[D@0/10]:0+4+4%0+d", "[A@0/10]:0+1+1+1%0+a+a");
    setRecordContext(5, topic1);
    driver.process(topic2, "A", "a");
    driver.flushState();
    setRecordContext(6, topic1);
    driver.process(topic2, "B", "b");
    driver.flushState();
    setRecordContext(7, topic1);
    driver.process(topic2, "D", "d");
    driver.flushState();
    setRecordContext(8, topic1);
    driver.process(topic2, "B", "b");
    driver.flushState();
    setRecordContext(9, topic1);
    driver.process(topic2, "C", "c");
    driver.flushState();
    proc1.checkAndClearProcessResult();
    proc2.checkAndClearProcessResult("[A@0/10]:0+a+a+a", "[A@5/15]:0+a", "[B@0/10]:0+b+b", "[B@5/15]:0+b", "[D@0/10]:0+d+d", "[D@5/15]:0+d", "[B@0/10]:0+b+b+b", "[B@5/15]:0+b+b", "[C@0/10]:0+c+c", "[C@5/15]:0+c");
    proc3.checkAndClearProcessResult("[A@0/10]:0+1+1+1%0+a+a+a", "[A@5/15]:0+1%0+a", "[B@0/10]:0+2+2+2%0+b+b", "[B@5/15]:0+2+2%0+b", "[D@0/10]:0+4+4%0+d+d", "[D@5/15]:0+4%0+d", "[B@0/10]:0+2+2+2%0+b+b+b", "[B@5/15]:0+2+2%0+b+b", "[C@0/10]:0+3+3%0+c+c", "[C@5/15]:0+3%0+c");
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Windowed(org.apache.kafka.streams.kstream.Windowed) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 79 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class AbstractStreamTest method testShouldBeExtensible.

@Test
public void testShouldBeExtensible() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    final MockProcessorSupplier<Integer, String> processor = new MockProcessorSupplier<>();
    final String topicName = "topic";
    ExtendedKStream<Integer, String> stream = new ExtendedKStream<>(builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String())));
    stream.randomFilter().process(processor);
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, "V" + expectedKey);
    }
    assertTrue(processor.processed.size() <= expectedKeys.length);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 80 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KStreamBranchTest method testKStreamBranch.

@SuppressWarnings("unchecked")
@Test
public void testKStreamBranch() {
    final StreamsBuilder builder = new StreamsBuilder();
    Predicate<Integer, String> isEven = new Predicate<Integer, String>() {

        @Override
        public boolean test(Integer key, String value) {
            return (key % 2) == 0;
        }
    };
    Predicate<Integer, String> isMultipleOfThree = new Predicate<Integer, String>() {

        @Override
        public boolean test(Integer key, String value) {
            return (key % 3) == 0;
        }
    };
    Predicate<Integer, String> isOdd = new Predicate<Integer, String>() {

        @Override
        public boolean test(Integer key, String value) {
            return (key % 2) != 0;
        }
    };
    final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6 };
    KStream<Integer, String> stream;
    KStream<Integer, String>[] branches;
    MockProcessorSupplier<Integer, String>[] processors;
    stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
    branches = stream.branch(isEven, isMultipleOfThree, isOdd);
    assertEquals(3, branches.length);
    processors = (MockProcessorSupplier<Integer, String>[]) Array.newInstance(MockProcessorSupplier.class, branches.length);
    for (int i = 0; i < branches.length; i++) {
        processors[i] = new MockProcessorSupplier<>();
        branches[i].process(processors[i]);
    }
    driver.setUp(builder);
    for (int expectedKey : expectedKeys) {
        driver.process(topicName, expectedKey, "V" + expectedKey);
    }
    assertEquals(3, processors[0].processed.size());
    assertEquals(1, processors[1].processed.size());
    assertEquals(2, processors[2].processed.size());
}
Also used : KStream(org.apache.kafka.streams.kstream.KStream) Predicate(org.apache.kafka.streams.kstream.Predicate) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Aggregations

MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)112 Test (org.junit.Test)109 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)34 HashMap (java.util.HashMap)29 MockStateStoreSupplier (org.apache.kafka.test.MockStateStoreSupplier)27 UUID (java.util.UUID)24 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)24 TaskId (org.apache.kafka.streams.processor.TaskId)24 SubscriptionInfo (org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo)23 HashSet (java.util.HashSet)21 MockInternalTopicManager (org.apache.kafka.test.MockInternalTopicManager)17 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)16 List (java.util.List)14 StreamsBuilderTest (org.apache.kafka.streams.StreamsBuilderTest)14 Metrics (org.apache.kafka.common.metrics.Metrics)13 Utils.mkList (org.apache.kafka.common.utils.Utils.mkList)11 Properties (java.util.Properties)9 TopicsInfo (org.apache.kafka.streams.processor.TopologyBuilder.TopicsInfo)8 InternalTopicConfig (org.apache.kafka.streams.processor.internals.InternalTopicConfig)8 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)8