public async Task expected_order_of_handler_invocation_in_unit_of_work() { var orderOfInvocation = new LinkedList <string>(); var dummyMessageResult = new MessageResultBuilder().WithTransportLevelMessage(new TransportLevelMessageBuilder().WithType("foo").Build()).Build(); var dummyMessageRegistration = new MessageRegistrationBuilder().WithMessageType("foo").Build(); var registry = new MessageHandlerRegistry(); registry.Register(dummyMessageRegistration); var sut = new ConsumerBuilder() .WithUnitOfWork(new UnitOfWorkSpy( handlerInstance: new MessageHandlerSpy <FooMessage>(() => orderOfInvocation.AddLast("during")), pre: () => orderOfInvocation.AddLast("before"), post: () => orderOfInvocation.AddLast("after") )) .WithConsumerScopeFactory(new ConsumerScopeFactoryStub(new ConsumerScopeStub(dummyMessageResult))) .WithMessageHandlerRegistry(registry) .Build(); await sut.ConsumeSingle(CancellationToken.None); Assert.Equal(new[] { "before", "during", "after" }, orderOfInvocation); }
public async Task disposes_consumer_scope_when_consuming_single_message() { var messageResultStub = new MessageResultBuilder().WithTransportLevelMessage(new TransportLevelMessageBuilder().WithType("foo").Build()).Build(); var handlerStub = Dummy.Of <IMessageHandler <FooMessage> >(); var messageRegistrationStub = new MessageRegistrationBuilder() .WithHandlerInstanceType(handlerStub.GetType()) .WithMessageInstanceType(typeof(FooMessage)) .WithMessageType("foo") .Build(); var spy = new ConsumerScopeSpy(messageResultStub); var registry = new MessageHandlerRegistry(); registry.Register(messageRegistrationStub); var consumer = new ConsumerBuilder() .WithConsumerScopeFactory(new ConsumerScopeFactoryStub(spy)) .WithUnitOfWork(new UnitOfWorkStub(handlerStub)) .WithMessageHandlerRegistry(registry) .Build(); await consumer.ConsumeSingle(CancellationToken.None); Assert.Equal(1, spy.Disposed); }
public async Task will_not_call_commit_when_auto_commit_is_enabled() { var handlerStub = Dummy.Of <IMessageHandler <FooMessage> >(); var messageRegistrationStub = new MessageRegistrationBuilder() .WithHandlerInstanceType(handlerStub.GetType()) .WithMessageInstanceType(typeof(FooMessage)) .WithMessageType("foo") .Build(); var wasCalled = false; var resultSpy = new MessageResultBuilder() .WithOnCommit(() => { wasCalled = true; return(Task.CompletedTask); }) .Build(); var topicSubscriberScopeFactoryStub = new TopicSubscriberScopeFactoryStub(new TopicSubscriberScopeStub(resultSpy)); var consumer = new ConsumerBuilder() .WithTopicSubscriberScopeFactory(topicSubscriberScopeFactoryStub) .WithUnitOfWorkFactory(x => new UnitOfWorkStub(handlerStub)) .WithMessageRegistrations(messageRegistrationStub) .WithEnableAutoCommit(true) .Build(); await consumer.ConsumeSingle(CancellationToken.None); Assert.False(wasCalled); }
public async Task throws_when_consuming_an_unknown_message_when_explicit_handlers_are_required() { var sut = new ConsumerBuilder().Build(); await Assert.ThrowsAsync <MissingMessageHandlerRegistrationException>( () => sut.ConsumeSingle(CancellationToken.None)); }
public async Task Has_expected_number_of_creations_and_disposals_when_singleton() { var dummyMessage = new DummyMessage(); var messageStub = new TransportLevelMessageBuilder() .WithType(nameof(DummyMessage)) .WithData(dummyMessage) .Build(); var messageResult = new MessageResultBuilder() .WithTransportLevelMessage(messageStub) .Build(); var services = new ServiceCollection(); services.AddTransient <Repository>(); services.AddSingleton <IHostApplicationLifetime, DummyApplicationLifetime>(); services.AddTransient <DummyMessageHandler>(); services.AddLogging(); var createCount = 0; var disposeCount = 0; services.AddSingleton <ScopeSpy>(provider => new ScopeSpy(onCreate: () => createCount++, onDispose: () => disposeCount++)); var serviceProvider = services.BuildServiceProvider(); var consumerConfiguration = new ConsumerConfigurationBuilder() .WithGroupId("dummy") .WithBootstrapServers("dummy") .RegisterMessageHandler <DummyMessage, DummyMessageHandler>("dummyTopic", nameof(DummyMessage)) .WithConsumerScopeFactory(_ => new ConsumerScopeFactoryStub(new ConsumerScopeStub(messageResult))) .WithUnitOfWorkFactory(new ServiceProviderUnitOfWorkFactory(serviceProvider)) .Build(); var consumer = new ConsumerBuilder() .WithMessageHandlerRegistry(consumerConfiguration.MessageHandlerRegistry) .WithUnitOfWorkFactory(consumerConfiguration.UnitOfWorkFactory) .WithConsumerScopeFactory(consumerConfiguration.ConsumerScopeFactory) .WithEnableAutoCommit(consumerConfiguration.EnableAutoCommit) .Build(); await consumer.ConsumeSingle(CancellationToken.None); await consumer.ConsumeSingle(CancellationToken.None); Assert.Equal(1, createCount); Assert.Equal(0, disposeCount); }
public async Task does_not_throw_when_consuming_an_unknown_message_with_no_op_strategy() { var sut = new ConsumerBuilder() .WithUnitOfWork( new UnitOfWorkStub( new NoOpHandler(new Mock <ILogger <NoOpHandler> >().Object))) .WithUnconfiguredMessageStrategy(new UseNoOpHandler()) .Build(); await sut.ConsumeSingle(CancellationToken.None); }
public async Task invokes_expected_handler_when_consuming() { var handlerMock = new Mock <IMessageHandler <FooMessage> >(); var handlerStub = handlerMock.Object; var messageRegistrationStub = new MessageRegistrationBuilder() .WithHandlerInstanceType(handlerStub.GetType()) .WithMessageInstanceType(typeof(FooMessage)) .WithMessageType("foo") .Build(); var sut = new ConsumerBuilder() .WithUnitOfWorkFactory(type => new UnitOfWorkStub(handlerStub)) .WithMessageRegistrations(messageRegistrationStub) .Build(); await sut.ConsumeSingle(CancellationToken.None); handlerMock.Verify(x => x.Handle(It.IsAny <FooMessage>()), Times.Once); }
public async Task throws_expected_exception_when_consuming_a_message_without_a_handler_as_been_registered_for_it() { var sut = new ConsumerBuilder().Build(); await Assert.ThrowsAsync <MissingMessageHandlerRegistrationException>(() => sut.ConsumeSingle(CancellationToken.None)); }