Search in sources :

Example 6 with TestRecord

use of org.apache.kafka.streams.test.TestRecord in project kafka by apache.

the class KTableSourceTest method testKTableSourceEmitOnChange.

// we have disabled KIP-557 until KAFKA-12508 can be properly addressed
@Ignore
@Test
public void testKTableSourceEmitOnChange() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String topic1 = "topic1";
    builder.table(topic1, Consumed.with(Serdes.String(), Serdes.Integer()), Materialized.as("store")).toStream().to("output");
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, Integer> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new IntegerSerializer());
        final TestOutputTopic<String, Integer> outputTopic = driver.createOutputTopic("output", new StringDeserializer(), new IntegerDeserializer());
        inputTopic.pipeInput("A", 1, 10L);
        inputTopic.pipeInput("B", 2, 11L);
        inputTopic.pipeInput("A", 1, 12L);
        inputTopic.pipeInput("B", 3, 13L);
        // this record should be kept since this is out of order, so the timestamp
        // should be updated in this scenario
        inputTopic.pipeInput("A", 1, 9L);
        assertEquals(1.0, getMetricByName(driver.metrics(), "idempotent-update-skip-total", "stream-processor-node-metrics").metricValue());
        assertEquals(asList(new TestRecord<>("A", 1, Instant.ofEpochMilli(10L)), new TestRecord<>("B", 2, Instant.ofEpochMilli(11L)), new TestRecord<>("B", 3, Instant.ofEpochMilli(13L)), new TestRecord<>("A", 1, Instant.ofEpochMilli(9L))), outputTopic.readRecordsToList());
    }
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with TestRecord

use of org.apache.kafka.streams.test.TestRecord in project kafka by apache.

the class SlidingWindowedCogroupedKStreamImplTest method slidingWindowAggregateStreamsTest.

@Test
public void slidingWindowAggregateStreamsTest() {
    final KTable<Windowed<String>, String> customers = windowedCogroupedStream.aggregate(MockInitializer.STRING_INIT, Materialized.with(Serdes.String(), Serdes.String()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
        final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE_MS), new StringDeserializer());
        testInputTopic.pipeInput("k1", "A", 500);
        testInputTopic.pipeInput("k2", "A", 500);
        testInputTopic.pipeInput("k2", "A", 501);
        testInputTopic.pipeInput("k1", "A", 502);
        testInputTopic.pipeInput("k1", "B", 503);
        testInputTopic.pipeInput("k2", "B", 503);
        testInputTopic.pipeInput("k2", "B", 504);
        testInputTopic.pipeInput("k1", "B", 504);
        final List<TestRecord<Windowed<String>, String>> results = testOutputTopic.readRecordsToList();
        final List<TestRecord<Windowed<String>, String>> expected = new LinkedList<>();
        // k1-A-500
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(0L, 500L)), "0+A", null, 500L));
        // k2-A-500
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(0L, 500L)), "0+A", null, 500L));
        // k2-A-501
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A", null, 501L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(1L, 501L)), "0+A+A", null, 501L));
        // k1-A-502
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A", null, 502L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(2L, 502L)), "0+A+A", null, 502L));
        // k1-B-503
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(503L, 1003L)), "0+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(3L, 503L)), "0+A+A+B", null, 503L));
        // k2-B-503
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(502L, 1002)), "0+B", null, 503L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(3L, 503L)), "0+A+A+B", null, 503L));
        // k2-B-504
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(502L, 1002L)), "0+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(501L, 1001L)), "0+A+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(504L, 1004L)), "0+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k2", new TimeWindow(4L, 504L)), "0+A+A+B+B", null, 504L));
        // k1-B-504
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(503L, 1003L)), "0+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(501L, 1001L)), "0+A+B+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(504L, 1004L)), "0+B", null, 504L));
        expected.add(new TestRecord<>(new Windowed<>("k1", new TimeWindow(4L, 504L)), "0+A+A+B+B", null, 504L));
        assertEquals(expected, results);
    }
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LinkedList(java.util.LinkedList) Windowed(org.apache.kafka.streams.kstream.Windowed) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord) Test(org.junit.Test)

Example 8 with TestRecord

use of org.apache.kafka.streams.test.TestRecord in project kafka by apache.

the class TestTopicsTest method testRecordsToList.

@Test
public void testRecordsToList() {
    final TestInputTopic<Long, String> inputTopic = testDriver.createInputTopic(INPUT_TOPIC_MAP, longSerde.serializer(), stringSerde.serializer());
    final TestOutputTopic<String, Long> outputTopic = testDriver.createOutputTopic(OUTPUT_TOPIC_MAP, stringSerde.deserializer(), longSerde.deserializer());
    final List<String> inputList = Arrays.asList("This", "is", "an", "example");
    final List<KeyValue<Long, String>> input = new LinkedList<>();
    final List<TestRecord<String, Long>> expected = new LinkedList<>();
    long i = 0;
    final Duration advance = Duration.ofSeconds(15);
    Instant recordInstant = Instant.parse("2019-06-01T10:00:00Z");
    for (final String s : inputList) {
        input.add(new KeyValue<>(i, s));
        expected.add(new TestRecord<>(s, i, recordInstant));
        i++;
        recordInstant = recordInstant.plus(advance);
    }
    inputTopic.pipeKeyValueList(input, Instant.parse("2019-06-01T10:00:00Z"), advance);
    final List<TestRecord<String, Long>> output = outputTopic.readRecordsToList();
    assertThat(output, is(equalTo(expected)));
}
Also used : Instant(java.time.Instant) Duration(java.time.Duration) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LinkedList(java.util.LinkedList) TestRecord(org.apache.kafka.streams.test.TestRecord) Test(org.junit.jupiter.api.Test)

Example 9 with TestRecord

use of org.apache.kafka.streams.test.TestRecord in project kafka by apache.

the class TestTopicsTest method testRecordList.

@Test
public void testRecordList() {
    final TestInputTopic<Long, String> inputTopic = testDriver.createInputTopic(INPUT_TOPIC_MAP, longSerde.serializer(), stringSerde.serializer());
    final TestOutputTopic<String, Long> outputTopic = testDriver.createOutputTopic(OUTPUT_TOPIC_MAP, stringSerde.deserializer(), longSerde.deserializer());
    final List<String> inputList = Arrays.asList("This", "is", "an", "example");
    final List<TestRecord<Long, String>> input = new LinkedList<>();
    final List<TestRecord<String, Long>> expected = new LinkedList<>();
    final Duration advance = Duration.ofSeconds(15);
    Instant recordInstant = testBaseTime;
    Long i = 0L;
    for (final String s : inputList) {
        input.add(new TestRecord<>(i, s, recordInstant));
        expected.add(new TestRecord<>(s, i, recordInstant));
        i++;
        recordInstant = recordInstant.plus(advance);
    }
    inputTopic.pipeRecordList(input);
    final List<TestRecord<String, Long>> output = outputTopic.readRecordsToList();
    assertThat(output, is(equalTo(expected)));
}
Also used : Instant(java.time.Instant) Duration(java.time.Duration) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestRecord(org.apache.kafka.streams.test.TestRecord) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 10 with TestRecord

use of org.apache.kafka.streams.test.TestRecord in project kafka by apache.

the class TopologyTestDriverTest method shouldProcessFromSourcesThatMatchMultiplePattern.

@Test
public void shouldProcessFromSourcesThatMatchMultiplePattern() {
    final Pattern pattern2Source1 = Pattern.compile("source-topic-\\d");
    final Pattern pattern2Source2 = Pattern.compile("source-topic-[A-Z]");
    final String consumerTopic2 = "source-topic-Z";
    final TestRecord<byte[], byte[]> consumerRecord2 = new TestRecord<>(key2, value2, null, timestamp2);
    testDriver = new TopologyTestDriver(setupMultipleSourcesPatternTopology(pattern2Source1, pattern2Source2), config);
    final List<TTDTestRecord> processedRecords1 = mockProcessors.get(0).processedRecords;
    final List<TTDTestRecord> processedRecords2 = mockProcessors.get(1).processedRecords;
    pipeRecord(SOURCE_TOPIC_1, testRecord1);
    assertEquals(1, processedRecords1.size());
    assertEquals(0, processedRecords2.size());
    final TTDTestRecord record1 = processedRecords1.get(0);
    final TTDTestRecord expectedResult1 = new TTDTestRecord(SOURCE_TOPIC_1, testRecord1, 0L);
    assertThat(record1, equalTo(expectedResult1));
    pipeRecord(consumerTopic2, consumerRecord2);
    assertEquals(1, processedRecords1.size());
    assertEquals(1, processedRecords2.size());
    final TTDTestRecord record2 = processedRecords2.get(0);
    final TTDTestRecord expectedResult2 = new TTDTestRecord(consumerTopic2, consumerRecord2, 0L);
    assertThat(record2, equalTo(expectedResult2));
}
Also used : Pattern(java.util.regex.Pattern) TestRecord(org.apache.kafka.streams.test.TestRecord) Test(org.junit.jupiter.api.Test)

Aggregations

TestRecord (org.apache.kafka.streams.test.TestRecord)18 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)14 Test (org.junit.Test)12 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)11 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)9 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Duration (java.time.Duration)6 LinkedList (java.util.LinkedList)5 Bytes (org.apache.kafka.common.utils.Bytes)5 TestInputTopic (org.apache.kafka.streams.TestInputTopic)5 Windowed (org.apache.kafka.streams.kstream.Windowed)5 Instant (java.time.Instant)4 List (java.util.List)4 KeyValue (org.apache.kafka.streams.KeyValue)4 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)4 Topology (org.apache.kafka.streams.Topology)4 Duration.ofMillis (java.time.Duration.ofMillis)3 ArrayList (java.util.ArrayList)3 Arrays.asList (java.util.Arrays.asList)3