예제 #1
0
        public void ZKAwareProducerSends1Message()
        {
            int totalWaitTimeInMiliseconds = 0;
            int waitSingle = 100;
            var originalMessage = new Message(Encoding.UTF8.GetBytes("TestData"));

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);
            multipleBrokersHelper.GetCurrentOffsets();

            var producerConfig = new ProducerConfig(clientConfig);
            var mockPartitioner = new MockAlwaysZeroPartitioner();
            using (var producer = new Producer<string, Message>(producerConfig, mockPartitioner, new DefaultEncoder()))
            {
                var producerData = new ProducerData<string, Message>(
                    CurrentTestTopic, "somekey", new List<Message>() { originalMessage });
                producer.Send(producerData);

                while (!multipleBrokersHelper.CheckIfAnyBrokerHasChanged())
                {
                    totalWaitTimeInMiliseconds += waitSingle;
                    Thread.Sleep(waitSingle);
                    if (totalWaitTimeInMiliseconds > MaxTestWaitTimeInMiliseconds)
                    {
                        Assert.Fail("None of the brokers changed their offset after sending a message");
                    }
                }

                totalWaitTimeInMiliseconds = 0;

                var consumerConfig = new ConsumerConfig(clientConfig)
                    {
                        Host = multipleBrokersHelper.BrokerThatHasChanged.Address,
                        Port = multipleBrokersHelper.BrokerThatHasChanged.Port
                    };
                IConsumer consumer = new Consumers.Consumer(consumerConfig);
                var request = new FetchRequest(CurrentTestTopic, 0, multipleBrokersHelper.OffsetFromBeforeTheChange);

                BufferedMessageSet response;

                while (true)
                {
                    Thread.Sleep(waitSingle);
                    response = consumer.Fetch(request);
                    if (response != null & response.Messages.Count() > 0)
                    {
                        break;
                    }

                    totalWaitTimeInMiliseconds += waitSingle;
                    if (totalWaitTimeInMiliseconds >= MaxTestWaitTimeInMiliseconds)
                    {
                        break;
                    }
                }

                Assert.NotNull(response);
                Assert.AreEqual(1, response.Messages.Count());
                Assert.AreEqual(originalMessage.ToString(), response.Messages.First().ToString());
            }
        }
예제 #2
0
        public void ProducerSends1Message()
        {
            var prodConfig = this.ConfigBasedSyncProdConfig;

            int totalWaitTimeInMiliseconds = 0;
            int waitSingle = 100;
            var originalMessage = new Message(Encoding.UTF8.GetBytes("TestData"));

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);
            multipleBrokersHelper.GetCurrentOffsets(
                new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 });
            using (var producer = new Producer(prodConfig))
            {
                var producerData = new ProducerData<string, Message>(
                    CurrentTestTopic, new List<Message> { originalMessage });
                producer.Send(producerData);
                Thread.Sleep(waitSingle);
            }

            while (
                !multipleBrokersHelper.CheckIfAnyBrokerHasChanged(
                    new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 }))
            {
                totalWaitTimeInMiliseconds += waitSingle;
                Thread.Sleep(waitSingle);
                if (totalWaitTimeInMiliseconds > this.maxTestWaitTimeInMiliseconds)
                {
                    Assert.Fail("None of the brokers changed their offset after sending a message");
                }
            }

            totalWaitTimeInMiliseconds = 0;

            var consumerConfig = new ConsumerConfiguration(
                multipleBrokersHelper.BrokerThatHasChanged.Host, multipleBrokersHelper.BrokerThatHasChanged.Port);
            IConsumer consumer = new Consumer(consumerConfig);
            var request1 = new FetchRequest(CurrentTestTopic, multipleBrokersHelper.PartitionThatHasChanged, multipleBrokersHelper.OffsetFromBeforeTheChange);
            BufferedMessageSet response;
            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request1);
                if (response != null && response.Messages.Count() > 0)
                {
                    break;
                }

                totalWaitTimeInMiliseconds += waitSingle;
                if (totalWaitTimeInMiliseconds >= this.maxTestWaitTimeInMiliseconds)
                {
                    break;
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(1, response.Messages.Count());
            Assert.AreEqual(originalMessage.ToString(), response.Messages.First().ToString());
        }
예제 #3
0
        public void ZkAwareProducerSends1Message()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            int totalWaitTimeInMiliseconds = 0;
            int waitSingle      = 100;
            var originalMessage = new Message(Encoding.UTF8.GetBytes("TestData2"));
            var topic           = CurrentTestTopic;

            var multipleBrokersHelper = new TestMultipleBrokersHelper(topic);

            multipleBrokersHelper.GetCurrentOffsets(new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 });

            var mockPartitioner = new MockAlwaysZeroPartitioner();

            using (var producer = new Producer <string, Message>(prodConfig, mockPartitioner, new DefaultEncoder()))
            {
                var producerData = new ProducerData <string, Message>(
                    topic, "somekey", new List <Message> {
                    originalMessage
                });
                producer.Send(producerData);

                while (!multipleBrokersHelper.CheckIfAnyBrokerHasChanged(new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 }))
                {
                    totalWaitTimeInMiliseconds += waitSingle;
                    Thread.Sleep(waitSingle);
                    if (totalWaitTimeInMiliseconds > this.maxTestWaitTimeInMiliseconds)
                    {
                        Assert.Fail("None of the brokers changed their offset after sending a message");
                    }
                }

                totalWaitTimeInMiliseconds = 0;

                var consumerConfig = new ConsumerConfiguration(
                    multipleBrokersHelper.BrokerThatHasChanged.Host,
                    multipleBrokersHelper.BrokerThatHasChanged.Port);
                IConsumer consumer = new Consumer(consumerConfig);
                var       request  = new FetchRequest(topic, multipleBrokersHelper.PartitionThatHasChanged, multipleBrokersHelper.OffsetFromBeforeTheChange);

                BufferedMessageSet response;

                while (true)
                {
                    Thread.Sleep(waitSingle);
                    response = consumer.Fetch(request);
                    if (response != null & response.Messages.Count() > 0)
                    {
                        break;
                    }

                    totalWaitTimeInMiliseconds += waitSingle;
                    if (totalWaitTimeInMiliseconds >= this.maxTestWaitTimeInMiliseconds)
                    {
                        break;
                    }
                }

                Assert.NotNull(response);
                Assert.AreEqual(1, response.Messages.Count());
                Assert.AreEqual(originalMessage.ToString(), response.Messages.First().ToString());
            }
        }
예제 #4
0
        public void ProducerSends1MessageUsingNotDefaultEncoder()
        {
            var prodConfig = this.ConfigBasedSyncProdConfig;

            int    totalWaitTimeInMiliseconds = 0;
            int    waitSingle      = 100;
            string originalMessage = "TestData";

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);

            multipleBrokersHelper.GetCurrentOffsets(new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 });
            using (var producer = new Producer <string, string>(prodConfig, null, new StringEncoder(), null))
            {
                var producerData = new ProducerData <string, string>(
                    CurrentTestTopic, new List <string> {
                    originalMessage
                });

                producer.Send(producerData);
            }

            Thread.Sleep(waitSingle);

            while (!multipleBrokersHelper.CheckIfAnyBrokerHasChanged(new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 }))
            {
                totalWaitTimeInMiliseconds += waitSingle;
                Thread.Sleep(waitSingle);
                if (totalWaitTimeInMiliseconds > this.maxTestWaitTimeInMiliseconds)
                {
                    Assert.Fail("None of the brokers changed their offset after sending a message");
                }
            }

            totalWaitTimeInMiliseconds = 0;

            var consumerConfig = new ConsumerConfiguration(
                multipleBrokersHelper.BrokerThatHasChanged.Host,
                multipleBrokersHelper.BrokerThatHasChanged.Port);
            IConsumer consumer = new Consumer(consumerConfig);
            var       request  = new FetchRequest(CurrentTestTopic, multipleBrokersHelper.PartitionThatHasChanged, multipleBrokersHelper.OffsetFromBeforeTheChange);

            BufferedMessageSet response;

            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request);
                if (response != null && response.Messages.Count() > 0)
                {
                    break;
                }

                totalWaitTimeInMiliseconds += waitSingle;
                if (totalWaitTimeInMiliseconds >= this.maxTestWaitTimeInMiliseconds)
                {
                    break;
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(1, response.Messages.Count());
            Assert.AreEqual(originalMessage, Encoding.UTF8.GetString(response.Messages.First().Payload));
        }
예제 #5
0
        public void ProducerSends3Messages()
        {
            var prodConfig = this.ConfigBasedSyncProdConfig;

            int totalWaitTimeInMiliseconds = 0;
            int waitSingle          = 100;
            var originalMessage1    = new Message(Encoding.UTF8.GetBytes("TestData1"));
            var originalMessage2    = new Message(Encoding.UTF8.GetBytes("TestData2"));
            var originalMessage3    = new Message(Encoding.UTF8.GetBytes("TestData3"));
            var originalMessageList = new List <Message> {
                originalMessage1, originalMessage2, originalMessage3
            };

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);

            multipleBrokersHelper.GetCurrentOffsets(
                new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 });
            using (var producer = new Producer(prodConfig))
            {
                var producerData = new ProducerData <string, Message>(CurrentTestTopic, originalMessageList);
                producer.Send(producerData);
            }

            Thread.Sleep(waitSingle);
            while (
                !multipleBrokersHelper.CheckIfAnyBrokerHasChanged(
                    new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 }))
            {
                totalWaitTimeInMiliseconds += waitSingle;
                Thread.Sleep(waitSingle);
                if (totalWaitTimeInMiliseconds > this.maxTestWaitTimeInMiliseconds)
                {
                    Assert.Fail("None of the brokers changed their offset after sending a message");
                }
            }

            totalWaitTimeInMiliseconds = 0;

            var consumerConfig = new ConsumerConfiguration(
                multipleBrokersHelper.BrokerThatHasChanged.Host, multipleBrokersHelper.BrokerThatHasChanged.Port);
            IConsumer consumer = new Consumer(consumerConfig);
            var       request  = new FetchRequest(CurrentTestTopic, multipleBrokersHelper.PartitionThatHasChanged, multipleBrokersHelper.OffsetFromBeforeTheChange);

            BufferedMessageSet response;

            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request);
                if (response != null && response.Messages.Count() > 2)
                {
                    break;
                }

                totalWaitTimeInMiliseconds += waitSingle;
                if (totalWaitTimeInMiliseconds >= this.maxTestWaitTimeInMiliseconds)
                {
                    break;
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(3, response.Messages.Count());
            Assert.AreEqual(originalMessage1.ToString(), response.Messages.First().ToString());
            Assert.AreEqual(originalMessage2.ToString(), response.Messages.Skip(1).First().ToString());
            Assert.AreEqual(originalMessage3.ToString(), response.Messages.Skip(2).First().ToString());
        }
예제 #6
0
        public void ZkAwareProducerSends3Messages()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;
            int totalWaitTimeInMiliseconds = 0;
            int waitSingle = 100;
            var originalMessage1 = new Message(Encoding.UTF8.GetBytes("TestData1"));
            var originalMessage2 = new Message(Encoding.UTF8.GetBytes("TestData2"));
            var originalMessage3 = new Message(Encoding.UTF8.GetBytes("TestData3"));
            var originalMessageList = new List<Message> { originalMessage1, originalMessage2, originalMessage3 };

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);
            multipleBrokersHelper.GetCurrentOffsets(new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 });

            var mockPartitioner = new MockAlwaysZeroPartitioner();
            using (var producer = new Producer<string, Message>(prodConfig, mockPartitioner, new DefaultEncoder()))
            {
                var producerData = new ProducerData<string, Message>(CurrentTestTopic, "somekey", originalMessageList);
                producer.Send(producerData);

                while (!multipleBrokersHelper.CheckIfAnyBrokerHasChanged(new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 }))
                {
                    totalWaitTimeInMiliseconds += waitSingle;
                    Thread.Sleep(waitSingle);
                    if (totalWaitTimeInMiliseconds > this.maxTestWaitTimeInMiliseconds)
                    {
                        Assert.Fail("None of the brokers changed their offset after sending a message");
                    }
                }

                totalWaitTimeInMiliseconds = 0;

                var consumerConfig = new ConsumerConfiguration(
                    multipleBrokersHelper.BrokerThatHasChanged.Host,
                    multipleBrokersHelper.BrokerThatHasChanged.Port);
                IConsumer consumer = new Consumer(consumerConfig);
                var request = new FetchRequest(CurrentTestTopic, 0, multipleBrokersHelper.OffsetFromBeforeTheChange);
                BufferedMessageSet response;

                while (true)
                {
                    Thread.Sleep(waitSingle);
                    response = consumer.Fetch(request);
                    if (response != null && response.Messages.Count() > 2)
                    {
                        break;
                    }

                    totalWaitTimeInMiliseconds += waitSingle;
                    if (totalWaitTimeInMiliseconds >= this.maxTestWaitTimeInMiliseconds)
                    {
                        break;
                    }
                }

                Assert.NotNull(response);
                Assert.AreEqual(3, response.Messages.Count());
                Assert.AreEqual(originalMessage1.ToString(), response.Messages.First().ToString());
                Assert.AreEqual(originalMessage2.ToString(), response.Messages.Skip(1).First().ToString());
                Assert.AreEqual(originalMessage3.ToString(), response.Messages.Skip(2).First().ToString());
            }
        }
예제 #7
0
        public void SimpleSyncProducerSendsLotsOfCompressedMessagesWithIncreasedSizeAndConsumerConnectorGetsThemBack()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ZooKeeperBasedConsumerConfig;

            consumerConfig.AutoCommit         = true;
            consumerConfig.AutoCommitInterval = 100;
            int    numberOfMessages   = 2000;
            int    messagesPerPackage = 5;
            string topic = CurrentTestTopic;

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);

            multipleBrokersHelper.GetCurrentOffsets(
                new[] { prodConfig });

            int  msgNr     = 0;
            long totalSize = 0;

            using (var producer = new SyncProducer(prodConfig))
            {
                for (int i = 0; i < numberOfMessages; i++)
                {
                    var messagePackageList = new List <Message>();
                    for (int messageInPackageNr = 0; messageInPackageNr < messagesPerPackage; messageInPackageNr++)
                    {
                        string payload1     = CreatePayloadByNumber(msgNr);
                        byte[] payloadData1 = Encoding.UTF8.GetBytes(payload1);
                        var    msg          = new Message(payloadData1);
                        totalSize += msg.Size;
                        messagePackageList.Add(msg);
                        msgNr++;
                    }
                    var packageMessage = CompressionUtils.Compress(messagePackageList, CompressionCodecs.GZIPCompressionCodec);
                    producer.Send(topic, 0, new List <Message>()
                    {
                        packageMessage
                    });
                }
            }

            // now consuming
            int  resultCount = 0;
            long resultSize  = 0;

            using (IConsumerConnector consumerConnector = new ZookeeperConsumerConnector(consumerConfig, true))
            {
                var topicCount = new Dictionary <string, int> {
                    { topic, 1 }
                };
                var messages = consumerConnector.CreateMessageStreams(topicCount);
                var sets     = messages[topic];

                try
                {
                    foreach (var set in sets)
                    {
                        foreach (var message in set)
                        {
                            Assert.AreEqual(CreatePayloadByNumber(resultCount), Encoding.UTF8.GetString(message.Payload));
                            resultCount++;
                            resultSize += message.Size;
                        }
                    }
                }
                catch (ConsumerTimeoutException)
                {
                    // do nothing, this is expected
                }
            }

            Assert.AreEqual(numberOfMessages * messagesPerPackage, resultCount);
            Assert.AreEqual(totalSize, resultSize);
        }