Skip to content

Commit 4b437a6

Browse files
authored
fix: address user feedback in Testcontainers tutorial (#5) (#64)
1 parent 851c3dd commit 4b437a6

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

kafka-native-testcontainers/kafka/README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
<!-- title: How to integration test a Kafka application with a native (non-JVM) Kafka binary in Testcontainers -->
2-
<!-- description: In this tutorial, learn how to integration test a Kafka application with a native (non-JVM) Kafka binary in Testcontainers, with step-by-step instructions and supporting code. -->
1+
<!-- title: How to integration test a Kafka application with a native (non-JVM) Kafka binary with Testcontainers -->
2+
<!-- description: In this tutorial, learn how to integration test a Kafka application with a native (non-JVM) Kafka binary with Testcontainers, with step-by-step instructions and supporting code. -->
33

4-
# How to integration test a Kafka application with a native (non-JVM) Kafka binary in Testcontainers
4+
# How to integration test a Kafka application with a native (non-JVM) Kafka binary with Testcontainers
55

66
In this tutorial, we will use the `apache/kafka-native` Docker Image released in Apache Kafka® 3.8 to integration test a basic event routing Kafka consumer / producer application. This [GraalVM](https://www.graalvm.org/)-based image runs a [native binary](https://www.graalvm.org/latest/reference-manual/native-image/) Kafka broker running in KRaft [combined mode](https://kafka.apache.org/documentation/#kraft_role) by default (i.e., it serves as both broker and KRaft controller). As a native binary executable, it offers the following test scenario benefits compared to the JVM-based `apache/kafka` image:
77

88
1. Smaller image size (faster download time)
99
2. Faster startup time
1010
3. Lower memory usage
1111

12-
Given these benefits, this image is well-suited for non-production development and testing scenarios that require an actual Kafka broker. [Testcontainers](https://java.testcontainers.org/modules/kafka/) supports this image as of version `1.20.1` of `org.testcontainers`'s `kafka` artifact.
12+
Given these benefits, this image is well-suited for non-production development and testing scenarios that require an actual Kafka broker. [Testcontainers](https://java.testcontainers.org/modules/kafka/) supports this image as of version `1.20.1` of `org.testcontainers`'s `kafka` module.
1313

1414
Testing in this way is as easy as declaring the [Testcontainers Kafka dependency](https://mvnrepository.com/artifact/org.testcontainers/kafka/1.20.1) in your dependency manager and then writing a test like this:
1515

1616
```java
17-
try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("apache/kafka-native:<VERSION>>"))) {
17+
try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("apache/kafka-native:<VERSION>"))) {
1818
kafka.start();
1919

2020
// Instantiate and start your application with kafka.getBootstrapServers() as your bootstrap servers endpoint
@@ -30,8 +30,6 @@ Testing in this way is as easy as declaring the [Testcontainers Kafka dependency
3030
// properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
3131

3232
// Assert that collected outputs are as expected!
33-
34-
kafka.stop();
3533
}
3634
```
3735
@@ -119,17 +117,24 @@ docker rmi apache/kafka-native:3.8.0
119117
docker rmi apache/kafka:3.8.0
120118
```
121119
122-
Now run the test twice, once using the `apache/kafka` image and again using the `apache/kafka-native` image. In test
123-
runs from a laptop with 400 Mbps download speed, the `apache/kafka-native`-based test took 7.3 seconds while the
124-
`apache/kafka`-based test took 16 seconds. Once the images were downloaded, the `apache/kafka-native`-based test
125-
took 4.1 seconds while the `apache/kafka`-based test took 7.2 seconds. Note that these integration tests are on the slow
126-
and heavyweight side, e.g., they take time to validate that no more events show up after the expected events show up, a
127-
condition which requires waiting 200ms in the test:
120+
Next, run the test twice, once using the `apache/kafka` image, and again using the `apache/kafka-native` image. Test
121+
runs from a laptop with 300 Mbps download speed resulted in the following performance numbers:
122+
123+
| Image | Image Download Time | Container Startup Time | Test Execution Time | Total Time |
124+
|:----------------------|--------------------:|-----------------------:|--------------------:|-----------:|
125+
| apache/kafka | 6 sec | 3.5 sec | 5 sec | 14.5 sec |
126+
| apache/kafka-native | 1.5 sec | 0.6 sec | 4.5 sec | 6.6 sec |
127+
128+
129+
Note that the image download time only applies the first time that you run the test, i.e., a subsequent test
130+
run on the same laptop takes about 8.5 seconds using the `apache/kafka` image and 5.1 seconds using the `apache/kafka-native` image.
131+
Also, keep in mind that this integration test is on the slow and heavyweight side, e.g., it takes time to validate that no more
132+
events show up after the expected events show up, a condition that requires waiting 200 ms in the test:
128133
129134
```java
130135
// make sure no more events show up in prime / composite topics
131136
assertEquals(0, consumer.poll(Duration.ofMillis(200)).count());
132137
```
133138
134-
So, a more streamlined test that doesn't wait for unexpected events would run much more quickly, making the shorter
135-
download and Kafka startup times that much more valuable for development and automated testing.
139+
So, a more streamlined test that doesn't wait for unexpected events would run much more quickly, making the shorter image
140+
download and container startup times that much more valuable for development and automated testing.

0 commit comments

Comments
 (0)