Skip to content

Commit dace7e4

Browse files
rnorthkiviewbsideup
authored
Fix flaky kafka cluster example (#4549)
Co-authored-by: Kevin Wittek <[email protected]> Co-authored-by: Sergei Egorov <[email protected]>
1 parent abf1846 commit dace7e4

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

examples/kafka-cluster/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ dependencies {
1313
testImplementation 'org.apache.kafka:kafka-clients:2.8.0'
1414
testImplementation 'org.assertj:assertj-core:3.20.2'
1515
testImplementation 'com.google.guava:guava:23.0'
16-
testImplementation 'org.slf4j:slf4j-simple:1.7.32'
16+
testImplementation 'ch.qos.logback:logback-classic:1.2.5'
1717
}

examples/kafka-cluster/src/test/java/com/example/kafkacluster/KafkaContainerCluster.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.testcontainers.containers.KafkaContainer;
1111
import org.testcontainers.utility.DockerImageName;
1212

13+
import java.time.Duration;
1314
import java.util.Collection;
1415
import java.util.concurrent.TimeUnit;
1516
import java.util.stream.Collectors;
@@ -56,7 +57,8 @@ public KafkaContainerCluster(String confluentPlatformVersion, int brokersNum, in
5657
.withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", internalTopicsRf + "")
5758
.withEnv("KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS", internalTopicsRf + "")
5859
.withEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", internalTopicsRf + "")
59-
.withEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", internalTopicsRf + "");
60+
.withEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", internalTopicsRf + "")
61+
.withStartupTimeout(Duration.ofMinutes(1));
6062
})
6163
.collect(Collectors.toList());
6264
}
@@ -81,8 +83,8 @@ private Stream<GenericContainer<?>> allContainers() {
8183
@Override
8284
@SneakyThrows
8385
public void start() {
84-
Stream<Startable> startables = this.brokers.stream().map(Startable.class::cast);
85-
Startables.deepStart(startables).get(60, SECONDS);
86+
// sequential start to avoid resource contention on CI systems with weaker hardware
87+
brokers.forEach(GenericContainer::start);
8688

8789
Unreliables.retryUntilTrue(30, TimeUnit.SECONDS, () -> {
8890
Container.ExecResult result = this.zookeeper.execInContainer(

examples/kafka-cluster/src/test/java/com/example/kafkacluster/KafkaContainerClusterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class KafkaContainerClusterTest {
3030
@Test
3131
public void testKafkaContainerCluster() throws Exception {
3232
try (
33-
KafkaContainerCluster cluster = new KafkaContainerCluster("5.2.1", 3, 2)
33+
KafkaContainerCluster cluster = new KafkaContainerCluster("6.2.1", 3, 2)
3434
) {
3535
cluster.start();
3636
String bootstrapServers = cluster.getBootstrapServers();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<!-- encoders are assigned the type
5+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6+
<encoder>
7+
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="STDOUT"/>
13+
</root>
14+
</configuration>

modules/kafka/src/main/java/org/testcontainers/containers/KafkaContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
118118
"advertised.listeners=[" + String.join(",", getBootstrapServers(), brokerAdvertisedListener) + "]"
119119
);
120120
if (result.getExitCode() != 0) {
121-
throw new IllegalStateException(result.getStderr());
121+
throw new IllegalStateException(result.toString());
122122
}
123123
}
124124

0 commit comments

Comments
 (0)