コード例 #1
0
ファイル: Program.cs プロジェクト: winhu/Steeltoe
    public void PublishSubscribeChannel_Send_10_000_000_TwoHandlers()
    {
        var services = new ServiceCollection();

        services.AddSingleton <IIntegrationServices, IntegrationServices>();
        var provider = services.BuildServiceProvider();
        var channel  = new PublishSubscribeChannel(provider);
        var handler1 = new CounterHandler();
        var handler2 = new CounterHandler();

        channel.Subscribe(handler1);
        channel.Subscribe(handler2);
        var message = new GenericMessage("test");

        for (var i = 0; i < 10_000_000; i++)
        {
            channel.Send(message);
        }
        if (handler1.Count != 10_000_000)
        {
            throw new InvalidOperationException("Handler count1 invalid");
        }
        if (handler2.Count != 10_000_000)
        {
            throw new InvalidOperationException("Handler count2 invalid");
        }
    }
コード例 #2
0
 private static void SimpleMessageHandlerEventing()
 {
     PublishSubscribeChannel channel = new PublishSubscribeChannel();
     channel.Subscribe(new SimpleMessageHandler("one"));
     channel.Subscribe(new SimpleMessageHandler("two"));
     IMessage message = MessageBuilder.WithPayload("foo").Build();
     channel.Send(message);
 }
コード例 #3
0
        private static void SimpleMessageHandlerEventing()
        {
            PublishSubscribeChannel channel = new PublishSubscribeChannel();

            channel.Subscribe(new SimpleMessageHandler("one"));
            channel.Subscribe(new SimpleMessageHandler("two"));
            IMessage message = MessageBuilder.WithPayload("foo").Build();

            channel.Send(message);
        }
コード例 #4
0
        public void UsingPublishSubscribeChannels()
        {
            PublishSubscribeChannel channel = new PublishSubscribeChannel();

            channel.Subscribe(new SimpleMessageHandler("Handler-1"));
            channel.Subscribe(new SimpleMessageHandler("Handler-2"));

            IMessage diningMessage =
                MessageBuilder.WithPayload("Hello World").Build();

            channel.Send(diningMessage);
            channel.Send(diningMessage);
            channel.Send(diningMessage);
        }
コード例 #5
0
        public void HandlerThrowsExceptionPublishSubscribeWithExecutor()
        {
            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 PublishSubscribeChannel(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);
        }
コード例 #6
0
        public async Task ValidateSuccessfulErrorFlowDoesNotThrowErrors()
        {
            var provider   = services.BuildServiceProvider();
            var outChannel = new DirectChannel(provider);
            var handler    = new ExceptionHandler();

            outChannel.Subscribe(handler);
            var errorChannel = new PublishSubscribeChannel(provider);
            var errorService = new SuccessfulErrorService();
            var errorHandler = new ServiceActivatingHandler(provider, errorService);

            errorChannel.Subscribe(errorHandler);

            var mps = new TestMessageProducerSupportEndpoint(provider);

            mps.OutputChannel = outChannel;
            mps.ErrorChannel  = errorChannel;

            await mps.Start();

            var message = new GenericMessage("hello");

            mps.SendMessage(message);
            Assert.IsType <ErrorMessage>(errorService.LastMessage);
            Assert.IsType <MessageDeliveryException>(errorService.LastMessage.Payload);
            var exception = (MessageDeliveryException)errorService.LastMessage.Payload;

            Assert.Equal(message, exception.FailedMessage);
        }
コード例 #7
0
        public void HandlerThrowsExceptionPublishSubscribeWithoutScheduler()
        {
            var provider = services.BuildServiceProvider();
            var channel  = new PublishSubscribeChannel(provider.GetService <IApplicationContext>());
            var handler  = new ThrowingHandler();

            channel.Subscribe(handler);
            var message = IntegrationMessageBuilder.WithPayload("test").Build();

            Assert.Throws <MessageDeliveryException>(() => channel.Send(message));
        }
コード例 #8
0
        public async ValueTask TestSendAsyncTwoHandler_10_000_000()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IIntegrationServices, IntegrationServices>();
            var provider = services.BuildServiceProvider();
            var handler1 = new CounterHandler();
            var handler2 = new CounterHandler();
            var channel  = new PublishSubscribeChannel(provider);

            channel.Subscribe(handler1);
            channel.Subscribe(handler2);
            var message = new GenericMessage("test");

            for (var i = 0; i < 10000000; i++)
            {
                await channel.SendAsync(message);
            }

            Assert.Equal(10000000, handler1.Count);
            Assert.Equal(10000000, handler2.Count);
        }
コード例 #9
0
        public void HandlerThrowsExceptionPublishSubscribeWithoutScheduler()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IIntegrationServices, IntegrationServices>();
            var provider = services.BuildServiceProvider();
            var channel  = new PublishSubscribeChannel(provider);
            var handler  = new ThrowingHandler();

            channel.Subscribe(handler);
            var message = MessageBuilder.WithPayload("test").Build();

            Assert.Throws <MessageDeliveryException>(() => channel.Send(message));
        }
コード例 #10
0
        public async ValueTask TestSendAsync()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IIntegrationServices, IntegrationServices>();
            var provider = services.BuildServiceProvider();
            var handler  = new CounterHandler();
            var channel  = new PublishSubscribeChannel(provider);

            channel.Subscribe(handler);
            var message = new GenericMessage("test");

            Assert.True(await channel.SendAsync(message));
            Assert.Equal(1, handler.Count);
        }
コード例 #11
0
        public void TestSend()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IIntegrationServices, IntegrationServices>();
            var provider = services.BuildServiceProvider();
            var handler  = new CounterHandler();
            var channel  = new PublishSubscribeChannel(provider.GetService <IApplicationContext>());

            channel.Subscribe(handler);
            var message = Message.Create("test");

            Assert.True(channel.Send(message));
            Assert.Equal(1, handler.Count);
        }
コード例 #12
0
        public void TestSendOneHandler_10_000_000()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IIntegrationServices, IntegrationServices>();
            var provider = services.BuildServiceProvider();
            var handler  = new CounterHandler();
            var channel  = new PublishSubscribeChannel(provider);

            channel.Subscribe(handler);
            var message = new GenericMessage("test");

            Assert.True(channel.Send(message));
            for (var i = 0; i < 10000000; i++)
            {
                channel.Send(message);
            }

            Assert.Equal(10000001, handler.Count);
        }
コード例 #13
0
        public async ValueTask TestSendAsyncOneHandler_10_000_000()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IIntegrationServices, IntegrationServices>();
            var provider = services.BuildServiceProvider();
            var handler  = new CounterHandler();
            var channel  = new PublishSubscribeChannel(provider.GetService <IApplicationContext>());

            channel.Subscribe(handler);
            var message = Message.Create("test");

            Assert.True(await channel.SendAsync(message));
            for (var i = 0; i < 10000000; i++)
            {
                await channel.SendAsync(message);
            }

            Assert.Equal(10000001, handler.Count);
        }
コード例 #14
0
        public async Task ValidateExceptionIfSendToErrorChannelFails()
        {
            var provider   = services.BuildServiceProvider();
            var outChannel = new DirectChannel(provider);
            var handler    = new ExceptionHandler();

            outChannel.Subscribe(handler);
            var errorChannel = new PublishSubscribeChannel(provider);

            errorChannel.Subscribe(handler);

            var mps = new TestMessageProducerSupportEndpoint(provider);

            mps.OutputChannel = outChannel;
            mps.ErrorChannel  = errorChannel;

            await mps.Start();

            Assert.Throws <MessageDeliveryException>(() => mps.SendMessage(new GenericMessage("hello")));
        }
コード例 #15
0
    public void PublishSubscribeChannel_Send_10_000_000_SingleHandler()
    {
        var services = new ServiceCollection();

        services.AddSingleton <IIntegrationServices, IntegrationServices>();
        var provider = services.BuildServiceProvider();
        var channel  = new PublishSubscribeChannel(provider.GetService <IApplicationContext>());
        var handler  = new CounterHandler();

        channel.Subscribe(handler);
        var message = Message.Create("test");

        channel.Send(message);
        for (var i = 0; i < 10_000_000; i++)
        {
            channel.Send(message);
        }
        if (handler.Count != 10_000_000 + 1)
        {
            throw new InvalidOperationException("Handler count invalid");
        }
    }
        public void UsingPublishSubscribeChannels()
        {
            PublishSubscribeChannel channel = new PublishSubscribeChannel();

            channel.Subscribe(new SimpleMessageHandler("Handler-1"));
            channel.Subscribe(new SimpleMessageHandler("Handler-2"));

            IMessage diningMessage =
                MessageBuilder.WithPayload("Hello World").Build();

            channel.Send(diningMessage);
            channel.Send(diningMessage);
            channel.Send(diningMessage);
        }