Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Plain View
Kafka >> mail # user >> Getting the latest message always from Kafka


+
Kishore V. Kopalle 2013-05-17, 14:32
Copy link to this message
-
Re: Getting the latest message always from Kafka
For every unit test, you want your server and consumer to start fresh. In
your unit test, you maintain state in the KafkaManager across unit tests so
your tests don't work as expected. Try moving KafkaManager to the setup
method. Currently, the 2nd test is consuming the message produced by the
1st test

Thanks,
Neha
On Fri, May 17, 2013 at 7:32 AM, Kishore V. Kopalle <
[EMAIL PROTECTED]> wrote:

> Hi Neha,
>
> I am sorry about the design of this program, but what I am trying to do is
> to expose API to read and write from Kafka. I have some unit test cases as
> given below. The problem is that the first test case passes but the second
> test case fails saying that :
> org.junit.ComparisonFailure: expected:<Hi there[123]!> but was:<Hi
> there[]!>
>
> import java.util.Iterator;
>
> import org.junit.Assert;
> import org.junit.BeforeClass;
> import org.junit.Test;
>
> public class KafkaManagerTest
> {
> private static final String TOPIC = "testTopic";
> private static Iterator iter;
> private static KafkaManager kafkaManager = new KafkaManager();
> @BeforeClass
> public static void setupOnce()
> {
> iter = kafkaManager.fetchIterator(TOPIC);
> }
> @Test
> public void testSendAndReceiveMessage1()
> {
>
> String message = "Hi there!";
> kafkaManager.publish(TOPIC, message);
>  String messageFromTopic = null;
> do
> {
> }
> while((messageFromTopic = kafkaManager.fetchNextMessage(TOPIC, iter)) ==
> null);
> Assert.assertEquals(message,messageFromTopic);
> }
>  @Test
> public void testSendAndReceiveMessage2()
> {
> String message = "Hi there123!";
> kafkaManager.publish(TOPIC, message);
>  String messageFromTopic = null;
> do
> {
> }
> while((messageFromTopic = kafkaManager.fetchNextMessage(TOPIC, iter)) ==
> null);
> Assert.assertEquals(message,messageFromTopic);
> }
> }
>
>
> Could you please help me out with why this unit test would fail. I have the
> following line while creating ConsumerConfig:
> props.put("auto.offset.reset", "largest");
>
> Is there something else I need to do to always get the latest message
> always?
>
> Regards,
> Kishore
>