Example #1
0
        public async Task CommitWithMetadataSource_Commit_metadata_in_message_Should_work()
        {
            var topic = CreateTopic(1);
            var group = CreateGroup(1);

            string MetadataFromMessage <K, V>(ConsumeResult <K, V> message) => message.Offset.ToString();

            await ProduceStrings(topic, Enumerable.Range(1, 10), ProducerSettings);

            var(control, probe) = KafkaConsumer.CommitWithMetadataSource(CreateConsumerSettings <string>(group), Subscriptions.Topics(topic), MetadataFromMessage)
                                  .ToMaterialized(this.SinkProbe <CommittableMessage <Null, string> >(), Keep.Both)
                                  .Run(Materializer);

            probe.Request(10);

            probe.Within(TimeSpan.FromSeconds(10), () => probe.ExpectNextN(10)).ForEach(message =>
            {
                var offsetWithMeta = message.CommitableOffset as ICommittableOffsetMetadata;
                offsetWithMeta.Should().NotBeNull();
                offsetWithMeta.Metadata.Should().Be(message.CommitableOffset.Offset.Offset.ToString());
            });

            probe.Cancel();

            AwaitCondition(() => control.IsShutdown.IsCompletedSuccessfully, TimeSpan.FromSeconds(10));
        }
        private Source <CommittableMessage <K, V>, IControl> CreateSourceWithMetadata(
            MockConsumer <K, V> mock,
            Func <ConsumeResult <K, V>, string> metadataFromRecord,
            string groupId  = "group1",
            string[] topics = null)
        {
            var settings = ConsumerSettings <K, V> .Create(Sys, Deserializers.Utf8, Deserializers.Utf8)
                           .WithGroupId(groupId)
                           .WithConsumerFactory(_ => mock.Mock);

            mock.Settings = settings;

            return(KafkaConsumer.CommitWithMetadataSource(
                       settings,
                       Subscriptions.Topics(topics),
                       metadataFromRecord));
        }