コード例 #1
0
        public async Task WhenProcessMessageIsCalledWithMessageATheMessageAHandlerIsCalled()
        {
            IEnumerable <Type> MessageHandlerTypes()
            {
                return(new []
                {
                    typeof(HandlerA),
                    typeof(HandlerB),
                    typeof(NotAHandler)
                });
            }

            var scopeFactory = A.Fake <IServiceScopeFactory>();
            var scope        = A.Fake <IServiceScope>();
            var sp           = A.Fake <IServiceProvider>();
            var handler      = A.Fake <HandlerA>();

            A.CallTo(() => handler.Handle(A <MessageA> ._)).Returns(Task.FromResult(HandlerResult.Success()));

            A.CallTo(() => sp.GetService(A <Type> .That.IsEqualTo(typeof(HandlerA)))).Returns(handler);

            A.CallTo(() => scopeFactory.CreateScope()).Returns(scope);
            A.CallTo(() => scope.ServiceProvider).Returns(sp);

            var client   = A.Fake <IReceiverClient>();
            var registry = new MessageHandlerRegistry(MessageHandlerTypes);
            var sut      = new MessageDispatcher(scopeFactory, client, registry, new LogCorrelationHandler(false));

            await sut.ProcessMessage(typeof(MessageA).FullName, "{aProp1: \"hello\"}", () => Task.CompletedTask, m => Task.CompletedTask);

            A.CallTo(() => handler.Handle(A <MessageA> .That.Matches(m => m.AProp1 == "hello")))
            .MustHaveHappenedOnceExactly();
        }
コード例 #2
0
        public async Task WhenOneHandlerReturnsAbortMessageIsAborted()
        {
            IEnumerable <Type> MessageHandlerTypes()
            {
                return(new []
                {
                    typeof(HandlerA1),
                    typeof(HandlerA2)
                });
            }

            var scopeFactory   = A.Fake <IServiceScopeFactory>();
            var scope          = A.Fake <IServiceScope>();
            var sp             = A.Fake <IServiceProvider>();
            var successHandler = A.Fake <IHandleMessage <MessageA> >();
            var abortHandler   = A.Fake <IHandleMessage <MessageA> >();

            A.CallTo(() => sp.GetService(A <Type> .That.IsEqualTo(typeof(HandlerA1)))).Returns(successHandler);
            A.CallTo(() => sp.GetService(A <Type> .That.IsEqualTo(typeof(HandlerA2)))).Returns(abortHandler);

            A.CallTo(() => successHandler.Handle(A <MessageA> ._)).Returns(Task.FromResult(HandlerResult.Success()));
            A.CallTo(() => abortHandler.Handle(A <MessageA> ._)).Returns(Task.FromResult(HandlerResult.Abort("something bad happened")));

            A.CallTo(() => scopeFactory.CreateScope()).Returns(scope);
            A.CallTo(() => scope.ServiceProvider).Returns(sp);

            var client   = A.Fake <IReceiverClient>();
            var registry = new MessageHandlerRegistry(MessageHandlerTypes);
            var sut      = new MessageDispatcher(scopeFactory, client, registry, new LogCorrelationHandler(false));

            var isCompleted = false;
            var isAborted   = false;
            var message     = "";
            await sut.ProcessMessage(typeof(MessageA).FullName, "{aProp1: \"hello\"}", () =>
            {
                isCompleted = true;
                return(Task.CompletedTask);
            }, m =>
            {
                message   = m;
                isAborted = true;
                return(Task.CompletedTask);
            });

            Assert.False(isCompleted, "the markComplete callback was called");
            Assert.True(isAborted, "the abort callback was not called");
            Assert.Equal("something bad happened", message);
        }
コード例 #3
0
        public async Task WhenHandlerHandlesAandB_And_BMessage_Received_Only_B_Method_is_Called()
        {
            IEnumerable <Type> MessageHandlerTypes()
            {
                return(new []
                {
                    typeof(DuoHandler)
                });
            }

            var scopeFactory = A.Fake <IServiceScopeFactory>();
            var scope        = A.Fake <IServiceScope>();
            var sp           = A.Fake <IServiceProvider>();
            var duoHandler   = A.Fake <DuoHandler>();

            A.CallTo(() => sp.GetService(A <Type> .That.IsEqualTo(typeof(DuoHandler)))).Returns(duoHandler);

            A.CallTo(() => duoHandler.Handle(A <MessageA> ._)).Returns(Task.FromResult(HandlerResult.Success()));
            A.CallTo(() => duoHandler.Handle(A <MessageB> ._)).Returns(Task.FromResult(HandlerResult.Success()));

            A.CallTo(() => scopeFactory.CreateScope()).Returns(scope);
            A.CallTo(() => scope.ServiceProvider).Returns(sp);

            var client   = A.Fake <IReceiverClient>();
            var registry = new MessageHandlerRegistry(MessageHandlerTypes);
            var sut      = new MessageDispatcher(scopeFactory, client, registry, new LogCorrelationHandler(false));

            var isCompleted = false;
            var isAborted   = false;
            var message     = "";
            await sut.ProcessMessage(typeof(MessageB).FullName, "{aProp1: \"hello\"}", () =>
            {
                isCompleted = true;
                return(Task.CompletedTask);
            }, m =>
            {
                message   = m;
                isAborted = true;
                return(Task.CompletedTask);
            });

            Assert.True(isCompleted, "the markComplete callback was called");
            Assert.False(isAborted, "the abort callback was not called");
            Assert.Equal("", message);
            A.CallTo(() => duoHandler.Handle(A <MessageA> ._)).MustNotHaveHappened();
            A.CallTo(() => duoHandler.Handle(A <MessageB> ._)).MustHaveHappenedOnceExactly();
        }
コード例 #4
0
 public virtual Task <HandlerResult> Handle(MessageA message)
 {
     return(Task.FromResult(HandlerResult.Success()));
 }