Giter Club home page Giter Club logo

Comments (4)

charithe avatar charithe commented on June 26, 2024

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.

jainishan avatar jainishan commented on June 26, 2024

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.

charithe avatar charithe commented on June 26, 2024

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.

jainishan avatar jainishan commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.