public void ZkAwareProducerSendsLotsOfMessagesAndSessionCreatedHandlerInvokedInTheBackgroundShouldNotThrowException()
        {
            var prodConfig          = this.ZooKeeperBasedAsyncProdConfig;
            var originalMessage     = new Message(Encoding.UTF8.GetBytes("TestData1"));
            var originalMessageList = new List <Message> {
                originalMessage
            };
            int numberOfPackagesToSend = 500;
            int runBackgroundWorkerAfterNIterations = 100;

            var mockPartitioner = new MockAlwaysZeroPartitioner();

            using (var producer = new Producer <string, Message>(prodConfig, mockPartitioner, new DefaultEncoder()))
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += new DoWorkEventHandler(ZkAwareProducerSendsLotsOfMessagesAndSessionCreatedHandlerInvokedInTheBackgroundShouldNotThrowException_DoWork);

                for (int i = 0; i < numberOfPackagesToSend; i++)
                {
                    if (i == runBackgroundWorkerAfterNIterations)
                    {
                        bw.RunWorkerAsync(producer);
                    }
                    var producerData = new ProducerData <string, Message>(CurrentTestTopic, "somekey",
                                                                          originalMessageList);
                    producer.Send(producerData);
                }

                bw.CancelAsync();
            }
        }
        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());
            }
        }