Beispiel #1
0
        public async Task OutboxProduceStrategy_DefaultSettings_ProducedAndConsumed()
        {
            var serviceProvider = Host
                                  .WithTestDbContext()
                                  .ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .UseDbContext <TestDbContext>()
                .WithConnectionToMessageBroker(
                    options => options
                    .AddMockedKafka()
                    .AddOutboxDatabaseTable()
                    .AddOutboxWorker(TimeSpan.FromMilliseconds(100)))
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IIntegrationEvent>(
                        new KafkaProducerEndpoint(DefaultTopicName)
            {
                Strategy = ProduceStrategy.Outbox()
            })
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
            {
                Configuration = new KafkaConsumerConfig
                {
                    GroupId = "consumer1",
                    AutoCommitIntervalMs = 100
                }
            }))
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>()
                .AddSingletonSubscriber <OutboundInboundSubscriber>())
                                  .Run();

            var publisher = serviceProvider.GetRequiredService <IEventPublisher>();
            var dbContext = serviceProvider.GetRequiredService <TestDbContext>();

            for (int i = 1; i <= 15; i++)
            {
                await publisher.PublishAsync(new TestEventOne { Content = $"{i}" });
            }

            await dbContext.SaveChangesAsync();

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            Subscriber.OutboundEnvelopes.Should().HaveCount(15);
            Subscriber.InboundEnvelopes.Should().HaveCount(15);

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(15);
            SpyBehavior.InboundEnvelopes.Should().HaveCount(15);

            SpyBehavior.InboundEnvelopes
            .Select(envelope => ((TestEventOne)envelope.Message !).Content)
            .Should().BeEquivalentTo(Enumerable.Range(1, 15).Select(i => $"{i}"));
        }
        public async Task HandleAsync_OutboundMessageWithOutboxStrategy_RelayedToOutbox()
        {
            var outboundEnvelope = new OutboundEnvelope <TestEventOne>(
                new TestEventOne(),
                Array.Empty <MessageHeader>(),
                new TestProducerEndpoint("test")
            {
                Strategy = ProduceStrategy.Outbox()
            });

            await _behavior.HandleAsync(new[] { outboundEnvelope, outboundEnvelope, outboundEnvelope }, Task.FromResult !);

            await _outbox.CommitAsync();

            var queued = await _outbox.ReadAsync(10);

            queued.Should().HaveCount(3);
            _broker.ProducedMessages.Should().BeEmpty();
        }