Ejemplo n.º 1
0
        public async Task ExactlyOnce_InMemoryInboundLog_DuplicatedMessagesIgnored()
        {
            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(
                    options => options
                    .AddMockedKafka(mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1))
                    .AddInMemoryInboundLog())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IIntegrationEvent>(new KafkaProducerEndpoint(DefaultTopicName))
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
            {
                Configuration =
                {
                    GroupId              = "consumer1",
                    AutoCommitIntervalMs = 100
                },
                ExactlyOnceStrategy = ExactlyOnceStrategy.Log()
            }))
                .AddSingletonSubscriber <OutboundInboundSubscriber>())
            .Run();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IEventPublisher>();

            await publisher.PublishAsync(new TestEventOne());

            await publisher.PublishAsync(
                new TestEventWithUniqueKey
            {
                UniqueKey = "1"
            });

            await publisher.PublishAsync(
                new TestEventWithUniqueKey
            {
                UniqueKey = "2"
            });

            await publisher.PublishAsync(
                new TestEventWithUniqueKey
            {
                UniqueKey = "1"
            });

            await publisher.PublishAsync(
                new TestEventWithUniqueKey
            {
                UniqueKey = "2"
            });

            await publisher.PublishAsync(new TestEventOne());

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            Subscriber.InboundEnvelopes.Should().HaveCount(4);

            Subscriber.InboundEnvelopes[0].Message.Should().BeOfType <TestEventOne>();
            Subscriber.InboundEnvelopes[1].Message.Should().BeOfType <TestEventWithUniqueKey>();
            Subscriber.InboundEnvelopes[1].Message.As <TestEventWithUniqueKey>().UniqueKey.Should().Be("1");
            Subscriber.InboundEnvelopes[2].Message.Should().BeOfType <TestEventWithUniqueKey>();
            Subscriber.InboundEnvelopes[2].Message.As <TestEventWithUniqueKey>().UniqueKey.Should().Be("2");
            Subscriber.InboundEnvelopes[3].Message.Should().BeOfType <TestEventOne>();

            DefaultTopic.GetCommittedOffsetsCount("consumer1").Should().Be(6);
        }
Ejemplo n.º 2
0
        public async Task ExactlyOnce_DbOffsetStore_DuplicatedMessagesIgnored()
        {
            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .UseDbContext <TestDbContext>()
                .WithConnectionToMessageBroker(
                    options => options
                    .AddMockedKafka(mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1))
                    .AddOffsetStoreDatabaseTable())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IIntegrationEvent>(new KafkaProducerEndpoint(DefaultTopicName))
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
            {
                Configuration =
                {
                    GroupId              = "consumer1",
                    AutoCommitIntervalMs = 100
                },
                ExactlyOnceStrategy = ExactlyOnceStrategy.OffsetStore(),
                Events =
                {
                    PartitionsAssignedHandler = (partitions, _) =>
                                                partitions.Select(
                        topicPartition => new TopicPartitionOffset(
                            topicPartition,
                            Offset.Beginning))
                }
            }))
                .AddSingletonSubscriber <OutboundInboundSubscriber>())
            .WithTestDbContext()
            .Run();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IEventPublisher>();

            await publisher.PublishAsync(
                new TestEventOne
            {
                Content = "Message 1"
            });

            await publisher.PublishAsync(
                new TestEventOne
            {
                Content = "Message 2"
            });

            await publisher.PublishAsync(
                new TestEventOne
            {
                Content = "Message 3"
            });

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            Subscriber.InboundEnvelopes.Should().HaveCount(3);

            await Broker.DisconnectAsync();

            await Broker.ConnectAsync();

            await publisher.PublishAsync(
                new TestEventOne
            {
                Content = "Message 4"
            });

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            Subscriber.InboundEnvelopes.Should().HaveCount(4);
        }