vvcephei commented on a change in pull request #9414:
URL: https://github.com/apache/kafka/pull/9414#discussion_r508798891



##########
File path: 
streams/src/test/java/org/apache/kafka/streams/integration/StateDirectoryIntegrationTest.java
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.integration;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KafkaStreams;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.StreamsConfig;
+import org.apache.kafka.streams.Topology;
+import org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster;
+import org.apache.kafka.streams.kstream.Materialized;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.test.IntegrationTest;
+import org.apache.kafka.test.TestUtils;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TestName;
+
+import static org.apache.kafka.common.utils.Utils.mkEntry;
+import static org.apache.kafka.common.utils.Utils.mkMap;
+import static org.apache.kafka.common.utils.Utils.mkProperties;
+import static 
org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@Category({IntegrationTest.class})
+public class StateDirectoryIntegrationTest {
+
+    private static final int NUM_BROKERS = 1;
+
+    @ClassRule
+    public static final EmbeddedKafkaCluster CLUSTER = new 
EmbeddedKafkaCluster(NUM_BROKERS);
+
+    @Rule
+    public TestName testName = new TestName();
+
+    @Test
+    public void testCleanUpStateDirIfEmpty() throws InterruptedException {
+        final String uniqueTestName = safeUniqueTestName(getClass(), testName);
+
+        // Create Topic
+        final String input = uniqueTestName + "-input";
+        CLUSTER.createTopic(input);
+
+        final Properties producerConfig = mkProperties(mkMap(
+            mkEntry(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, 
CLUSTER.bootstrapServers()),
+            mkEntry(ProducerConfig.ACKS_CONFIG, "all"),
+            mkEntry(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, 
StringSerializer.class.getCanonicalName()),
+            mkEntry(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, 
StringSerializer.class.getCanonicalName())
+        ));
+
+        try (final KafkaProducer<String, String> producer =
+                 new KafkaProducer<>(producerConfig, 
Serdes.String().serializer(), Serdes.String().serializer())) {
+            // Create Test Records
+            producer.send(new ProducerRecord<>(input, "a"));
+            producer.send(new ProducerRecord<>(input, "b"));
+            producer.send(new ProducerRecord<>(input, "c"));
+
+            // Create Topology
+            final String storeName = uniqueTestName + "-input-table";
+
+            final StreamsBuilder builder = new StreamsBuilder();
+            builder.table(
+                input,
+                Materialized
+                    .<String, String, KeyValueStore<Bytes, 
byte[]>>as(storeName)
+                    .withKeySerde(Serdes.String())
+                    .withValueSerde(Serdes.String())
+            );
+            final Topology topology = builder.build();
+
+            // State Store Directory
+            final String stateDir = 
TestUtils.tempDirectory(uniqueTestName).getPath();
+
+            // Create KafkaStreams instance
+            final String applicationId = uniqueTestName + "-app";
+            final Properties streamsConfig = mkProperties(mkMap(
+                mkEntry(StreamsConfig.APPLICATION_ID_CONFIG, applicationId),
+                mkEntry(StreamsConfig.STATE_DIR_CONFIG, stateDir),
+                mkEntry(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, 
CLUSTER.bootstrapServers())
+            ));
+
+            final KafkaStreams streams = new KafkaStreams(topology, 
streamsConfig);
+
+            // Create StateListener
+            final CountDownLatch runningLatch = new CountDownLatch(1);
+            final CountDownLatch notRunningLatch = new CountDownLatch(1);
+
+            final KafkaStreams.StateListener stateListener = (newState, 
oldState) -> {
+                if (newState == KafkaStreams.State.RUNNING) {
+                    runningLatch.countDown();
+                }
+                if (newState == KafkaStreams.State.NOT_RUNNING) {
+                    notRunningLatch.countDown();
+                }
+            };
+            streams.setStateListener(stateListener);
+
+            // Application state directory
+            final File appDir = new File(stateDir, applicationId);
+
+            // Validate application state directory is created.
+            streams.start();
+            try {
+                runningLatch.await(10 * 1000L, TimeUnit.MILLISECONDS);

Review comment:
       I'd be tempted to give it more time. Experience says that Jenkins will 
take unreasonably long to perform these operations. How about using the default 
timeout in IntegrationTestUtils here and below?




----------------------------------------------------------------
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.

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


Reply via email to