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)));
}
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)));
}
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)));
}
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)));
}
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);
}
Aggregations