public async Task Can_persist_outbox_messages_with_different_serializer_formats() { var services = new ServiceCollection(); var fake = new FakeOutboxPersistence(); services.AddOutbox(options => { options.WithOutboxEntryRepository(serviceProvider => fake); options.Register <DummyMessage>("foo", "bar", x => "baz"); options.WithPayloadSerializer("foo", new PayloadSerializerStub("foo_dummy", "expected foo payload format")); options.Register <AnotherDummyMessage>("bar", "bar", x => "baz"); options.WithPayloadSerializer("bar", new PayloadSerializerStub("bar_dummy", "expected foo payload format")); }); var provider = services.BuildServiceProvider(); var outbox = provider.GetRequiredService <OutboxQueue>(); await outbox.Enqueue(new[] { new DummyMessage() }); await outbox.Enqueue(new[] { new AnotherDummyMessage() }); Assert.Equal( expected: new[] { "foo_dummy", "bar_dummy", }, actual: fake.OutboxEntries.Select(x => x.Payload) ); }
public async Task Can_persist_outbox_message() { var services = new ServiceCollection(); var fake = new FakeOutboxPersistence(); var messageId = Guid.NewGuid(); services.AddOutbox(options => { options.WithMessageIdGenerator(new MessageIdGeneratorStub(() => messageId.ToString())); options.Register <DummyMessage>("foo", "bar", x => "baz"); options.WithPayloadSerializer("foo", new PayloadSerializerStub("dummy")); options.WithOutboxEntryRepository(serviceProvider => fake); }); var provider = services.BuildServiceProvider(); var outbox = provider.GetRequiredService <OutboxQueue>(); await outbox.Enqueue(new[] { new DummyMessage() }); var entry = fake.OutboxEntries.Single(); Assert.Equal("foo", entry.Topic); Assert.Equal(messageId, entry.MessageId); Assert.Equal("baz", entry.Key); Assert.Equal("dummy", entry.Payload); //Assert.Equal(DateTime.Now, entry.OccurredOnUtc); // TODO -- should probably be testable Assert.Null(entry.ProcessedUtc); }
public async Task Can_produce_outbox_message() { var spy = new KafkaProducerSpy(); var services = new ServiceCollection(); var fake = new FakeOutboxPersistence(); var dummyNotification = new DummyNotification(); var messageId = Guid.NewGuid().ToString(); services.AddLogging(); services.AddOutbox(options => { options.WithMessageIdGenerator(new MessageIdGeneratorStub(() => messageId)); options.Register <DummyMessage>("foo", "bar", x => "baz"); options.WithOutboxMessageRepository(serviceProvider => fake); options.WithNotifier(serviceProvider => dummyNotification); }); services.AddOutboxProducer(options => { options.WithBootstrapServers("localhost"); options.WithKafkaProducerFactory(() => spy); options.WithUnitOfWorkFactory(serviceProvider => fake); options.WithNotification(serviceProvider => dummyNotification); }); var provider = services.BuildServiceProvider(); var outbox = provider.GetRequiredService <OutboxQueue>(); await outbox.Enqueue(new[] { new DummyMessage() }); var pollingPublisher = provider .GetServices <IHostedService>() .OfType <OutboxDispatcherHostedService>() .First(); using (var cts = new CancellationTokenSource()) { cts.CancelAfter(10); pollingPublisher.ProcessOutbox(cts.Token); } Assert.True(fake.OutboxMessages.All(x => x.ProcessedUtc.HasValue)); Assert.True(fake.Committed); Assert.Equal("foo", spy.LastMessage.Topic); Assert.Equal(messageId, spy.LastMessage.MessageId); Assert.Equal("bar", spy.LastMessage.Type); Assert.Equal("baz", spy.LastMessage.Key); // Assert.Equal("", spy.LastOutgoingMessage.Value); }
public async Task Can_produce_outbox_message() { var spy = new KafkaProducerSpy(); var services = new ServiceCollection(); var fake = new FakeOutboxPersistence(); var messageId = Guid.NewGuid().ToString(); services.AddLogging(); services.AddProducer(options => { options.WithBootstrapServers("localhost"); options.WithKafkaProducerFactory(new KafkaProducerFactoryStub(spy)); options.WithMessageIdGenerator(new MessageIdGeneratorStub(() => messageId)); options.Register <DummyMessage>("foo", "bar", x => "baz"); options.AddOutbox(opt => { opt.WithOutboxMessageRepository(serviceProvider => fake); opt.WithOutboxPublisher(pub => { pub.WithUnitOfWorkFactory(serviceProvider => fake); }); }); }); var provider = services.BuildServiceProvider(); var outbox = provider.GetRequiredService <IOutbox>(); await outbox.Enqueue(new[] { new DummyMessage(), }); var pollingPublisher = provider.GetServices <IHostedService>().FirstOrDefault(x => x is PollingPublisher); using (CancellationTokenSource cts = new CancellationTokenSource()) { cts.CancelAfter(500); await pollingPublisher.StartAsync(cts.Token); } Assert.True(fake.OutboxMessages.All(x => x.ProcessedUtc.HasValue)); Assert.Equal("foo", spy.LastMessage.Topic); Assert.Equal(messageId, spy.LastMessage.MessageId); Assert.Equal("bar", spy.LastMessage.Type); Assert.Equal("baz", spy.LastMessage.Key); // Assert.Equal("", spy.LastOutgoingMessage.Value); }
public async Task Can_persist_outbox_message_with_special_serializer_format() { var services = new ServiceCollection(); var fake = new FakeOutboxPersistence(); services.AddOutbox(options => { options.Register <DummyMessage>("foo", "bar", x => "baz"); options.WithOutboxEntryRepository(serviceProvider => fake); options.WithPayloadSerializer("foo", new PayloadSerializerStub("dummy", "expected payload format")); }); var provider = services.BuildServiceProvider(); var outbox = provider.GetRequiredService <OutboxQueue>(); await outbox.Enqueue(new[] { new DummyMessage() }); var entry = fake.OutboxEntries.Single(); Assert.Equal("dummy", entry.Payload); }
public FakeUnitOfWork(FakeOutboxPersistence fake) { _fake = fake; }