Search in sources :

Example 41 with MockApiProcessorSupplier

use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.

the class SlidingWindowedKStreamImplTest method shouldCountSlidingWindows.

@Test
public void shouldCountSlidingWindows() {
    final MockApiProcessorSupplier<Windowed<String>, Long, Void, Void> supplier = new MockApiProcessorSupplier<>();
    windowedStream.count().toStream().process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        processData(driver);
    }
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(0L, 100L))), equalTo(ValueAndTimestamp.make(1L, 100L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(101L, 201L))), equalTo(ValueAndTimestamp.make(1L, 150L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(50L, 150L))), equalTo(ValueAndTimestamp.make(2L, 150L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(400L, 500L))), equalTo(ValueAndTimestamp.make(1L, 500L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(100L, 200L))), equalTo(ValueAndTimestamp.make(2L, 200L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(50L, 150L))), equalTo(ValueAndTimestamp.make(1L, 150L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(151L, 251L))), equalTo(ValueAndTimestamp.make(1L, 200L)));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Test(org.junit.Test)

Example 42 with MockApiProcessorSupplier

use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.

the class SlidingWindowedKStreamImplTest method shouldAggregateSlidingWindows.

@Test
public void shouldAggregateSlidingWindows() {
    final MockApiProcessorSupplier<Windowed<String>, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
    windowedStream.aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, Materialized.with(Serdes.String(), Serdes.String())).toStream().process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        processData(driver);
    }
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(0L, 100L))), equalTo(ValueAndTimestamp.make("0+1", 100L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(101L, 201L))), equalTo(ValueAndTimestamp.make("0+2", 150L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(50L, 150L))), equalTo(ValueAndTimestamp.make("0+1+2", 150L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(400L, 500L))), equalTo(ValueAndTimestamp.make("0+3", 500L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(100L, 200L))), equalTo(ValueAndTimestamp.make("0+10+20", 200L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(50L, 150L))), equalTo(ValueAndTimestamp.make("0+20", 150L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(151L, 251L))), equalTo(ValueAndTimestamp.make("0+10", 200L)));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Test(org.junit.Test)

Example 43 with MockApiProcessorSupplier

use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.

the class TimeWindowedKStreamImplTest method shouldAggregateWindowed.

@Test
public void shouldAggregateWindowed() {
    final MockApiProcessorSupplier<Windowed<String>, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
    windowedStream.aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, Materialized.with(Serdes.String(), Serdes.String())).toStream().process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        processData(driver);
    }
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(0L, 500L))), equalTo(ValueAndTimestamp.make("0+1+2", 15L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(500L, 1000L))), equalTo(ValueAndTimestamp.make("0+10+20", 550L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(500L, 1000L))), equalTo(ValueAndTimestamp.make("0+3", 500L)));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Test(org.junit.Test)

Example 44 with MockApiProcessorSupplier

use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.

the class TimeWindowedKStreamImplTest method shouldCountWindowed.

@Test
public void shouldCountWindowed() {
    final MockApiProcessorSupplier<Windowed<String>, Long, Void, Void> supplier = new MockApiProcessorSupplier<>();
    windowedStream.count().toStream().process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        processData(driver);
    }
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(0L, 500L))), equalTo(ValueAndTimestamp.make(2L, 15L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("2", new TimeWindow(500L, 1000L))), equalTo(ValueAndTimestamp.make(2L, 550L)));
    assertThat(supplier.theCapturedProcessor().lastValueAndTimestampPerKey().get(new Windowed<>("1", new TimeWindow(500L, 1000L))), equalTo(ValueAndTimestamp.make(1L, 500L)));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Test(org.junit.Test)

Example 45 with MockApiProcessorSupplier

use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.

the class KStreamSlidingWindowAggregateTest method testEarlyRecordsSmallInput.

@Test
public void testEarlyRecordsSmallInput() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String topic = "topic";
    final KTable<Windowed<String>, String> table2 = builder.stream(topic, Consumed.with(Serdes.String(), Serdes.String())).groupByKey(Grouped.with(Serdes.String(), Serdes.String())).windowedBy(SlidingWindows.ofTimeDifferenceAndGrace(ofMillis(50), ofMillis(200))).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, Materialized.<String, String, WindowStore<Bytes, byte[]>>as("topic-Canonized").withValueSerde(Serdes.String()));
    final MockApiProcessorSupplier<Windowed<String>, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
    table2.toStream().process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> inputTopic = driver.createInputTopic(topic, new StringSerializer(), new StringSerializer());
        inputTopic.pipeInput("A", "1", 0L);
        inputTopic.pipeInput("A", "2", 5L);
        inputTopic.pipeInput("A", "3", 6L);
        inputTopic.pipeInput("A", "4", 3L);
        inputTopic.pipeInput("A", "5", 13L);
        inputTopic.pipeInput("A", "6", 10L);
    }
    final Map<Long, ValueAndTimestamp<String>> actual = new HashMap<>();
    for (final KeyValueTimestamp<Windowed<String>, String> entry : supplier.theCapturedProcessor().processed()) {
        final Windowed<String> window = entry.key();
        final Long start = window.window().start();
        final ValueAndTimestamp<String> valueAndTimestamp = ValueAndTimestamp.make(entry.value(), entry.timestamp());
        if (actual.putIfAbsent(start, valueAndTimestamp) != null) {
            actual.replace(start, valueAndTimestamp);
        }
    }
    final Map<Long, ValueAndTimestamp<String>> expected = new HashMap<>();
    expected.put(0L, ValueAndTimestamp.make("0+1+2+3+4+5+6", 13L));
    expected.put(1L, ValueAndTimestamp.make("0+2+3+4+5+6", 13L));
    expected.put(4L, ValueAndTimestamp.make("0+2+3+5+6", 13L));
    expected.put(6L, ValueAndTimestamp.make("0+3+5+6", 13L));
    expected.put(7L, ValueAndTimestamp.make("0+5+6", 13L));
    expected.put(11L, ValueAndTimestamp.make("0+5", 13L));
    assertEquals(expected, actual);
}
Also used : MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) HashMap(java.util.HashMap) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Windowed(org.apache.kafka.streams.kstream.Windowed) InMemoryWindowStore(org.apache.kafka.streams.state.internals.InMemoryWindowStore) WindowStore(org.apache.kafka.streams.state.WindowStore) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Aggregations

MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)90 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)79 Test (org.junit.Test)78 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)68 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)64 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)41 Windowed (org.apache.kafka.streams.kstream.Windowed)19 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)14 Properties (java.util.Properties)13 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)12 HashSet (java.util.HashSet)11 Set (java.util.Set)11 Serdes (org.apache.kafka.common.serialization.Serdes)10 TestInputTopic (org.apache.kafka.streams.TestInputTopic)10 Consumed (org.apache.kafka.streams.kstream.Consumed)10 MockApiProcessor (org.apache.kafka.test.MockApiProcessor)10 StreamsTestUtils (org.apache.kafka.test.StreamsTestUtils)9 Topology (org.apache.kafka.streams.Topology)8 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)8 Assert.assertEquals (org.junit.Assert.assertEquals)8