Search in sources :

Example 16 with TestRecord

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

the class AbstractJoinIntegrationTest method runTestWithDriver.

void runTestWithDriver(final List<List<TestRecord<Long, String>>> expectedResult, final String storeName) {
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(STREAMS_CONFIG), STREAMS_CONFIG)) {
        final TestInputTopic<Long, String> right = driver.createInputTopic(INPUT_TOPIC_RIGHT, new LongSerializer(), new StringSerializer());
        final TestInputTopic<Long, String> left = driver.createInputTopic(INPUT_TOPIC_LEFT, new LongSerializer(), new StringSerializer());
        final TestOutputTopic<Long, String> outputTopic = driver.createOutputTopic(OUTPUT_TOPIC, new LongDeserializer(), new StringDeserializer());
        final Map<String, TestInputTopic<Long, String>> testInputTopicMap = new HashMap<>();
        testInputTopicMap.put(INPUT_TOPIC_RIGHT, right);
        testInputTopicMap.put(INPUT_TOPIC_LEFT, left);
        TestRecord<Long, String> expectedFinalResult = null;
        final long firstTimestamp = time.milliseconds();
        long eventTimestamp = firstTimestamp;
        final Iterator<List<TestRecord<Long, String>>> resultIterator = expectedResult.iterator();
        for (final Input<String> singleInputRecord : input) {
            testInputTopicMap.get(singleInputRecord.topic).pipeInput(singleInputRecord.record.key, singleInputRecord.record.value, ++eventTimestamp);
            final List<TestRecord<Long, String>> expected = resultIterator.next();
            if (expected != null) {
                final List<TestRecord<Long, String>> updatedExpected = new LinkedList<>();
                for (final TestRecord<Long, String> record : expected) {
                    updatedExpected.add(new TestRecord<>(record.key(), record.value(), null, firstTimestamp + record.timestamp()));
                }
                final List<TestRecord<Long, String>> output = outputTopic.readRecordsToList();
                assertThat(output, equalTo(updatedExpected));
                expectedFinalResult = updatedExpected.get(expected.size() - 1);
            }
        }
        if (storeName != null) {
            checkQueryableStore(storeName, expectedFinalResult, driver);
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LinkedList(java.util.LinkedList) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord)

Example 17 with TestRecord

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

the class AbstractJoinIntegrationTest method runTestWithDriver.

void runTestWithDriver(final TestRecord<Long, String> expectedFinalResult, final String storeName) throws InterruptedException {
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(STREAMS_CONFIG), STREAMS_CONFIG)) {
        final TestInputTopic<Long, String> right = driver.createInputTopic(INPUT_TOPIC_RIGHT, new LongSerializer(), new StringSerializer());
        final TestInputTopic<Long, String> left = driver.createInputTopic(INPUT_TOPIC_LEFT, new LongSerializer(), new StringSerializer());
        final TestOutputTopic<Long, String> outputTopic = driver.createOutputTopic(OUTPUT_TOPIC, new LongDeserializer(), new StringDeserializer());
        final Map<String, TestInputTopic<Long, String>> testInputTopicMap = new HashMap<>();
        testInputTopicMap.put(INPUT_TOPIC_RIGHT, right);
        testInputTopicMap.put(INPUT_TOPIC_LEFT, left);
        final long firstTimestamp = time.milliseconds();
        long eventTimestamp = firstTimestamp;
        for (final Input<String> singleInputRecord : input) {
            testInputTopicMap.get(singleInputRecord.topic).pipeInput(singleInputRecord.record.key, singleInputRecord.record.value, ++eventTimestamp);
        }
        final TestRecord<Long, String> updatedExpectedFinalResult = new TestRecord<>(expectedFinalResult.key(), expectedFinalResult.value(), null, firstTimestamp + expectedFinalResult.timestamp());
        final List<TestRecord<Long, String>> output = outputTopic.readRecordsToList();
        assertThat(output.get(output.size() - 1), equalTo(updatedExpectedFinalResult));
        if (storeName != null) {
            checkQueryableStore(storeName, updatedExpectedFinalResult, driver);
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord)

Example 18 with TestRecord

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

the class TestTopicsTest method testKeyValueListDuration.

@Test
public void testKeyValueListDuration() {
    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 = testBaseTime;
    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, testBaseTime, 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)

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