Exemplo n.º 1
0
        public async Task Given_MultipleEndpointsForSameServices_When_SendAsyncMultiple_SingleCommand_Assert_CallsSendOnBothEndpointClient()
        {
            var signal = new Subject <Unit>();

            var wrongEndpoint   = new FakeServiceEndpoint(typeof(TestServiceCommand1));
            var wrongCommands   = wrongEndpoint.Messages.TakeUntil(signal).ToList().ToTask();
            var correctEndpoint = new FakeServiceEndpoint(typeof(TestServiceCommand1));
            var correctCommands = correctEndpoint.Messages.TakeUntil(signal).ToList().ToTask();

            var serviceEndpointClients = new[] { wrongEndpoint, correctEndpoint };
            var serviceEndpoints       = Enumerable.Empty <IServiceEndpoint <IMessage, ICommand, IEvent, IRequest, IResponse> >();
            var sut = new ServiceBusClient <IMessage, ICommand, IEvent, IRequest, IResponse>(serviceEndpointClients, serviceEndpoints, new DefaultRequestCorrelationProvider());

            var cmd      = new TestServiceCommand1();
            var commands = new[] { cmd };

            await sut.SendAsync(commands);

            signal.OnNext(Unit.Default);

            var sentCommands1 = await wrongCommands;

            Assert.NotEmpty(sentCommands1);
            Assert.Contains(cmd, sentCommands1);
            var sendCommands2 = await correctCommands;

            Assert.NotEmpty(sendCommands2);
            Assert.Contains(cmd, sendCommands2);
        }
Exemplo n.º 2
0
        public void ShouldNotDeliverMessagesToSubscriberAfterSubscriptionDisposed()
        {
            FakeServiceEndpoint serviceEndpoint1 = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint serviceEndpoint2 = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint1)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint1)
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint2)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint2)
                .UsingConsoleLogging().Create();

            var subscriber = new FakeSubscriber();
            var testScheduler = new TestScheduler();
            var subscription = serviceBus.Subscribe(subscriber, testScheduler);

            serviceEndpoint1.Messages.OnNext(new TestServiceEvent1());
            testScheduler.AdvanceBy(1);

            subscription.Dispose();

            serviceEndpoint1.Messages.OnNext(new TestServiceEvent2());
            testScheduler.AdvanceBy(1);

            Assert.That(subscriber.Received.Count(), Is.EqualTo(1));
            Assert.That(subscriber.Received[0].GetType(), Is.EqualTo(typeof(TestServiceEvent1)));
        }
Exemplo n.º 3
0
        public void ShouldEmitSubscriberExceptionsOnExceptionObservable()
        {
            FakeServiceEndpoint serviceEndpoint1 = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint serviceEndpoint2 = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint1)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint1)
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint2)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint2)
                .UsingConsoleLogging().Create();

            ConcurrentBag<Exception> exceptions = new ConcurrentBag<Exception>();
            serviceBus.Exceptions.Subscribe(exceptions.Add);

            FakeSubscriber subscriber = new FakeSubscriber();

            var testScheduler = new TestScheduler();
            var subscription = serviceBus.Subscribe(subscriber, testScheduler);

            subscriber.ThrowExceptions = true;
            serviceBus.PublishAsync(new TestServiceEvent1());
            testScheduler.AdvanceBy(1);

            serviceBus.PublishAsync(new TestServiceEvent2());
            testScheduler.AdvanceBy(1);

            serviceBus.SendAsync(new TestServiceCommand1());
            testScheduler.AdvanceBy(1);

            serviceBus.SendAsync(new TestServiceCommand2());
            testScheduler.AdvanceBy(1);

            subscription.Dispose();

            Assert.That(exceptions.Count(), Is.EqualTo(4));
            Assert.That(subscriber.Received.Count(), Is.EqualTo(0));
        }
Exemplo n.º 4
0
        public void ShouldCatchAndHandleExceptionsThrownByEndpointObservables()
        {
            FakeServiceEndpoint erroringEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage1)) { ThrowException = true };
            FakeServiceEndpoint serviceEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpointClient) erroringEndpoint)
                .WithEndpoint((IServiceEndpoint)erroringEndpoint)
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint)
                .UsingConsoleLogging().Create();

            ConcurrentBag<IMessage> messages = new ConcurrentBag<IMessage>();
            ConcurrentBag<Exception> exceptions = new ConcurrentBag<Exception>();

            serviceBus.Events.Subscribe(messages.Add);
            serviceBus.Commands.Subscribe(messages.Add);
            serviceBus.Requests.Subscribe(messages.Add);
            serviceBus.Exceptions.Subscribe(exceptions.Add);

            // trigger exception
            serviceBus.PublishAsync(new TestServiceEvent1());

            TestServiceEvent2 message1 = new TestServiceEvent2();
            serviceBus.PublishAsync(message1);

            // trigger another exception
            serviceBus.PublishAsync(new TestServiceEvent1());

            TestServiceEvent2 message2 = new TestServiceEvent2();
            serviceBus.PublishAsync(message2);

            Assert.That(exceptions.Count(), Is.EqualTo(2));
            Assert.That(messages.Contains(message1), "message1 not received");
            Assert.That(messages.Contains(message2), "message2 not received");
        }
Exemplo n.º 5
0
        public void ShouldThrowExceptionIfSubscriberIsNull()
        {
            FakeServiceEndpoint serviceEndpoint1 = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint serviceEndpoint2 = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint1)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint1)
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint2)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint2)
                .UsingConsoleLogging().Create();

            serviceBus.Subscribe(null);
        }
Exemplo n.º 6
0
        public void ShouldSendAllMessagesToSubscribers()
        {
            FakeServiceEndpoint serviceEndpoint1 = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint serviceEndpoint2 = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint1)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint1)
                .WithEndpoint((IServiceEndpointClient) serviceEndpoint2)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint2)
                .UsingConsoleLogging()
                .Create();

            ConcurrentBag<Exception> exceptions = new ConcurrentBag<Exception>();
            serviceBus.Exceptions.Subscribe(exceptions.Add);

            FakeSubscriber subscriber = new FakeSubscriber();
            FakeSubscriber2 subscriber2 = new FakeSubscriber2();

            var testScheduler = new TestScheduler();
            var subscription = serviceBus.Subscribe(subscriber, testScheduler);
            var subscription2 = serviceBus.Subscribe(subscriber2, testScheduler);

            serviceBus.PublishAsync(new TestServiceEvent1());
            testScheduler.AdvanceBy(1);

            serviceBus.PublishAsync(new TestServiceEvent2());
            testScheduler.AdvanceBy(1);

            serviceBus.SendAsync(new TestServiceCommand1());
            testScheduler.AdvanceBy(1);

            serviceBus.SendAsync(new TestServiceCommand2());
            testScheduler.AdvanceBy(1);

            serviceEndpoint1.Messages.OnNext(new TestServiceRequest1());
            testScheduler.AdvanceBy(1);

            subscription.Dispose();
            subscription2.Dispose();

            Assert.That(exceptions.Count(), Is.EqualTo(0));
            Assert.That(subscriber.Received.Count(), Is.EqualTo(5));
            Assert.That(subscriber.Received[0].GetType(), Is.EqualTo(typeof(TestServiceEvent1)));
            Assert.That(subscriber.Received[1].GetType(), Is.EqualTo(typeof(TestServiceEvent2)));
            Assert.That(subscriber.Received[2].GetType(), Is.EqualTo(typeof(TestServiceCommand1)));
            Assert.That(subscriber.Received[3].GetType(), Is.EqualTo(typeof(TestServiceCommand2)));
            Assert.That(subscriber.Received[4].GetType(), Is.EqualTo(typeof(TestServiceRequest1)));

            Assert.That(subscriber2.Received.Count(), Is.EqualTo(5));
            Assert.That(subscriber2.Received[0].GetType(), Is.EqualTo(typeof(TestServiceEvent1)));
            Assert.That(subscriber2.Received[1].GetType(), Is.EqualTo(typeof(TestServiceEvent2)));
            Assert.That(subscriber2.Received[2].GetType(), Is.EqualTo(typeof(TestServiceCommand1)));
            Assert.That(subscriber2.Received[3].GetType(), Is.EqualTo(typeof(TestServiceCommand2)));
            Assert.That(subscriber2.Received[4].GetType(), Is.EqualTo(typeof(TestServiceRequest1)));
        }
Exemplo n.º 7
0
        public async void ShouldMonitorAllMessagesSentAndReceived()
        {
            FakeServiceEndpoint fakeServiceEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint fakeServer = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            List<IMessage> monitorReceived = new List<IMessage>();
            List<IMessage> monitorSent = new List<IMessage>();

            var monitorFactory = A.Fake<IMonitorFactory<IMessage>>();
            var monitor = A.Fake<IMonitor<IMessage>>();
            A.CallTo(() => monitor.MessageReceived(A<IMessage>._, A<TimeSpan>._)).Invokes(call =>
            {
                var message = call.GetArgument<IMessage>(0);
                Console.WriteLine("Received {0}", message);
                monitorReceived.Add(message);
            });
            A.CallTo(() => monitor.MessageSent(A<IMessage>._, A<TimeSpan>._)).Invokes(call =>
            {
                var message = call.GetArgument<IMessage>(0);
                Console.WriteLine("Sent {0}", message);
                monitorSent.Add(message);
            });
            A.CallTo(() => monitorFactory.Create(A<string>._)).Returns(monitor);

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpoint)fakeServiceEndpoint)
                .WithEndpoint((IServiceEndpointClient)fakeServer)
                .UsingMonitor(monitorFactory)
                .Create();

            List<Exception> exceptions = new List<Exception>();
            List<IMessage> serviceBusMessages = new List<IMessage>();
            serviceBus.Exceptions.Subscribe(exceptions.Add);
            serviceBus.Events.Subscribe(serviceBusMessages.Add);
            serviceBus.Commands.Subscribe(serviceBusMessages.Add);
            serviceBus.Requests.Subscribe(serviceBusMessages.Add);

            fakeServer.Commands.OfType<TestServiceCommand1>().Subscribe(async command => await fakeServer.PublishAsync(new TestServiceEvent2()));
            fakeServer.Requests.OfType<TestServiceRequest2>().Subscribe(async request => await fakeServer.ReplyAsync(request, new TestServiceResponse2()));

            serviceBus.GetResponses(new TestServiceRequest2()).Subscribe(serviceBusMessages.Add);
            await serviceBus.SendAsync(new TestServiceCommand2());
            await serviceBus.PublishAsync(new TestServiceEvent1());

            Assert.That(monitorSent.Count, Is.EqualTo(3));
            Assert.That(monitorSent.OfType<TestServiceCommand2>().Count(), Is.EqualTo(1));
            Assert.That(monitorSent.OfType<TestServiceEvent1>().Count(), Is.EqualTo(1));
            Assert.That(monitorSent.OfType<TestServiceRequest2>().Count(), Is.EqualTo(1));

            Assert.That(monitorReceived.Count, Is.EqualTo(1));
            Assert.That(monitorReceived.OfType<TestServiceResponse2>().Count(), Is.EqualTo(1));
        }
Exemplo n.º 8
0
        public void ShouldPublishMessagesWithNoEndpointToLocalBusWhenConfigured()
        {
            FakeServiceEndpoint fakeServiceEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint fakeServer = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            var localBus = new SubjectMessageBus<IMessage>();

            var testScheduler = new TestScheduler();

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpoint)fakeServiceEndpoint)
                .WithEndpoint((IServiceEndpointClient)fakeServer)
                .PublishLocally(localBus).OnlyMessagesWithNoEndpoints()
                .UsingConsoleMonitor(TimeSpan.FromSeconds(5), testScheduler)
                .UsingConsoleLogging()
                .Create();

            List<Exception> exceptions = new List<Exception>();
            List<IMessage> serviceBusMessages = new List<IMessage>();
            List<IMessage> localBusMessages = new List<IMessage>();
            serviceBus.Exceptions.Subscribe(exceptions.Add);
            serviceBus.Events.Subscribe(serviceBusMessages.Add);
            serviceBus.Commands.Subscribe(serviceBusMessages.Add);
            serviceBus.Requests.Subscribe(serviceBusMessages.Add);
            localBus.Messages.Subscribe(localBusMessages.Add);

            fakeServer.Commands.Subscribe(command => fakeServer.PublishAsync(new TestServiceEvent2()));
            fakeServer.Requests.Subscribe(request => fakeServer.ReplyAsync(request, new TestServiceResponse2()));
            serviceBus.Requests.OfType<TestServiceRequest1>().Subscribe(request =>
            {
                serviceBus.ReplyAsync(request, new TestServiceResponse1());
            });

            serviceBus.GetResponses(new TestServiceRequest1()).Subscribe(serviceBusMessages.Add);
            serviceBus.GetResponses(new TestServiceRequest2()).Subscribe(serviceBusMessages.Add);
            serviceBus.SendAsync(new TestServiceCommand2());
            serviceBus.PublishAsync(new TestServiceEvent1());
            serviceBus.SendAsync(new TestServiceCommand1());
            serviceBus.PublishAsync(new TestEventBelongingToNoService());

            Assert.That(exceptions.Count(), Is.EqualTo(0));

            Assert.That(localBusMessages.Count(), Is.EqualTo(1));
            Assert.That(localBusMessages[0].GetType(), Is.EqualTo(typeof(TestEventBelongingToNoService)));

            Assert.That(serviceBusMessages.Count(), Is.EqualTo(3));
            Assert.That(serviceBusMessages[0].GetType(), Is.EqualTo(typeof(TestServiceResponse2)));
            Assert.That(serviceBusMessages[1].GetType(), Is.EqualTo(typeof(TestServiceEvent2)));
            Assert.That(serviceBusMessages[2].GetType(), Is.EqualTo(typeof(TestEventBelongingToNoService))); // locally published

            testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
            
        }
Exemplo n.º 9
0
        public void ShouldPublishMessagesToLocalBusWhenConfigured()
        {
            FakeServiceEndpoint fakeServiceEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint fakeServer = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            var localBus = new SubjectMessageBus<IMessage>();

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpoint)fakeServiceEndpoint)
                .WithEndpoint((IServiceEndpointClient)fakeServer)
                .PublishLocally(localBus).AnyMessagesWithNoEndpointClients()
                .Create();

            List<Exception> exceptions = new List<Exception>();
            List<IMessage> serviceBusMessages = new List<IMessage>();
            List<IMessage> localBusMessages = new List<IMessage>();
            serviceBus.Exceptions.Subscribe(exceptions.Add);
            serviceBus.Events.Subscribe(serviceBusMessages.Add);
            serviceBus.Commands.Subscribe(serviceBusMessages.Add);
            serviceBus.Requests.Subscribe(serviceBusMessages.Add);
            localBus.Messages.Subscribe(localBusMessages.Add);

            fakeServer.Commands.Subscribe(command => fakeServer.PublishAsync(new TestServiceEvent2()));
            fakeServer.Requests.Subscribe(request => fakeServer.ReplyAsync(request, new TestServiceResponse2()));
            serviceBus.Requests.OfType<TestServiceRequest1>().Subscribe(request =>
            {
                serviceBus.ReplyAsync(request, new TestServiceResponse1());
            });

            serviceBus.GetResponses(new TestServiceRequest1()).Subscribe(serviceBusMessages.Add);
            serviceBus.GetResponses(new TestServiceRequest2()).Subscribe(serviceBusMessages.Add);
            serviceBus.SendAsync(new TestServiceCommand2());
            serviceBus.PublishAsync(new TestServiceEvent1());
            serviceBus.SendAsync(new TestServiceCommand1());
            serviceBus.PublishAsync(new TestEventBelongingToNoService());

            Assert.That(exceptions.Count(), Is.EqualTo(0));

            Assert.That(localBusMessages.Count(), Is.EqualTo(5));
            Assert.That(localBusMessages[0].GetType(), Is.EqualTo(typeof(TestServiceResponse1)));
            Assert.That(localBusMessages[1].GetType(), Is.EqualTo(typeof(TestServiceRequest1)));
            Assert.That(localBusMessages[2].GetType(), Is.EqualTo(typeof(TestServiceEvent1)));
            Assert.That(localBusMessages[3].GetType(), Is.EqualTo(typeof(TestServiceCommand1)));
            Assert.That(localBusMessages[4].GetType(), Is.EqualTo(typeof(TestEventBelongingToNoService)));

            Assert.That(serviceBusMessages.Count(), Is.EqualTo(7));
            Assert.That(serviceBusMessages[0].GetType(), Is.EqualTo(typeof(TestServiceRequest1))); // locally published
            Assert.That(serviceBusMessages[1].GetType(), Is.EqualTo(typeof(TestServiceResponse1))); // locally published
            Assert.That(serviceBusMessages[2].GetType(), Is.EqualTo(typeof(TestServiceResponse2)));
            Assert.That(serviceBusMessages[3].GetType(), Is.EqualTo(typeof(TestServiceEvent2)));
            Assert.That(serviceBusMessages[4].GetType(), Is.EqualTo(typeof(TestServiceEvent1))); // locally published
            Assert.That(serviceBusMessages[5].GetType(), Is.EqualTo(typeof(TestServiceCommand1))); // locally published
            Assert.That(serviceBusMessages[6].GetType(), Is.EqualTo(typeof(TestEventBelongingToNoService))); // locally published
            
        }
Exemplo n.º 10
0
        public void ShouldDeliverAllMessagesFromEndpointsWithoutLoggingEnabled()
        {
            FakeServiceEndpoint serviceEndpoint1 = new FakeServiceEndpoint(typeof(ITestServiceMessage1));
            FakeServiceEndpoint serviceEndpoint2 = new FakeServiceEndpoint(typeof(ITestServiceMessage2));

            IServiceBus serviceBus = ServiceBus.Configure()
                .WithEndpoint((IServiceEndpointClient)serviceEndpoint1)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint1)
                .WithEndpoint((IServiceEndpointClient)serviceEndpoint2)
                .WithEndpoint((IServiceEndpoint)serviceEndpoint2)
                .UsingConsoleLogging(endpoint => false)
                .Create();

            List<Exception> exceptions = new List<Exception>();
            List<IMessage> messages = new List<IMessage>();
            serviceBus.Exceptions.Subscribe(exceptions.Add);
            serviceBus.Events.Subscribe(messages.Add);
            serviceBus.Commands.Subscribe(messages.Add);
            
            serviceEndpoint1.Messages.OnNext(new TestServiceEvent1());
            serviceEndpoint1.Messages.OnNext(new TestServiceCommand1());

            Assert.That(exceptions.Count(), Is.EqualTo(0));
            Assert.That(messages.Count(), Is.EqualTo(2));
            Assert.That(messages[0].GetType(), Is.EqualTo(typeof(TestServiceEvent1)));
            Assert.That(messages[1].GetType(), Is.EqualTo(typeof(TestServiceCommand1)));
            
        }