Comments (4)
There's not enough information here for me to give you a good answer. Please provide a minimal test case that reproduces the issue.
from kafka-junit.
Updated the code sample above to include the working test case. When you execute A or B individually, they will pass always. When you run the whole test suite where first A is executed then B, B will always fail.
from kafka-junit.
OK, so this is interesting. First of all, KafkaHelper#consumeStrings
is only good for happy path usage because otherwise the timeout/cancellation of the Future
would fail to properly close the consumer. (I kind of regret adding those helpers but now it's too late to change the API.) Because the consumer is running in a background thread and has not been cleanly shutdown, it is interfering with the subsequent tests. This is because you have pinned the Kafka rule to use a fixed port -- which allows the zombie consumer to reconnect to the Kafka broker spun up for the next test in the suite.
So, the hacky way to make your tests pass is to use a random Kafka port for each test by changing the rule creation to look as follows:
@Rule
public KafkaJunitRule kafkaRule = new KafkaJunitRule(EphemeralKafkaBroker.create());
// If you really need to know he Kafka port, it can be found by calling kafkaRule.helper().kafkaPort()
However, the better way (IMO) to write the tests would be as follows:
public class SomeTest {
private static int kafkaBrokerPort = 9000;
private final String MY_TOPIC = "my-topic";
@Rule
public KafkaJunitRule kafkaRule = new KafkaJunitRule(EphemeralKafkaBroker.create(kafkaBrokerPort)).waitForStartup();
@Test
public void A() {
try (KafkaConsumer<String, String> consumer = kafkaRule.helper().createStringConsumer()) {
consumer.subscribe(Lists.newArrayList(MY_TOPIC));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(10));
assertTrue(records.isEmpty());
}
}
@Test
public void B() throws InterruptedException, TimeoutException, ExecutionException {
KafkaHelper helper = kafkaRule.helper();
helper.produceStrings(MY_TOPIC, "a", "b", "c", "d", "e");
List<String> result = helper.consumeStrings(MY_TOPIC, 5).get(10, TimeUnit.SECONDS);
assertEquals(5, result.size());
}
}
from kafka-junit.
Hi,
Thanks for the answer. It worked nicely when i created a separate consumer for each broker. Probably it will help to change the example in the README.md file to not show consumeStrings.
What are your thoughts on testing 'no message' scenarios ? I searched a lot but seems like not everyone implements them. Yet i wanted to try because they seem relevant in my case. Those waiting times drastically increase the test cases run time, but i see no other good way to implement that.
from kafka-junit.
Related Issues (20)
- Getting error when running unit test HOT 5
- Add support for Kafka version 0.10.2.0 HOT 2
- Failing JUnits on Windows HOT 9
- java.lang.NoSuchFieldError: configFileStr HOT 3
- Does not work with dependency kafka-avro-serializer HOT 6
- JUnit 5 example test fails with NoSuchFieldError: configFileStr HOT 2
- Transactional producer not working HOT 2
- do not support transaction HOT 1
- Update to Kafka 2.1.1 HOT 2
- Kafka 2.3 is out HOT 2
- KafkaHelper threading issue HOT 3
- Kafka 2.4 HOT 3
- Multipartition topic HOT 2
- consumeStrings() discards buffered messages HOT 3
- Support Kafka 2.6
- kafka-junit support for kafka_2.13 (Kafka 2.6.0 recommends using Scala 2.13.x) HOT 3
- Breaking changes from Kafka 2.8.0 HOT 3
- EphemeralKafkaCluster with more than 2 brokers fails with NOT_ENOUGH_REPLICAS error HOT 1
- OWASP Dependency checker reported vulnerabilities in snappy java.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kafka-junit.