Example #1
0
        public void CouldAssingConsumerForTopicWatermark()
        {
            // Arrange
            var topicName = new TopicName("Test");
            var offset    = new WatermarkOffsets(new Offset(1), new Offset(2));
            var partition = new Partition(1);
            var watermark = new PartitionWatermark(topicName, offset, partition);

            var topicWatermark = new TopicWatermark(new[] { watermark });
            var consumerMock   = new Mock <IConsumer <object, object> >();
            var consumer       = consumerMock.Object;

            // Act
            var exception = Record.Exception(() => topicWatermark.AssignWithConsumer(consumer));

            // Assert
            exception.Should().BeNull();

            consumerMock.Verify(x => x.Assign(It.Is <IEnumerable <TopicPartitionOffset> >(value =>
                                                                                          value.Single().Topic == topicName.Value
                                                                                          &&
                                                                                          value.Single().Partition == partition
                                                                                          &&
                                                                                          value.Single().Offset == offset.High
                                                                                          &&
                                                                                          value.Single().TopicPartition.Topic == topicName.Value
                                                                                          &&
                                                                                          value.Single().TopicPartition.Partition == partition
                                                                                          )
                                              ), Times.Once);
        }
Example #2
0
        public void CouldNotAssingNullConsumerForTopicWatermark()
        {
            // Arrange
            var partitionWatermarks             = (new Mock <IEnumerable <PartitionWatermark> >()).Object;
            var watermark                       = new TopicWatermark(partitionWatermarks);
            IConsumer <object, object> consumer = null !;

            // Act
            var exception = Record.Exception(() => watermark.AssignWithConsumer(consumer));

            // Assert
            exception.Should().NotBeNull().And.BeOfType <ArgumentNullException>();
        }
Example #3
0
        public void CanCreateTopicWatermarkWithValidParams()
        {
            // Arrange
            var partitionWatermarks = (new Mock <IEnumerable <PartitionWatermark> >()).Object;

            // Act
            TopicWatermark result    = null !;
            var            exception = Record.Exception(() => result = new TopicWatermark(partitionWatermarks));

            // Assert
            exception.Should().BeNull();
            result.Watermarks.Should().BeEquivalentTo(partitionWatermarks);
        }
        public async Task CanLoadWatermarksWithValidParamsAsync()
        {
            // Arrange
            var topic        = new TopicName("test");
            var clientMock   = new Mock <IAdminClient>();
            var client       = clientMock.Object;
            var timeout      = 1000;
            var loader       = new TopicWatermarkLoader(topic, client, timeout);
            var consumerMock = new Mock <IConsumer <object, object> >();

            IConsumer <object, object> consumerFactory() => consumerMock.Object;

            var adminClientPartition = new TopicPartition(topic.Value, new Partition(1));

            var adminParitions = new[] { adminClientPartition };

            var borkerMeta = new BrokerMetadata(1, "testHost", 1000);

            var partitionMeta = new PartitionMetadata(1, 1, new[] { 1 }, new[] { 1 }, null);

            var topicMeta = new TopicMetadata(topic.Value, new[] { partitionMeta }.ToList(), null);

            var meta = new Confluent.Kafka.Metadata(
                new[] { borkerMeta }.ToList(),
                new[] { topicMeta }.ToList(), 1, "test"
                );

            clientMock.Setup(c => c.GetMetadata(topic.Value, TimeSpan.FromSeconds(timeout))).Returns(meta);

            var offets = new WatermarkOffsets(new Offset(1), new Offset(2));

            consumerMock.Setup(x => x.QueryWatermarkOffsets(adminClientPartition, TimeSpan.FromSeconds(timeout))).Returns(offets);

            TopicWatermark result = null !;

            // Act
            var exception = await Record.ExceptionAsync(async() => result = await loader.LoadWatermarksAsync(consumerFactory, CancellationToken.None));

            // Assert
            exception.Should().BeNull();

            consumerMock.Verify(x => x.Close(), Times.Once);

            consumerMock.Verify(x => x.Dispose(), Times.Once);

            result.Should().NotBeNull();

            var watermarks = result.Watermarks.ToList();

            watermarks.Should().ContainSingle();

            clientMock.Verify(c => c.GetMetadata(topic.Value, TimeSpan.FromSeconds(timeout)), Times.Once);

            consumerMock.Verify(x => x.QueryWatermarkOffsets(adminClientPartition, TimeSpan.FromSeconds(timeout)), Times.Once);

            watermarks.Single().TopicName.Should().Be(topic);

            watermarks.Single().Partition.Value.Should().Be(partitionMeta.PartitionId);

            watermarks.Single().Offset.Should().Be(offets);
        }