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 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 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 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 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); }