Search in sources :

Example 6 with KafkaConsumer

use of com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testImplicitTopicProducer.

@Test
public void testImplicitTopicProducer() throws Exception {
    checkAssumes();
    Topology top = new Topology("testImplicitTopicProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String groupId = newGroupId(top.getName());
    String topicVal = getKafkaTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    KafkaProducer producer = new KafkaProducer(top, createProducerConfig());
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig(groupId));
    // Test producer that takes an arbitrary TStream<T> and implicit topic
    List<Vals> msgs = new ArrayList<>();
    msgs.add(new Vals(mgen.create(topicVal, "Hello"), null, null));
    msgs.add(new Vals(mgen.create(topicVal, "key1", "Are you there?"), "key1", null));
    TStream<Vals> valsToPublish = top.constants(msgs).asType(Vals.class);
    TStream<Message> msgsToPublish = valsToPublish.transform(msgFromValsFunc(topicVal));
    msgsToPublish = msgsToPublish.modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    producer.publish(msgsToPublish);
    TStream<Message> rcvdMsgs = consumer.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    List<Message> expectedAsMessage = mapList(msgs, msgFromValsFunc(topicVal));
    List<String> expectedAsString = mapList(expectedAsMessage, msgToJSONStringFunc());
    setupDebug();
    if (testBuildOnly(top))
        return;
    completeAndValidate(groupId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
}
Also used : KafkaProducer(com.ibm.streamsx.topology.messaging.kafka.KafkaProducer) InitialDelay(com.ibm.streamsx.topology.test.InitialDelay) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) ArrayList(java.util.ArrayList) KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Example 7 with KafkaConsumer

use of com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testMultiTopicProducer.

@Test
public void testMultiTopicProducer() throws Exception {
    checkAssumes();
    // streamsx.messaging issue#118 prevents successful execution
    // For standalone it seems to consistently get 0 topic1 msgs.
    assumeTrue(getTesterType() != StreamsContext.Type.STANDALONE_TESTER);
    Topology top = new Topology("testMultiTopicProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String groupId = newGroupId(top.getName());
    String[] topics = getKafkaTopics();
    String topic1Val = topics[0];
    String topic2Val = topics[1];
    Supplier<String> topic1 = new Value<String>(topic1Val);
    Supplier<String> topic2 = new Value<String>(topic2Val);
    KafkaProducer producer = new KafkaProducer(top, createProducerConfig());
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig(groupId));
    // Test producer that publishes to multiple topics (implies implicit topic)
    List<Message> topic1Msgs = new ArrayList<>();
    topic1Msgs.add(new SimpleMessage(mgen.create(topic1Val, "Hello"), null, topic1Val));
    topic1Msgs.add(new SimpleMessage(mgen.create(topic1Val, "Are you there?"), null, topic1Val));
    List<Message> topic2Msgs = new ArrayList<>();
    topic2Msgs.add(new SimpleMessage(mgen.create(topic2Val, "Hello"), null, topic2Val));
    topic2Msgs.add(new SimpleMessage(mgen.create(topic2Val, "Are you there?"), null, topic2Val));
    List<Message> msgs = new ArrayList<>(topic1Msgs);
    msgs.addAll(topic2Msgs);
    TStream<Message> msgsToPublish = top.constants(msgs);
    msgsToPublish = msgsToPublish.modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    producer.publish(msgsToPublish);
    TStream<Message> rcvdTopic1Msgs = consumer.subscribe(topic1);
    TStream<Message> rcvdTopic2Msgs = consumer.subscribe(topic2);
    // for validation...
    TStream<Message> rcvdMsgs = rcvdTopic1Msgs.union(rcvdTopic2Msgs);
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    List<String> expectedAsString = mapList(msgs, msgToJSONStringFunc());
    setupDebug();
    if (testBuildOnly(top))
        return;
    completeAndValidateUnordered(groupId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
}
Also used : KafkaProducer(com.ibm.streamsx.topology.messaging.kafka.KafkaProducer) InitialDelay(com.ibm.streamsx.topology.test.InitialDelay) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) ArrayList(java.util.ArrayList) KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Example 8 with KafkaConsumer

use of com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testSubscribeNullThreadsPerTopic.

@Test(expected = IllegalArgumentException.class)
public void testSubscribeNullThreadsPerTopic() throws Exception {
    Topology top = new Topology("testSubscribeNullThreadsPerTopic");
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig("group"));
    // throws IAE
    consumer.subscribe(null, new Value<String>("topic"));
}
Also used : KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 9 with KafkaConsumer

use of com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testSubtypeImplicitTopicProducer.

@Test
public void testSubtypeImplicitTopicProducer() throws Exception {
    checkAssumes();
    Topology top = new Topology("testSubtypeImplicitTopicProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String groupId = newGroupId(top.getName());
    String topicVal = getKafkaTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    KafkaProducer producer = new KafkaProducer(top, createProducerConfig());
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig(groupId));
    // Test producer that takes a TStream<MyMsgSubtype> implicit topic
    List<MyMsgSubtype> msgs = new ArrayList<>();
    msgs.add(new MyMsgSubtype(mgen.create(topicVal, "Hello"), null, topicVal));
    msgs.add(new MyMsgSubtype(mgen.create(topicVal, "key1", "Are you there?"), "key1", topicVal));
    TStream<MyMsgSubtype> msgsToPublish = top.constants(msgs).asType(MyMsgSubtype.class);
    msgsToPublish = msgsToPublish.modify(new InitialDelay<MyMsgSubtype>(PUB_DELAY_MSEC));
    producer.publish(msgsToPublish);
    TStream<Message> rcvdMsgs = consumer.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    List<String> expectedAsString = mapList(msgs, subtypeMsgToJSONStringFunc(null));
    setupDebug();
    if (testBuildOnly(top))
        return;
    completeAndValidate(groupId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
}
Also used : KafkaProducer(com.ibm.streamsx.topology.messaging.kafka.KafkaProducer) InitialDelay(com.ibm.streamsx.topology.test.InitialDelay) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) ArrayList(java.util.ArrayList) KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Example 10 with KafkaConsumer

use of com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testExplicitTopicProducer.

@Test
public void testExplicitTopicProducer() throws Exception {
    checkAssumes();
    Topology top = new Topology("testNewExplicitTopicProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String groupId = newGroupId(top.getName());
    String topicVal = getKafkaTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    KafkaProducer producer = new KafkaProducer(top, createProducerConfig());
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig(groupId));
    // Test producer that takes an arbitrary TStream<T> and explicit topic
    List<Vals> msgs = new ArrayList<>();
    msgs.add(new Vals(mgen.create(topicVal, "Hello"), null, null));
    msgs.add(new Vals(mgen.create(topicVal, "key1", "Are you there?"), "key1", null));
    msgs.add(new Vals(mgen.create(topicVal, "Msg with an empty key"), "", null));
    msgs.add(new Vals("", mgen.create(topicVal, null, "Msg with an empty msg (this is the key)"), null));
    TStream<Vals> valsToPublish = top.constants(msgs).asType(Vals.class);
    TStream<Message> msgsToPublish = valsToPublish.transform(msgFromValsFunc(null));
    msgsToPublish = msgsToPublish.modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    producer.publish(msgsToPublish, topic);
    TStream<Message> rcvdMsgs = consumer.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    List<Message> expectedAsMessage = mapList(msgs, msgFromValsFunc(topicVal));
    expectedAsMessage = modifyList(expectedAsMessage, adjustKey());
    List<String> expectedAsString = mapList(expectedAsMessage, msgToJSONStringFunc());
    setupDebug();
    if (testBuildOnly(top))
        return;
    completeAndValidate(groupId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
}
Also used : KafkaProducer(com.ibm.streamsx.topology.messaging.kafka.KafkaProducer) InitialDelay(com.ibm.streamsx.topology.test.InitialDelay) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) ArrayList(java.util.ArrayList) KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Aggregations

Topology (com.ibm.streamsx.topology.Topology)12 KafkaConsumer (com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer)12 TestTopology (com.ibm.streamsx.topology.test.TestTopology)11 Test (org.junit.Test)11 KafkaProducer (com.ibm.streamsx.topology.messaging.kafka.KafkaProducer)9 Message (com.ibm.streamsx.topology.tuple.Message)8 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)8 Value (com.ibm.streamsx.topology.logic.Value)7 InitialDelay (com.ibm.streamsx.topology.test.InitialDelay)7 ArrayList (java.util.ArrayList)7 JSONObject (com.ibm.json.java.JSONObject)1