Exemplo n.º 1
0
        public void SeekMethodSadPath()
        {
            var receiver = new KafkaReceiver("name", "one_topic", "groupId", "servers");

            Action act = () => receiver.Seek(DateTime.Now);

            act.Should().ThrowExactly <InvalidOperationException>()
            .WithMessage("Seek cannot be called before the receiver has been started.");
        }
Exemplo n.º 2
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);
        }