You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2022/03/23 12:39:14 UTC

[GitHub] [camel] essobedo commented on a change in pull request #7254: [CAMEL-17845] Use single Kafka server when running IT

essobedo commented on a change in pull request #7254:
URL: https://github.com/apache/camel/pull/7254#discussion_r833206215



##########
File path: components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/KafkaConsumerManualCommitIT.java
##########
@@ -29,9 +29,11 @@
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.TestInstance;
 
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
+@TestInstance(TestInstance.Lifecycle.PER_METHOD)

Review comment:
       AFAIK it is the default, so it should not be needed

##########
File path: test-infra/camel-test-infra-kafka/src/test/java/org/apache/camel/test/infra/kafka/services/ContainerLocalSingletonKafkaService.java
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.camel.test.infra.kafka.services;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.testcontainers.containers.KafkaContainer;
+import org.testcontainers.utility.DockerImageName;
+
+public class ContainerLocalSingletonKafkaService extends ContainerLocalKafkaService
+        implements ExtensionContext.Store.CloseableResource {
+    private static boolean started;
+
+    public ContainerLocalSingletonKafkaService(KafkaContainer container) {
+        super(container);
+    }
+
+    public ContainerLocalSingletonKafkaService() {
+        super();
+    }
+
+    @Override
+    public void beforeAll(ExtensionContext extensionContext) {
+        if (!started) {
+            started = true;
+            extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).put("kafka", this);
+            super.initialize();
+        }
+    }
+
+    @Override
+    public void afterAll(ExtensionContext extensionContext) {
+        // no op
+    }
+
+    @Override
+    public void close() {
+        super.shutdown();
+    }
+
+    public static ContainerLocalSingletonKafkaService kafka3Container() {
+        KafkaContainer container = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.1"));
+        container = container.withEmbeddedZookeeper();

Review comment:
       ```suggestion
           KafkaContainer container = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.1"))
                   .withEmbeddedZookeeper();
   ```

##########
File path: test-infra/camel-test-infra-kafka/src/test/java/org/apache/camel/test/infra/kafka/services/ContainerLocalSingletonKafkaService.java
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.camel.test.infra.kafka.services;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.testcontainers.containers.KafkaContainer;
+import org.testcontainers.utility.DockerImageName;
+
+public class ContainerLocalSingletonKafkaService extends ContainerLocalKafkaService
+        implements ExtensionContext.Store.CloseableResource {
+    private static boolean started;
+
+    public ContainerLocalSingletonKafkaService(KafkaContainer container) {
+        super(container);
+    }
+
+    public ContainerLocalSingletonKafkaService() {
+        super();
+    }
+
+    @Override
+    public void beforeAll(ExtensionContext extensionContext) {
+        if (!started) {
+            started = true;
+            extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).put("kafka", this);
+            super.initialize();
+        }

Review comment:
       It is not really thread safe, in case of parallel build we could face issues with it. To simplify and make it thread safe, you could use `getOrComputeIfAbsent` instead to initialize it only once, like I did [here](https://github.com/apache/camel/blob/main/components/camel-test/camel-test-main-junit5/src/main/java/org/apache/camel/test/main/junit5/CamelMainExtension.java#L76)




-- 
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: commits-unsubscribe@camel.apache.org

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