Beispiel #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);
        }
        public async ValueTask InterceptorWithException()
        {
            var expected = new Exception("Fake exception");
            var mock     = new Mock <IMessageHandler>();

            mock.Setup(h => h.HandleMessage(_message)).Throws(expected);
            _handler = mock.Object;

            var interceptor = new BeforeHandleInterceptor();

            _channel.AddInterceptor(interceptor);
            _channel.Subscribe(_handler);
            var exceptionThrown = false;

            try
            {
                await _channel.Writer.WriteAsync(_message);
            }
            catch (MessageDeliveryException actual)
            {
                exceptionThrown = true;
                Assert.Same(expected, actual.InnerException);
            }

            Assert.True(exceptionThrown);
            mock.Verify(h => h.HandleMessage(_message), Times.Once);
            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.WasAfterHandledInvoked);
        }
        public void TryWriteWithInterceptors()
        {
            var mock = new Mock <IMessageHandler>();

            _handler = mock.Object;
            var interceptor = new BeforeHandleInterceptor();

            _channel.AddInterceptor(interceptor);
            _channel.Subscribe(_handler);
            Assert.True(_channel.Writer.TryWrite(_message));
            mock.Verify(h => h.HandleMessage(_message));
            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.WasAfterHandledInvoked);
        }
        public async ValueTask WriteAsyncWithoutScheduler()
        {
            var mock = new Mock <IMessageHandler>();

            _handler = mock.Object;
            var interceptor = new BeforeHandleInterceptor();

            _channel.AddInterceptor(interceptor);
            _channel.Subscribe(_handler);
            await _channel.Writer.WriteAsync(_message);

            mock.Verify(h => h.HandleMessage(_message));
            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.WasAfterHandledInvoked);
        }
        public void SendWithScheduler()
        {
            var mock = new Mock <IMessageHandler>();

            _handler = mock.Object;
            var interceptor = new BeforeHandleInterceptor();
            var scheduler   = new TestScheduler();
            var testChannel = new TaskSchedulerSubscribableChannel(scheduler);

            testChannel.AddInterceptor(interceptor);
            testChannel.Subscribe(_handler);
            testChannel.Send(_message);
            Assert.True(scheduler.WasTaskScheduled);
            mock.Verify(h => h.HandleMessage(_message));
            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.WasAfterHandledInvoked);
        }
        public void InterceptorWithNull()
        {
            var mock = new Mock <IMessageHandler>();

            _handler = mock.Object;
            var interceptor1 = new BeforeHandleInterceptor();
            var interceptor2 = new NullReturningBeforeHandleInterceptor();

            _channel.AddInterceptor(interceptor1);
            _channel.AddInterceptor(interceptor2);
            _channel.Subscribe(_handler);
            _channel.Send(_message);
            mock.Verify(h => h.HandleMessage(_message), Times.Never);
            Assert.Equal(1, interceptor1.Counter);
            Assert.Equal(1, interceptor2.Counter);
            Assert.True(interceptor1.WasAfterHandledInvoked);
        }
Beispiel #7
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);
        }
        public void InterceptorWithModifiedMessage()
        {
            var mock = new Mock <IMessageHandler>();

            _handler = mock.Object;
            var mock2    = new Mock <IMessage>();
            var expected = mock2.Object;

            var interceptor = new BeforeHandleInterceptor();

            interceptor.MessageToReturn = expected;
            _channel.AddInterceptor(interceptor);
            _channel.Subscribe(_handler);
            _channel.Send(_message);
            mock.Verify(h => h.HandleMessage(expected), Times.Once);

            Assert.Equal(1, interceptor.Counter);
            Assert.True(interceptor.WasAfterHandledInvoked);
        }