Exemple #1
0
        public static void CreateWithSchemaIdRequired()
        {
            var name                = "name";
            var topic               = "topic";
            var groupId             = "groupId";
            var servers             = "bootstrapServers";
            var enableOffsetRestore = true;
            var schemaIdRequired    = true;
            var autoOffsetReset     = AutoOffsetReset.Error;
            var consumer            = new ConsumerConfig()
            {
                GroupId               = groupId,
                BootstrapServers      = servers,
                EnableAutoOffsetStore = enableOffsetRestore,
                AutoOffsetReset       = autoOffsetReset
            };

            using var receiver = new KafkaReceiver(name, topic, consumer, schemaIdRequired: schemaIdRequired);
            receiver.Name.Should().Be(name);
            receiver.Topic.Should().Be(topic);
            receiver.GroupId.Should().Be(groupId);
            receiver.BootstrapServers.Should().Be(servers);
            receiver.EnableAutoOffsetStore.Should().Be(enableOffsetRestore);
            receiver.AutoOffsetReset.Should().Be(autoOffsetReset);
            receiver.Consumer.Should().NotBeNull();

            var unlockedReceiver = receiver.Unlock();

            Assert.Equal(schemaIdRequired, unlockedReceiver._schemaIdRequired);
        }
Exemple #2
0
        public void KafkaReceiverStartHappyPath1()
        {
            var receiver = new KafkaReceiver("name", "one_topic", "groupId", "servers");

            var consumerMock = new Mock <IConsumer <string, byte[]> >();

            consumerMock.Setup(c => c.Subscribe(It.IsAny <string>()));

            var unlockedReceiver = receiver.Unlock();

            unlockedReceiver._consumer = new Lazy <IConsumer <string, byte[]> >(() => consumerMock.Object);
            unlockedReceiver.Start();

            consumerMock.Verify(cm => cm.Subscribe("one_topic"), Times.Once);

            unlockedReceiver.Dispose();
        }
Exemple #3
0
        public void SeekMethodHappyPath()
        {
            var assignments = new List <TopicPartition>
            {
                new TopicPartition("one_topic", new Partition(0)),
                new TopicPartition("one_topic", new Partition(1))
            };
            var offsetsForTimes = new List <TopicPartitionOffset>
            {
                new TopicPartitionOffset(assignments[0], new Offset(1)),
                new TopicPartitionOffset(assignments[1], new Offset(2))
            };

            TopicPartitionTimestamp[] actualTimestamps = null;

            Action <IEnumerable <TopicPartitionTimestamp>, TimeSpan> callback = (timestamps, timeout) =>
                                                                                actualTimestamps = timestamps.ToArray();

            var mockConsumer = new Mock <IConsumer <string, byte[]> >();

            mockConsumer.Setup(m => m.Assignment).Returns(assignments);
            mockConsumer.Setup(m => m.OffsetsForTimes(It.IsAny <IEnumerable <TopicPartitionTimestamp> >(), It.IsAny <TimeSpan>()))
            .Returns(offsetsForTimes)
            .Callback(callback);

            var lazyConsumer = new Lazy <IConsumer <string, byte[]> >(() => mockConsumer.Object);

            var receiver = new KafkaReceiver("name", "one_topic", "groupId", "servers");

            receiver.Unlock()._consumer = lazyConsumer;

            var timestamp = new DateTime(2020, 9, 3, 17, 52, 37, DateTimeKind.Local).ToUniversalTime();

            receiver.Seek(timestamp);

            mockConsumer.Verify(m => m.Seek(offsetsForTimes[0]), Times.Once());
            mockConsumer.Verify(m => m.Seek(offsetsForTimes[1]), Times.Once());

            var expectedTimestampsToSearch = assignments.Select(tp => new TopicPartitionTimestamp(tp, new Timestamp(timestamp))).ToArray();

            mockConsumer.Verify(m => m.OffsetsForTimes(expectedTimestampsToSearch, It.IsAny <TimeSpan>()), Times.Once());

            actualTimestamps.Should().HaveCount(2);
            actualTimestamps.Select(t => t.TopicPartition).Should().BeEquivalentTo(assignments);
            actualTimestamps.Select(t => t.Timestamp.UtcDateTime).Should().AllBeEquivalentTo(timestamp);
        }
Exemple #4
0
        public static void CallAddStatisticsEmittedHandlerForReceiver()
        {
            using var receiver = new KafkaReceiver("NAME", "TOPIC", "GROUPID", "SERVERS");
            var statsData = "STATS!";
            var callCount = 0;

            void Handler(object?sender, string stats)
            {
                sender.Should().BeSameAs(receiver);
                stats.Should().Be(statsData);
                callCount++;
            }

            receiver.AddStatisticsEmittedHandler(Handler);
            receiver.Unlock().OnStatisticsEmitted(null, statsData);

            callCount.Should().Be(1, "Event handler should have been called");
        }
Exemple #5
0
        public static void KafkaReceiverStopWithDispose()
        {
            using var receiver = new KafkaReceiver("name", "one_topic", "groupId", "servers");

            var consumerMock = new Mock <IConsumer <string, byte[]> >();

            consumerMock.Setup(cm => cm.Close());
            consumerMock.Setup(cm => cm.Dispose());

            var unlockedReceiver = receiver.Unlock();

            unlockedReceiver._consumer = new Lazy <IConsumer <string, byte[]> >(() => consumerMock.Object);
            unlockedReceiver.Start();

            unlockedReceiver.Dispose();

            consumerMock.Verify(cm => cm.Close(), Times.Once);
            consumerMock.Verify(cm => cm.Dispose(), Times.Once);
        }
Exemple #6
0
        public void KafkaReceiverHappyPath()
        {
            var message = new Message <string, byte[]>()
            {
                Value = Encoding.UTF8.GetBytes("This is the expected message!")
            };
            var result = new ConsumeResult <string, byte[]>()
            {
                Message = message
            };

            var consumerMock = new Mock <IConsumer <string, byte[]> >();

            consumerMock.Setup(c => c.Subscribe(It.IsAny <string>()));
            consumerMock.Setup(c => c.Consume(It.IsAny <CancellationToken>())).Returns(result);

            var waitHandle = new AutoResetEvent(false);

            string receivedMessage = null;

            using (var receiver = new KafkaReceiver("NAME", "TOPIC", "GROUPID", "SERVER"))
            {
                var unlockedReceiver = receiver.Unlock();
                unlockedReceiver._consumer = new Lazy <IConsumer <string, byte[]> >(() => consumerMock.Object);

                receiver.Start(m =>
                {
                    receivedMessage = m.StringPayload;
                    waitHandle.Set();
                    return(Task.CompletedTask);
                });

                waitHandle.WaitOne();
            }

            consumerMock.Verify(m => m.Consume(It.IsAny <CancellationToken>()));

            receivedMessage.Should().Be("This is the expected message!");
        }