public void VerifyFailoverWithLoadBalancing() { var numberOfMessages = 12; var channel = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default); var latch = new CountdownEvent(numberOfMessages); var handler1 = new TestHandler(latch); var handler2 = new TestHandler(latch); var handler3 = new TestHandler(latch); channel.Subscribe(handler1); channel.Subscribe(handler2); channel.Subscribe(handler3); handler2.ShouldFail = true; for (var i = 0; i < numberOfMessages; i++) { channel.Send(Message.Create("test-" + i)); } Assert.True(latch.Wait(3000)); Assert.NotNull(handler1.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler1.Thread.ManagedThreadId); Assert.NotNull(handler2.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler2.Thread.ManagedThreadId); Assert.NotNull(handler3.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler3.Thread.ManagedThreadId); Assert.Equal(4, handler1.Count); Assert.Equal(0, handler2.Count); Assert.Equal(8, handler3.Count); }
public void HandlerThrowsExceptionExecutorChannel() { services.AddSingleton <IDestinationResolver <IMessageChannel>, DefaultMessageChannelDestinationResolver>(); services.AddSingleton <IMessageBuilderFactory, DefaultMessageBuilderFactory>(); services.AddSingleton <IMessageChannel>((p) => new DirectChannel(p.GetService <IApplicationContext>(), "errorChannel")); var provider = services.BuildServiceProvider(); var defaultErrorChannel = provider.GetService <IMessageChannel>() as DirectChannel; var channel = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default); var resultHandler = new ResultHandler(latch); var throwingHandler = new ThrowMessageExceptionHandler(); channel.Subscribe(throwingHandler); defaultErrorChannel.Subscribe(resultHandler); var message = IntegrationMessageBuilder.WithPayload("test").Build(); channel.Send(message); Assert.True(latch.Wait(10000)); var errorMessage = resultHandler.LastMessage; Assert.IsType <MessagingException>(errorMessage.Payload); var exceptionPayload = (MessagingException)errorMessage.Payload; Assert.IsType <NotSupportedException>(exceptionPayload.InnerException); Assert.Same(message, exceptionPayload.FailedMessage); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, resultHandler.LastThread.ManagedThreadId); }
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 void RoundRobinLoadBalancing() { var numberOfMessages = 12; var channel = new TaskSchedulerChannel(provider, TaskScheduler.Default); var latch = new CountdownEvent(numberOfMessages); var handler1 = new TestHandler(latch); var handler2 = new TestHandler(latch); var handler3 = new TestHandler(latch); channel.Subscribe(handler1); channel.Subscribe(handler2); channel.Subscribe(handler3); for (var i = 0; i < numberOfMessages; i++) { channel.Send(new GenericMessage("test-" + i)); } Assert.True(latch.Wait(3000)); Assert.NotNull(handler1.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler1.Thread.ManagedThreadId); Assert.NotNull(handler2.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler2.Thread.ManagedThreadId); Assert.NotNull(handler3.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler3.Thread.ManagedThreadId); Assert.Equal(4, handler1.Count); Assert.Equal(4, handler2.Count); Assert.Equal(4, handler3.Count); }
public void VerifyDifferentThread() { var channel = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default); var latch = new CountdownEvent(1); var handler = new TestHandler(latch); channel.Subscribe(handler); channel.Send(Message.Create("test")); Assert.True(latch.Wait(1000)); Assert.NotNull(handler.Thread); Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, handler.Thread.ManagedThreadId); }
public void FailoverNoLoadBalancingWithExecutorConcurrent() { var channel = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default, null, null); channel.Failover = true; var dispatcher = channel.Dispatcher; dispatcher.AddHandler(handlerA.Object); dispatcher.AddHandler(handlerB.Object); dispatcher.AddHandler(handlerC.Object); handlerA.Setup((h) => h.HandleMessage(message)).Callback(() => { failed = 1; var e = new Exception(); throw e; }); handlerB.Setup((h) => h.HandleMessage(message)).Callback(() => { allDone.Signal(); }); void MessageSenderTask() { try { start.Wait(); } catch (Exception) { throw; } channel.Send(message); } for (var i = 0; i < TOTAL_EXECUTIONS; i++) { Task.Run(MessageSenderTask); } start.Signal(); Assert.True(allDone.Wait(10000)); handlerA.Verify((h) => h.HandleMessage(message), Times.Exactly(TOTAL_EXECUTIONS)); handlerB.Verify((h) => h.HandleMessage(message), Times.Exactly(TOTAL_EXECUTIONS)); handlerC.Verify((h) => h.HandleMessage(message), Times.Never()); }
public void NoFailoverNoLoadBalancingWithExecutorConcurrent() { var channel = new TaskSchedulerChannel(provider, TaskScheduler.Default, null, "noLoadBalancerNoFailoverExecutor", null); channel.Failover = false; handlerA.Setup((h) => h.HandleMessage(message)).Callback(() => { var e = new Exception(); allDone.Signal(); failed = 1; exceptionRegistry.Object.Add(e); throw e; }); var dispatcher = channel.Dispatcher; dispatcher.AddHandler(handlerA.Object); dispatcher.AddHandler(handlerB.Object); void MessageSenderTask() { try { start.Wait(); } catch (Exception) { throw; } channel.Send(message); } for (var i = 0; i < TOTAL_EXECUTIONS; i++) { Task.Run(MessageSenderTask); } start.Signal(); Assert.True(allDone.Wait(10000)); Assert.Equal(1, failed); handlerA.Verify((h) => h.HandleMessage(message), Times.Exactly(TOTAL_EXECUTIONS)); handlerB.Verify((h) => h.HandleMessage(message), Times.Exactly(0)); exceptionRegistry.Verify((list) => list.Add(It.IsAny <Exception>()), Times.Exactly(TOTAL_EXECUTIONS)); }
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 NoFailoverLoadBalancingWithExecutorConcurrent() { var channel = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default, null); channel.Failover = false; var dispatcher = channel.Dispatcher; dispatcher.AddHandler(handlerA.Object); dispatcher.AddHandler(handlerB.Object); dispatcher.AddHandler(handlerC.Object); var start1 = new CountdownEvent(1); var allDone1 = new CountdownEvent(TOTAL_EXECUTIONS); var message2 = message; var failed1 = 0; handlerA.Setup((h) => h.HandleMessage(message)).Callback(() => { failed1 = 1; var e = new Exception(); exceptionRegistry.Object.Add(e); allDone1.Signal(); throw e; }); handlerB.Setup((h) => h.HandleMessage(message)).Callback(() => { allDone1.Signal(); }); handlerC.Setup((h) => h.HandleMessage(message)).Callback(() => { allDone1.Signal(); }); void MessageSenderTask() { try { start1.Wait(); } catch (Exception) { throw; } channel.Send(message); } for (var i = 0; i < TOTAL_EXECUTIONS; i++) { Task.Run(MessageSenderTask); } start1.Signal(); Assert.True(allDone1.Wait(10000)); Assert.Equal(1, failed1); handlerA.Verify((h) => h.HandleMessage(message2), Times.Exactly(14)); handlerB.Verify((h) => h.HandleMessage(message2), Times.Exactly(13)); handlerC.Verify((h) => h.HandleMessage(message2), Times.Exactly(13)); exceptionRegistry.Verify((list) => list.Add(It.IsAny <Exception>()), Times.Exactly(14)); }