Example #1
0
 public ConsumerConfigurationBuilder RegisterMessageHandler <TMessage, TMessageHandler>(string topic, string messageType)
     where TMessage : class, new()
     where TMessageHandler : IMessageHandler <TMessage>
 {
     _messageHandlerRegistry.Register <TMessage, TMessageHandler>(topic, messageType);
     return(this);
 }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        private static TopicSubscriber CreateSubscriber()
        {
            var factory = new ConsumerFactory();

            var handlerRegistry = new MessageHandlerRegistry();

            handlerRegistry.Register <PositionMessage, MessageHandler>("p-project.tracking.vehicles", "vehicle_position_changed");

            var typeResolver = new HandRolledResolver();
            var dispatcher   = new LocalMessageDispatcher(handlerRegistry, typeResolver);

            return(new TopicSubscriber(factory, dispatcher));
        }
Example #5
0
        public void returns_expected_registrations_when_registering_single_handler()
        {
            var sut = new MessageHandlerRegistry();

            var expected = new MessageRegistrationBuilder().Build();

            var result = sut.Register(
                handlerInstanceType: expected.HandlerInstanceType,
                messageInstanceType: expected.MessageInstanceType,
                topic: expected.Topic,
                messageType: expected.MessageType
                );

            Assert.Equal(expected, result, new MessageRegistrationComparer());
        }
Example #6
0
        public async Task handler_exceptions_are_thrown_as_expected()
        {
            var transportMessageDummy = new TransportLevelMessageBuilder().WithType("foo").Build();
            var registrationDummy     = new MessageRegistrationBuilder().WithMessageType("foo").Build();
            var registry = new MessageHandlerRegistry();

            registry.Register(registrationDummy);

            var sut = new LocalMessageDispatcherBuilder()
                      .WithMessageHandlerRegistry(registry)
                      .WithHandlerUnitOfWork(new UnitOfWorkStub(new ErroneusHandler()))
                      .Build();

            await Assert.ThrowsAsync <ExpectedException>(() => sut.Dispatch(transportMessageDummy));
        }
Example #7
0
        public async Task throws_expected_exception_when_dispatching_and_unable_to_resolve_handler_instance()
        {
            var transportMessageStub    = new TransportLevelMessageBuilder().WithType("foo").Build();
            var messageRegistrationStub = new MessageRegistrationBuilder().WithMessageType("foo").Build();
            var registry = new MessageHandlerRegistry();

            registry.Register(messageRegistrationStub);

            var sut = new LocalMessageDispatcherBuilder()
                      .WithMessageHandlerRegistry(registry)
                      .WithHandlerUnitOfWorkFactory(new HandlerUnitOfWorkFactoryStub(null))
                      .Build();

            await Assert.ThrowsAsync <UnableToResolveUnitOfWorkForHandlerException>(() => sut.Dispatch(transportMessageStub));
        }
Example #8
0
        public async Task creates_consumer_scope_when_consuming_multiple_messages()
        {
            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();

            using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
            {
                var loops = 0;

                var subscriberScopeStub = new ConsumerScopeDecoratorWithHooks(
                    inner: new ConsumerScopeStub(messageResultStub),
                    postHook: () =>
                {
                    loops++;

                    if (loops == 2)
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
                    );

                var spy = new ConsumerScopeFactorySpy(subscriberScopeStub);

                var registry = new MessageHandlerRegistry();
                registry.Register(messageRegistrationStub);

                var consumer = new ConsumerBuilder()
                               .WithConsumerScopeFactory(spy)
                               .WithUnitOfWork(new UnitOfWorkStub(handlerStub))
                               .WithMessageHandlerRegistry(registry)
                               .Build();

                await consumer.ConsumeAll(cancellationTokenSource.Token);

                Assert.Equal(2, loops);
                Assert.Equal(1, spy.CreateConsumerScopeCalled);
            }
        }
Example #9
0
        public async Task handler_is_invoked_as_expected_when_dispatching()
        {
            var mock = new Mock <IMessageHandler <object> >();

            var transportMessageDummy = new TransportLevelMessageBuilder().WithType("foo").Build();
            var registrationDummy     = new MessageRegistrationBuilder().WithMessageType("foo").Build();
            var registry = new MessageHandlerRegistry();

            registry.Register(registrationDummy);

            var sut = new LocalMessageDispatcherBuilder()
                      .WithMessageHandlerRegistry(registry)
                      .WithHandlerUnitOfWork(new UnitOfWorkStub(mock.Object))
                      .Build();

            await sut.Dispatch(transportMessageDummy);

            mock.Verify(x => x.Handle(It.IsAny <object>(), It.IsAny <MessageHandlerContext>()), Times.Once);
        }
Example #10
0
        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 consumerScopeFactoryStub = new ConsumerScopeFactoryStub(new ConsumerScopeStub(resultSpy));
            var registry = new MessageHandlerRegistry();

            registry.Register(messageRegistrationStub);

            var consumer = new ConsumerBuilder()
                           .WithConsumerScopeFactory(consumerScopeFactoryStub)
                           .WithUnitOfWork(new UnitOfWorkStub(handlerStub))
                           .WithMessageHandlerRegistry(registry)
                           .WithEnableAutoCommit(true)
                           .Build();

            await consumer.ConsumeSingle(CancellationToken.None);

            Assert.False(wasCalled);
        }
Example #11
0
        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 registry = new MessageHandlerRegistry();

            registry.Register(messageRegistrationStub);

            var sut = new ConsumerBuilder()
                      .WithUnitOfWork(new UnitOfWorkStub(handlerStub))
                      .WithMessageHandlerRegistry(registry)
                      .Build();

            await sut.ConsumeSingle(CancellationToken.None);

            handlerMock.Verify(x => x.Handle(It.IsAny <FooMessage>(), It.IsAny <MessageHandlerContext>()), Times.Once);
        }