GithubHelp home page GithubHelp logo

Comments (9)

alastairparagas avatar alastairparagas commented on June 21, 2024

On another instance where I've debugged around this problem, I get:
java.lang.IllegalStateException: KafkaBroker is not running

from kafka-junit.

charithe avatar charithe commented on June 21, 2024

ClassRules must be static. That's probably the reason why it's not getting started up.

from kafka-junit.

alastairparagas avatar alastairparagas commented on June 21, 2024

Cool. I revised my code as below and it worked:

package com.termmerge.nlpcore.obtainer;

import junit.framework.TestCase;
import org.junit.Test;
import com.github.charithe.kafka.KafkaJunitRule;
import com.github.charithe.kafka.EphemeralKafkaBroker;
import net.jodah.concurrentunit.Waiter;

import java.util.Map;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;


public class KafkaStreamObtainerTest extends TestCase
{

  @Test
  public void testOneMessage() throws Throwable
  {
    Waiter waiter = new Waiter();
    EphemeralKafkaBroker kafkaBroker =
            EphemeralKafkaBroker.create(8000, 8001);
    kafkaBroker.start().get();

    KafkaJunitRule kafkaRule = new KafkaJunitRule(kafkaBroker);
    KafkaProducer testProducer = kafkaRule.helper().createStringProducer();
    testProducer.send(
            new ProducerRecord<>("testTopic", "testKey", "testValue")
    );
    testProducer.close();

    Map consumerSettings = new Properties();
    consumerSettings.put(
            "connection_string",
            "localhost:" + Integer.toString(kafkaRule.helper().kafkaPort())
    );
    consumerSettings.put("group_id", "test");
    KafkaStreamObtainer kafkaStream =
            new KafkaStreamObtainer(consumerSettings);
    kafkaStream.addListener((record) -> {
      waiter.assertEquals(record.get("key"), "testKey");
      waiter.assertEquals(record.get("value"), "testValue");
      waiter.resume();
    });
    kafkaStream.listenToStream("testTopic");

    waiter.await(50000);
  }

}

Now, however, I have a problem with my Kafka consumer hanging up at the poll() stage where it is not returning at all (after calling poll). Can we use the regular Kafka API client to interface with this?

from kafka-junit.

charithe avatar charithe commented on June 21, 2024

The rule starts a standard Kafka broker that can be accessed using any Kafka client.

Your revised test is not quite correct for a couple of reasons. Firstly, you are calling get on a future at the start of the test. It would just block there and not execute the rest of the code. Secondly, you're mixing JUnit3 style with JUnit4 style. I recommend switching completely to the JUnit4 style as that's the version that introduced rules and what this project has been tested with.

from kafka-junit.

alastairparagas avatar alastairparagas commented on June 21, 2024

@charithe I know I was doing a blocking call with get() for debugging purposes. I specifically wanted it to block so that I know full well that the broker has started before I even mount any producers or consumers on it. I'm using IntelliJ and I know that using the debugger, I am getting passed the blocking call without a problem. My problem lies in the fact that it takes so long for a Mock Producer to send a data and my consumer to poll for such data (even though my poll interval is only 100ms).

from kafka-junit.

alastairparagas avatar alastairparagas commented on June 21, 2024

The error I get is:

[kafka-producer-network-thread | kafka-junit] WARN org.apache.kafka.clients.NetworkClient - Error while fetching metadata with correlation id 2 : {testTopic=UNKNOWN}
[kafka-request-handler-3] ERROR kafka.server.KafkaApis - [KafkaApi-1] Error when handling request {topics=[testTopic]}

from kafka-junit.

alastairparagas avatar alastairparagas commented on June 21, 2024

Just confirmed that using a normal Apache Kafka Docker container, that I am able to pass my unit tests, but using the EphemeralKafkaBroker, my tests seem to fail. The image below shows that my unit tests pass using Apache Kafka straight on.

screen shot 2016-12-12 at 2 19 41 pm

For reference, I am using the Apache Kafka Docker container at https://hub.docker.com/r/wurstmeister/kafka/

from kafka-junit.

charithe avatar charithe commented on June 21, 2024

I am not sure how you could move past the Future.get() because that means the broker had stopped.

Anyway, I noticed in your screenshot that you're using Kafka 0.10.1.0. The JUnit rule is still on 0.10.0.1. The problem could be due to the library version mismatch.

This is an example of how to use the class rule: https://github.com/charithe/kafka-junit/blob/master/src/test/java/com/github/charithe/kafka/KafkaJunitClassRuleTest.java. I hope that helps.

from kafka-junit.

alastairparagas avatar alastairparagas commented on June 21, 2024

Got it running. The fix was to set my Kafka consumer's "auto.offset.reset" to "earliest" policy.

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.