Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncProducer"/> class.
        /// </summary>
        /// <param name="config">
        /// The producer config.
        /// </param>
        /// <param name="callbackHandler">
        /// The callback invoked when a request is finished being sent.
        /// </param>
        public AsyncProducer(
            AsyncProducerConfig config,
            ICallbackHandler callbackHandler)
        {
            Guard.Assert<ArgumentNullException>(() => config != null);

            this.config = config;
            this.callbackHandler = callbackHandler;
        }
Beispiel #2
0
        public void AsyncProducerSendsAndConsumerReceivesSingleSimpleMessage()
        {
            Message sourceMessage = new Message(Encoding.UTF8.GetBytes("test message"));

            var config = new AsyncProducerConfig(clientConfig);
            var producer = new AsyncProducer(config);
            var producerRequest = new ProducerRequest(CurrentTestTopic, 0, new List<Message>() { sourceMessage });

            long currentOffset = TestHelper.GetCurrentKafkaOffset(CurrentTestTopic, clientConfig);

            producer.Send(producerRequest);

            ConsumerConfig consumerConfig = new ConsumerConfig(clientConfig);
            IConsumer consumer = new Consumers.Consumer(consumerConfig);
            FetchRequest request = new FetchRequest(CurrentTestTopic, 0, currentOffset);

            BufferedMessageSet response;
            int totalWaitTimeInMiliseconds = 0;
            int waitSingle = 100;
            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request);
                if (response != null && response.Messages.Count() > 0)
                {
                    break;
                }
                else
                {
                    totalWaitTimeInMiliseconds += waitSingle;
                    if (totalWaitTimeInMiliseconds >= MaxTestWaitTimeInMiliseconds)
                    {
                        break;
                    }
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(1, response.Messages.Count());
            Message resultMessage = response.Messages.First();
            Assert.AreEqual(sourceMessage.ToString(), resultMessage.ToString());
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncProducer"/> class.
 /// </summary>
 /// <param name="config">
 /// The producer config.
 /// </param>
 public AsyncProducer(AsyncProducerConfig config)
     : this(config,
         ReflectionHelper.Instantiate<ICallbackHandler>(config.CallbackHandler))
 {
 }
Beispiel #4
0
 public void AsyncProducerSendsMessageWithCallbackClass()
 {
     List<Message> messages = new List<Message>()
                                  {
                                      new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                  };
     var config = new AsyncProducerConfig(clientConfig);
     TestCallbackHandler myHandler = new TestCallbackHandler();
     var producer = new AsyncProducer(config, myHandler);
     producer.Send(CurrentTestTopic, 0, messages);
     Thread.Sleep(1000);
     Assert.IsTrue(myHandler.WasRun);
 }
Beispiel #5
0
        public void AsyncProducerSendsManyLongRandomMessages()
        {
            List<Message> messages = GenerateRandomTextMessages(50);

            var config = new AsyncProducerConfig(clientConfig);

            var producer = new AsyncProducer(config);
            producer.Send(CurrentTestTopic, 0, messages);
        }
Beispiel #6
0
        public void AsyncProducerSendsFewShortFixedMessages()
        {
            List<Message> messages = new List<Message>()
                                         {
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 2")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 3")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 4"))
                                         };

            var config = new AsyncProducerConfig(clientConfig);

            var producer = new AsyncProducer(config);
            producer.Send(CurrentTestTopic, 0, messages);
        }