You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/11/16 00:17:43 UTC

[GitHub] [kafka] mjsax commented on a diff in pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

mjsax commented on code in PR #12861:
URL: https://github.com/apache/kafka/pull/12861#discussion_r1023379924


##########
streams/src/test/java/org/apache/kafka/streams/kstream/internals/KStreamNewProcessorApiTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.streams.kstream.internals;
+
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.TestInputTopic;
+import org.apache.kafka.streams.TopologyTestDriver;
+import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Produced;
+import org.apache.kafka.streams.processor.api.ContextualFixedKeyProcessor;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorContext;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
+import org.apache.kafka.streams.processor.api.FixedKeyRecord;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+
+public class KStreamNewProcessorApiTest {
+
+    @Test
+    @DisplayName("Test for using new Processor API and state stores with the DSL")
+    void shouldGetStateStoreWithNewProcessor() {
+        final StreamsBuilder builder = new StreamsBuilder();
+        final StoreBuilder<?> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("store"), Serdes.String(), Serdes.String());
+
+
+        builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()))
+                .processValues(new TransformerSupplier(storeBuilder), "store")

Review Comment:
   Should we expliclity call `addStateStore` instead of using the `ConnectedStoreProvider` interface (or maybe test both)?



##########
streams/src/test/java/org/apache/kafka/streams/kstream/internals/KStreamNewProcessorApiTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.streams.kstream.internals;
+
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.TestInputTopic;
+import org.apache.kafka.streams.TopologyTestDriver;
+import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Produced;
+import org.apache.kafka.streams.processor.api.ContextualFixedKeyProcessor;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorContext;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
+import org.apache.kafka.streams.processor.api.FixedKeyRecord;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+
+public class KStreamNewProcessorApiTest {
+
+    @Test
+    @DisplayName("Test for using new Processor API and state stores with the DSL")
+    void shouldGetStateStoreWithNewProcessor() {
+        final StreamsBuilder builder = new StreamsBuilder();
+        final StoreBuilder<?> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("store"), Serdes.String(), Serdes.String());
+
+
+        builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()))
+                .processValues(new TransformerSupplier(storeBuilder), "store")
+                .to("output", Produced.with(Serdes.String(), Serdes.String()));
+
+        final List<KeyValue<String, String>> words = Arrays.asList(KeyValue.pair("a", "foo"), KeyValue.pair("b", "bar"), KeyValue.pair("c", "baz"));
+        try (TopologyTestDriver testDriver = new TopologyTestDriver(builder.build())) {
+            final TestInputTopic<String, String>
+                    testDriverInputTopic =
+                    testDriver.createInputTopic("input", Serdes.String().serializer(), Serdes.String().serializer());
+
+            words.forEach(clk -> testDriverInputTopic.pipeInput(clk.key, clk.value));
+
+            final List<String> expectedOutput = asList("fooUpdated", "barUpdated", "bazUpdated");
+
+            final Deserializer<String> keyDeserializer = Serdes.String().deserializer();
+            final List<String> actualOutput =
+                    new ArrayList<>(testDriver.createOutputTopic("output", keyDeserializer, Serdes.String().deserializer()).readValuesToList());
+
+            Assertions.assertEquals(expectedOutput, actualOutput);
+        }
+    }
+    private static class TransformerSupplier implements FixedKeyProcessorSupplier<String, String, String> {
+        private final StoreBuilder<?> storeBuilder;
+
+        public TransformerSupplier(final StoreBuilder<?> storeBuilder) {
+            this.storeBuilder = storeBuilder;
+        }
+
+        @Override
+        public ContextualFixedKeyProcessor<String, String, String> get() {
+            return new ContextualFixedKeyProcessor<String, String, String>() {
+                KeyValueStore<String, String> store;
+                FixedKeyProcessorContext<String, String> context;
+
+                @Override
+                public void init(final FixedKeyProcessorContext<String, String> context) {
+                    super.init(context);
+                    store = context.getStateStore("store");
+                    this.context = context;
+                }
+
+                @Override
+                public void process(final FixedKeyRecord<String, String> record) {
+                    final String updated = store.get(record.key());
+                    store.putIfAbsent(record.key(), record.value() + "Updated");

Review Comment:
   Seems the only test guard is here, ie, if `store == null` we would crash? Should the test code above actually verify the store content in addition to the output topic content?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org