use of org.apache.kafka.streams.TopologyTestDriverWrapper in project kafka by apache.
the class KTableKTableLeftJoinTest method testSendingOldValue.
@Test
public void testSendingOldValue() {
final StreamsBuilder builder = new StreamsBuilder();
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
final KTable<Integer, String> table1;
final KTable<Integer, String> table2;
final KTable<Integer, String> joined;
final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
table1 = builder.table(topic1, consumed);
table2 = builder.table(topic2, consumed);
joined = table1.leftJoin(table2, MockValueJoiner.TOSTRING_JOINER);
((KTableImpl<?, ?, ?>) joined).enableSendingOldValues(true);
assertThat(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled(), is(true));
assertThat(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled(), is(true));
assertThat(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled(), is(true));
final Topology topology = builder.build().addProcessor("proc", supplier, ((KTableImpl<?, ?, ?>) joined).name);
try (final TopologyTestDriver driver = new TopologyTestDriverWrapper(topology, props)) {
final TestInputTopic<Integer, String> inputTopic1 = driver.createInputTopic(topic1, Serdes.Integer().serializer(), Serdes.String().serializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
final TestInputTopic<Integer, String> inputTopic2 = driver.createInputTopic(topic2, Serdes.Integer().serializer(), Serdes.String().serializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
final MockApiProcessor<Integer, String, Void, Void> proc = supplier.theCapturedProcessor();
assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
// push two items to the primary stream. the other table is empty
for (int i = 0; i < 2; i++) {
inputTopic1.pipeInput(expectedKeys[i], "X" + expectedKeys[i], 5L + i);
}
// pass tuple with null key, it will be discarded in join process
inputTopic1.pipeInput(null, "SomeVal", 42L);
// left: X0:0 (ts: 5), X1:1 (ts: 6)
// right:
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("X0+null", null), 5), new KeyValueTimestamp<>(1, new Change<>("X1+null", null), 6));
// push two items to the other stream. this should produce two items.
for (int i = 0; i < 2; i++) {
inputTopic2.pipeInput(expectedKeys[i], "Y" + expectedKeys[i], 10L * i);
}
// pass tuple with null key, it will be discarded in join process
inputTopic2.pipeInput(null, "AnotherVal", 73L);
// left: X0:0 (ts: 5), X1:1 (ts: 6)
// right: Y0:0 (ts: 0), Y1:1 (ts: 10)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("X0+Y0", "X0+null"), 5), new KeyValueTimestamp<>(1, new Change<>("X1+Y1", "X1+null"), 10));
// push all four items to the primary stream. this should produce four items.
for (final int expectedKey : expectedKeys) {
inputTopic1.pipeInput(expectedKey, "XX" + expectedKey, 7L);
}
// left: XX0:0 (ts: 7), XX1:1 (ts: 7), XX2:2 (ts: 7), XX3:3 (ts: 7)
// right: Y0:0 (ts: 0), Y1:1 (ts: 10)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("XX0+Y0", "X0+Y0"), 7), new KeyValueTimestamp<>(1, new Change<>("XX1+Y1", "X1+Y1"), 10), new KeyValueTimestamp<>(2, new Change<>("XX2+null", null), 7), new KeyValueTimestamp<>(3, new Change<>("XX3+null", null), 7));
// push all items to the other stream. this should produce four items.
for (final int expectedKey : expectedKeys) {
inputTopic2.pipeInput(expectedKey, "YY" + expectedKey, expectedKey * 5L);
}
// left: XX0:0 (ts: 7), XX1:1 (ts: 7), XX2:2 (ts: 7), XX3:3 (ts: 7)
// right: YY0:0 (ts: 0), YY1:1 (ts: 5), YY2:2 (ts: 10), YY3:3 (ts: 15)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("XX0+YY0", "XX0+Y0"), 7), new KeyValueTimestamp<>(1, new Change<>("XX1+YY1", "XX1+Y1"), 7), new KeyValueTimestamp<>(2, new Change<>("XX2+YY2", "XX2+null"), 10), new KeyValueTimestamp<>(3, new Change<>("XX3+YY3", "XX3+null"), 15));
// push all four items to the primary stream. this should produce four items.
for (final int expectedKey : expectedKeys) {
inputTopic1.pipeInput(expectedKey, "XXX" + expectedKey, 6L);
}
// left: XXX0:0 (ts: 6), XXX1:1 (ts: 6), XXX2:2 (ts: 6), XXX3:3 (ts: 6)
// right: YY0:0 (ts: 0), YY1:1 (ts: 5), YY2:2 (ts: 10), YY3:3 (ts: 15)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("XXX0+YY0", "XX0+YY0"), 6), new KeyValueTimestamp<>(1, new Change<>("XXX1+YY1", "XX1+YY1"), 6), new KeyValueTimestamp<>(2, new Change<>("XXX2+YY2", "XX2+YY2"), 10), new KeyValueTimestamp<>(3, new Change<>("XXX3+YY3", "XX3+YY3"), 15));
// push two items with null to the other stream as deletes. this should produce two item.
inputTopic2.pipeInput(expectedKeys[0], null, 5L);
inputTopic2.pipeInput(expectedKeys[1], null, 7L);
// left: XXX0:0 (ts: 6), XXX1:1 (ts: 6), XXX2:2 (ts: 6), XXX3:3 (ts: 6)
// right: YY2:2 (ts: 10), YY3:3 (ts: 15)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("XXX0+null", "XXX0+YY0"), 6), new KeyValueTimestamp<>(1, new Change<>("XXX1+null", "XXX1+YY1"), 7));
// push all four items to the primary stream. this should produce four items.
for (final int expectedKey : expectedKeys) {
inputTopic1.pipeInput(expectedKey, "XXXX" + expectedKey, 13L);
}
// left: XXXX0:0 (ts: 13), XXXX1:1 (ts: 13), XXXX2:2 (ts: 13), XXXX3:3 (ts: 13)
// right: YY2:2 (ts: 10), YY3:3 (ts: 15)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>("XXXX0+null", "XXX0+null"), 13), new KeyValueTimestamp<>(1, new Change<>("XXXX1+null", "XXX1+null"), 13), new KeyValueTimestamp<>(2, new Change<>("XXXX2+YY2", "XXX2+YY2"), 13), new KeyValueTimestamp<>(3, new Change<>("XXXX3+YY3", "XXX3+YY3"), 15));
// push four items to the primary stream with null. this should produce four items.
inputTopic1.pipeInput(expectedKeys[0], null, 0L);
inputTopic1.pipeInput(expectedKeys[1], null, 42L);
inputTopic1.pipeInput(expectedKeys[2], null, 5L);
inputTopic1.pipeInput(expectedKeys[3], null, 20L);
// left:
// right: YY2:2 (ts: 10), YY3:3 (ts: 15)
proc.checkAndClearProcessResult(new KeyValueTimestamp<>(0, new Change<>(null, "XXXX0+null"), 0), new KeyValueTimestamp<>(1, new Change<>(null, "XXXX1+null"), 42), new KeyValueTimestamp<>(2, new Change<>(null, "XXXX2+YY2"), 10), new KeyValueTimestamp<>(3, new Change<>(null, "XXXX3+YY3"), 20));
}
}
use of org.apache.kafka.streams.TopologyTestDriverWrapper in project kafka by apache.
the class KTableMapValuesTest method doTestValueGetter.
private void doTestValueGetter(final StreamsBuilder builder, final String topic1, final KTableImpl<String, String, Integer> table2, final KTableImpl<String, String, Integer> table3) {
final Topology topology = builder.build();
final KTableValueGetterSupplier<String, Integer> getterSupplier2 = table2.valueGetterSupplier();
final KTableValueGetterSupplier<String, Integer> getterSupplier3 = table3.valueGetterSupplier();
final InternalTopologyBuilder topologyBuilder = TopologyWrapper.getInternalTopologyBuilder(topology);
topologyBuilder.connectProcessorAndStateStores(table2.name, getterSupplier2.storeNames());
topologyBuilder.connectProcessorAndStateStores(table3.name, getterSupplier3.storeNames());
try (final TopologyTestDriverWrapper driver = new TopologyTestDriverWrapper(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic1 = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
final KTableValueGetter<String, Integer> getter2 = getterSupplier2.get();
final KTableValueGetter<String, Integer> getter3 = getterSupplier3.get();
getter2.init(driver.setCurrentNodeForProcessorContext(table2.name));
getter3.init(driver.setCurrentNodeForProcessorContext(table3.name));
inputTopic1.pipeInput("A", "01", 50L);
inputTopic1.pipeInput("B", "01", 10L);
inputTopic1.pipeInput("C", "01", 30L);
assertEquals(ValueAndTimestamp.make(1, 50L), getter2.get("A"));
assertEquals(ValueAndTimestamp.make(1, 10L), getter2.get("B"));
assertEquals(ValueAndTimestamp.make(1, 30L), getter2.get("C"));
assertEquals(ValueAndTimestamp.make(-1, 50L), getter3.get("A"));
assertEquals(ValueAndTimestamp.make(-1, 10L), getter3.get("B"));
assertEquals(ValueAndTimestamp.make(-1, 30L), getter3.get("C"));
inputTopic1.pipeInput("A", "02", 25L);
inputTopic1.pipeInput("B", "02", 20L);
assertEquals(ValueAndTimestamp.make(2, 25L), getter2.get("A"));
assertEquals(ValueAndTimestamp.make(2, 20L), getter2.get("B"));
assertEquals(ValueAndTimestamp.make(1, 30L), getter2.get("C"));
assertEquals(ValueAndTimestamp.make(-2, 25L), getter3.get("A"));
assertEquals(ValueAndTimestamp.make(-2, 20L), getter3.get("B"));
assertEquals(ValueAndTimestamp.make(-1, 30L), getter3.get("C"));
inputTopic1.pipeInput("A", "03", 35L);
assertEquals(ValueAndTimestamp.make(3, 35L), getter2.get("A"));
assertEquals(ValueAndTimestamp.make(2, 20L), getter2.get("B"));
assertEquals(ValueAndTimestamp.make(1, 30L), getter2.get("C"));
assertEquals(ValueAndTimestamp.make(-3, 35L), getter3.get("A"));
assertEquals(ValueAndTimestamp.make(-2, 20L), getter3.get("B"));
assertEquals(ValueAndTimestamp.make(-1, 30L), getter3.get("C"));
inputTopic1.pipeInput("A", (String) null, 1L);
assertNull(getter2.get("A"));
assertEquals(ValueAndTimestamp.make(2, 20L), getter2.get("B"));
assertEquals(ValueAndTimestamp.make(1, 30L), getter2.get("C"));
assertNull(getter3.get("A"));
assertEquals(ValueAndTimestamp.make(-2, 20L), getter3.get("B"));
assertEquals(ValueAndTimestamp.make(-1, 30L), getter3.get("C"));
}
}
use of org.apache.kafka.streams.TopologyTestDriverWrapper in project kafka by apache.
the class KTableImplTest method shouldCreateSourceAndSinkNodesForRepartitioningTopic.
@Test
public void shouldCreateSourceAndSinkNodesForRepartitioningTopic() throws Exception {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final String storeName1 = "storeName1";
final KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed, Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(storeName1).withKeySerde(Serdes.String()).withValueSerde(Serdes.String()));
table1.groupBy(MockMapper.noOpKeyValueMapper()).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, MockAggregator.TOSTRING_REMOVER, Materialized.as("mock-result1"));
table1.groupBy(MockMapper.noOpKeyValueMapper()).reduce(MockReducer.STRING_ADDER, MockReducer.STRING_REMOVER, Materialized.as("mock-result2"));
final Topology topology = builder.build();
try (final TopologyTestDriverWrapper driver = new TopologyTestDriverWrapper(topology, props)) {
assertEquals(3, driver.getAllStateStores().size());
assertTopologyContainsProcessor(topology, "KSTREAM-SINK-0000000003");
assertTopologyContainsProcessor(topology, "KSTREAM-SOURCE-0000000004");
assertTopologyContainsProcessor(topology, "KSTREAM-SINK-0000000007");
assertTopologyContainsProcessor(topology, "KSTREAM-SOURCE-0000000008");
final Field valSerializerField = ((SinkNode) driver.getProcessor("KSTREAM-SINK-0000000003")).getClass().getDeclaredField("valSerializer");
final Field valDeserializerField = ((SourceNode) driver.getProcessor("KSTREAM-SOURCE-0000000004")).getClass().getDeclaredField("valDeserializer");
valSerializerField.setAccessible(true);
valDeserializerField.setAccessible(true);
assertNotNull(((ChangedSerializer) valSerializerField.get(driver.getProcessor("KSTREAM-SINK-0000000003"))).inner());
assertNotNull(((ChangedDeserializer) valDeserializerField.get(driver.getProcessor("KSTREAM-SOURCE-0000000004"))).inner());
assertNotNull(((ChangedSerializer) valSerializerField.get(driver.getProcessor("KSTREAM-SINK-0000000007"))).inner());
assertNotNull(((ChangedDeserializer) valDeserializerField.get(driver.getProcessor("KSTREAM-SOURCE-0000000008"))).inner());
}
}
use of org.apache.kafka.streams.TopologyTestDriverWrapper in project kafka by apache.
the class KTableSourceTest method testValueGetter.
@Test
public void testValueGetter() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
@SuppressWarnings("unchecked") final KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, stringConsumed, Materialized.as("store"));
final Topology topology = builder.build();
final KTableValueGetterSupplier<String, String> getterSupplier1 = table1.valueGetterSupplier();
final InternalTopologyBuilder topologyBuilder = TopologyWrapper.getInternalTopologyBuilder(topology);
topologyBuilder.connectProcessorAndStateStores(table1.name, getterSupplier1.storeNames());
try (final TopologyTestDriverWrapper driver = new TopologyTestDriverWrapper(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic1 = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
final KTableValueGetter<String, String> getter1 = getterSupplier1.get();
getter1.init(driver.setCurrentNodeForProcessorContext(table1.name));
inputTopic1.pipeInput("A", "01", 10L);
inputTopic1.pipeInput("B", "01", 20L);
inputTopic1.pipeInput("C", "01", 15L);
assertEquals(ValueAndTimestamp.make("01", 10L), getter1.get("A"));
assertEquals(ValueAndTimestamp.make("01", 20L), getter1.get("B"));
assertEquals(ValueAndTimestamp.make("01", 15L), getter1.get("C"));
inputTopic1.pipeInput("A", "02", 30L);
inputTopic1.pipeInput("B", "02", 5L);
assertEquals(ValueAndTimestamp.make("02", 30L), getter1.get("A"));
assertEquals(ValueAndTimestamp.make("02", 5L), getter1.get("B"));
assertEquals(ValueAndTimestamp.make("01", 15L), getter1.get("C"));
inputTopic1.pipeInput("A", "03", 29L);
assertEquals(ValueAndTimestamp.make("03", 29L), getter1.get("A"));
assertEquals(ValueAndTimestamp.make("02", 5L), getter1.get("B"));
assertEquals(ValueAndTimestamp.make("01", 15L), getter1.get("C"));
inputTopic1.pipeInput("A", null, 50L);
inputTopic1.pipeInput("B", null, 3L);
assertNull(getter1.get("A"));
assertNull(getter1.get("B"));
assertEquals(ValueAndTimestamp.make("01", 15L), getter1.get("C"));
}
}
use of org.apache.kafka.streams.TopologyTestDriverWrapper in project kafka by apache.
the class KTableFilterTest method doTestValueGetter.
private void doTestValueGetter(final StreamsBuilder builder, final KTableImpl<String, Integer, Integer> table2, final KTableImpl<String, Integer, Integer> table3, final String topic1) {
final Topology topology = builder.build();
final KTableValueGetterSupplier<String, Integer> getterSupplier2 = table2.valueGetterSupplier();
final KTableValueGetterSupplier<String, Integer> getterSupplier3 = table3.valueGetterSupplier();
final InternalTopologyBuilder topologyBuilder = TopologyWrapper.getInternalTopologyBuilder(topology);
topologyBuilder.connectProcessorAndStateStores(table2.name, getterSupplier2.storeNames());
topologyBuilder.connectProcessorAndStateStores(table3.name, getterSupplier3.storeNames());
try (final TopologyTestDriverWrapper driver = new TopologyTestDriverWrapper(topology, props)) {
final TestInputTopic<String, Integer> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new IntegerSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
final KTableValueGetter<String, Integer> getter2 = getterSupplier2.get();
final KTableValueGetter<String, Integer> getter3 = getterSupplier3.get();
getter2.init(driver.setCurrentNodeForProcessorContext(table2.name));
getter3.init(driver.setCurrentNodeForProcessorContext(table3.name));
inputTopic.pipeInput("A", 1, 5L);
inputTopic.pipeInput("B", 1, 10L);
inputTopic.pipeInput("C", 1, 15L);
assertNull(getter2.get("A"));
assertNull(getter2.get("B"));
assertNull(getter2.get("C"));
assertEquals(ValueAndTimestamp.make(1, 5L), getter3.get("A"));
assertEquals(ValueAndTimestamp.make(1, 10L), getter3.get("B"));
assertEquals(ValueAndTimestamp.make(1, 15L), getter3.get("C"));
inputTopic.pipeInput("A", 2, 10L);
inputTopic.pipeInput("B", 2, 5L);
assertEquals(ValueAndTimestamp.make(2, 10L), getter2.get("A"));
assertEquals(ValueAndTimestamp.make(2, 5L), getter2.get("B"));
assertNull(getter2.get("C"));
assertNull(getter3.get("A"));
assertNull(getter3.get("B"));
assertEquals(ValueAndTimestamp.make(1, 15L), getter3.get("C"));
inputTopic.pipeInput("A", 3, 15L);
assertNull(getter2.get("A"));
assertEquals(ValueAndTimestamp.make(2, 5L), getter2.get("B"));
assertNull(getter2.get("C"));
assertEquals(ValueAndTimestamp.make(3, 15L), getter3.get("A"));
assertNull(getter3.get("B"));
assertEquals(ValueAndTimestamp.make(1, 15L), getter3.get("C"));
inputTopic.pipeInput("A", null, 10L);
inputTopic.pipeInput("B", null, 20L);
assertNull(getter2.get("A"));
assertNull(getter2.get("B"));
assertNull(getter2.get("C"));
assertNull(getter3.get("A"));
assertNull(getter3.get("B"));
assertEquals(ValueAndTimestamp.make(1, 15L), getter3.get("C"));
}
}
Aggregations