Ejemplo n.º 1
0
        public void TestMultiTopic()
        {
            var topic = "persistent://public/default/topic" + Guid.NewGuid();

            var topic2 = "persistent://public/default/topic2" + Guid.NewGuid();

            var            topic3 = "persistent://public/default/topic3" + Guid.NewGuid();
            IList <string> topics = new List <string> {
                topic, topic2, topic3
            };
            var builder = new ReaderConfigBuilder <string>()
                          .Topics(topics)
                          .StartMessageId(IMessageId.Earliest)

                          .ReaderName("my-reader");

            var reader = _client.NewReader(ISchema <object> .String, builder);
            // create producer and send msg
            IList <Producer <string> > producerList = new List <Producer <string> >();

            foreach (var topicName in topics)
            {
                var producer = _client.NewProducer(ISchema <object> .String, new ProducerConfigBuilder <string>().Topic(topicName));

                producerList.Add(producer);
            }
            var           msgNum   = 10;
            ISet <string> messages = new HashSet <string>();

            for (var i = 0; i < producerList.Count; i++)
            {
                var producer = producerList[i];
                for (var j = 0; j < msgNum; j++)
                {
                    var msg = i + "msg" + j;
                    producer.Send(msg);
                    messages.Add(msg);
                }
            }
            // receive messagesS
            var message = reader.ReadNext(TimeSpan.FromSeconds(5));

            while (message != null)
            {
                var value = message.Value;
                _output.WriteLine(value);
                Assert.True(messages.Remove(value));
                message = reader.ReadNext(TimeSpan.FromSeconds(5));
            }
            Assert.Equal(0, messages.Count);
            // clean up
            foreach (var producer in producerList)
            {
                producer.Close();
            }
            reader.Close();
        }
Ejemplo n.º 2
0
        public virtual void ReaderInstantiation()
        {
            var reader = new ReaderConfigBuilder <string>();

            reader.Topic("ReaderInstantiation");
            reader.StartMessageId(IMessageId.Earliest);
            var stringReaderBuilder = _client.NewReader(new StringSchema(), reader);

            Assert.NotNull(stringReaderBuilder);
            stringReaderBuilder.Close();
        }
Ejemplo n.º 3
0
        private void TestReadMessages(string topic, bool enableBatch)
        {
            var numKeys = 10;

            var builder = new ReaderConfigBuilder <byte[]>()
                          .Topic(topic)
                          .StartMessageId(IMessageId.Earliest)
                          .ReaderName(Subscription);
            var reader = _client.NewReader(builder);

            var keys = PublishMessages(topic, numKeys, enableBatch);

            Thread.Sleep(TimeSpan.FromSeconds(5));
            for (var i = 0; i < numKeys; i++)
            {
                var message = (Message <byte[]>)reader.ReadNext();
                if (message != null)
                {
                    _output.WriteLine($"{message.Key}:{message.MessageId}:{Encoding.UTF8.GetString(message.Data)}");
                    Assert.True(keys.Remove(message.Key));
                }
                else
                {
                    break;
                }
            }
            Assert.True(keys.Count == 0);
        }