示例#1
0
        public void InterceptorWithException()
        {
            var channel     = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default);
            var message     = Message.Create("foo");
            var mockHandler = new Mock <IMessageHandler>();

            var expected = new InvalidOperationException("Fake exception");
            var latch    = new CountdownEvent(2);

            mockHandler.Setup((h) => h.HandleMessage(message)).Throws(expected);
            var interceptor = new BeforeHandleInterceptor(latch);

            channel.AddInterceptor(interceptor);
            channel.Subscribe(mockHandler.Object);

            try
            {
                channel.Send(message);
            }
            catch (MessageDeliveryException actual)
            {
                Assert.Same(expected, actual.InnerException);
            }

            Assert.True(latch.Wait(3000));
            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.AfterHandledInvoked);
        }
示例#2
0
        public void InterceptorWithModifiedMessage()
        {
            var channel = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default);

            var mockHandler  = new Mock <IMessageHandler>();
            var mockExpected = new Mock <IMessage>();
            var latch        = new CountdownEvent(2);
            var interceptor  = new BeforeHandleInterceptor(latch);

            interceptor.MessageToReturn = mockExpected.Object;
            channel.AddInterceptor(interceptor);
            channel.Subscribe(mockHandler.Object);
            channel.Send(Message.Create("foo"));
            Assert.True(latch.Wait(10000));
            mockHandler.Verify((h) => h.HandleMessage(mockExpected.Object));
            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.AfterHandledInvoked);
        }