public void A_consumer_is_subscribed_to_a_message()
        {
            Consumer = new ConsumerOf <SimpleMessage>();
            RemoteBus.SubscribeInstance(Consumer);

            LocalBus.ShouldHaveSubscriptionFor <SimpleMessage>();
        }
Beispiel #2
0
        protected override void EstablishContext()
        {
            base.EstablishContext();

            _ping   = new PingMessage();
            _future = new FutureMessage <PingMessage, Guid>(_ping.CorrelationId);

            _unsubscribe = RemoteBus.SubscribeHandler <PingMessage>(message => { _future.Set(message); });


            RemoteBus.ShouldHaveRemoteSubscriptionFor <PingMessage>();

            LocalBus.ShouldHaveRemoteSubscriptionFor <PingMessage>();

            Trace.WriteLine("LocalBus");

            LocalBus.OutboundPipeline.Trace();

            Trace.WriteLine("RemoteBus");

            RemoteBus.OutboundPipeline.Trace();


            LocalBus.Publish(_ping);
        }
        public void Should_throw_a_timeout_exception_if_no_response_received()
        {
            var pongReceived = new FutureMessage <PongMessage>();
            var pingReceived = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeHandler <PingMessage>(pingReceived.Set);
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage();

            TimeSpan timeout = 2.Seconds();

            Assert.Throws <RequestTimeoutException>(() =>
            {
                LocalBus.PublishRequest(ping, x =>
                {
                    x.Handle <PongMessage>(pongReceived.Set);

                    x.SetTimeout(timeout);
                });
            }, "A timeout exception should have been thrown");

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeFalse("The pong should not have been received");
        }
        public void Should_use_a_clean_syntax_following_standard_conventions()
        {
            var pongReceived = new FutureMessage <PongMessage>();
            var pingReceived = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeContextHandler <PingMessage>(x =>
            {
                pingReceived.Set(x.Message);
                x.Respond(new PongMessage {
                    TransactionId = x.Message.TransactionId
                });
            });
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage();

            TimeSpan timeout = 8.Seconds();

            LocalBus.PublishRequest(ping, x =>
            {
                x.Handle <PongMessage>(message =>
                {
                    message.TransactionId.ShouldEqual(ping.TransactionId,
                                                      "The response correlationId did not match");
                    pongReceived.Set(message);
                });

                x.SetTimeout(timeout);
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");
        }
Beispiel #5
0
        public void Should_ignore_a_response_that_was_not_for_us()
        {
            var pongReceived = new FutureMessage <PongMessage>();
            var pingReceived = new FutureMessage <PingMessage>();
            var badResponse  = new FutureMessage <PongMessage>();

            LocalBus.SubscribeHandler <PongMessage>(pongReceived.Set);

            RemoteBus.SubscribeContextHandler <PingMessage>(x =>
            {
                pingReceived.Set(x.Message);
                RemoteBus.Publish(new PongMessage {
                    TransactionId = x.Message.TransactionId
                });
            });
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage();

            TimeSpan timeout = 8.Seconds();

            Assert.Throws <RequestTimeoutException>(() =>
            {
                RemoteBus.Endpoint.SendRequest(ping, LocalBus, x =>
                {
                    x.Handle <PongMessage>(badResponse.Set);

                    x.SetTimeout(timeout);
                });
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");
            badResponse.IsAvailable(2.Seconds()).ShouldBeFalse("Should not have received a response");
        }
Beispiel #6
0
        public void Adding_many_dynamic_and_removing_should_retain_dynamics()
        {
            var dynamicA = new FutureMessage <A>();
            var dynamicB = new FutureMessage <B>();
            var dynamicC = new FutureMessage <C>();
            var dynamicD = new FutureMessage <D>();

            UnsubscribeAction subscriptionA = RemoteBus.SubscribeHandler <A>(dynamicA.Set);
            UnsubscribeAction subscriptionB = RemoteBus.SubscribeHandler <B>(dynamicB.Set);
            UnsubscribeAction subscriptionC = RemoteBus.SubscribeHandler <C>(dynamicC.Set);
            UnsubscribeAction subscriptionD = RemoteBus.SubscribeHandler <D>(dynamicD.Set);

            LocalBus.HasSubscription <D>(8.Seconds()).Any().ShouldBeTrue("No D subscription");
            try
            {
                subscriptionA().ShouldBeFalse("A static not remaining");
                subscriptionB().ShouldBeFalse("B static not remaining");
                subscriptionC().ShouldBeFalse("C static not remaining");

                LocalBus.Publish(new A());
                LocalBus.Publish(new B());
                LocalBus.Publish(new C());
                LocalBus.Publish(new D());

                _receivedA.IsAvailable(8.Seconds()).ShouldBeTrue("A not received");
                _receivedB.IsAvailable(8.Seconds()).ShouldBeTrue("B not received");
                _receivedC.IsAvailable(8.Seconds()).ShouldBeTrue("C not received");
                dynamicD.IsAvailable(8.Seconds()).ShouldBeTrue("D should have been received");
            }
            finally
            {
                subscriptionD();
            }
        }
Beispiel #7
0
        public void Should_support_send_as_well()
        {
            var pongReceived = new FutureMessage <PongMessage>();
            var pingReceived = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeContextHandler <PingMessage>(x =>
            {
                pingReceived.Set(x.Message);
                x.Respond(new PongMessage {
                    TransactionId = x.Message.TransactionId
                });
            });
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage();

            TimeSpan timeout = 8.Seconds();

            RemoteBus.Endpoint.SendRequest(ping, LocalBus, x =>
            {
                x.Handle <PongMessage>(message =>
                {
                    message.TransactionId.ShouldEqual(ping.TransactionId,
                                                      "The response correlationId did not match");
                    pongReceived.Set(message);
                });

                x.SetTimeout(timeout);
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");
        }
Beispiel #8
0
        public void Should_throw_a_timeout_exception_for_async_when_end_is_called()
        {
            var pongReceived   = new FutureMessage <PongMessage>();
            var pingReceived   = new FutureMessage <PingMessage>();
            var callbackCalled = new FutureMessage <IAsyncResult>();

            RemoteBus.SubscribeHandler <PingMessage>(pingReceived.Set);
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage();

            TimeSpan timeout = 2.Seconds();

            LocalBus.BeginPublishRequest(ping, callbackCalled.Set, null, x =>
            {
                x.Handle <PongMessage>(pongReceived.Set);

                x.SetTimeout(timeout);
            });

            callbackCalled.IsAvailable(8.Seconds()).ShouldBeTrue("Callback was not invoked");

            Assert.Throws <RequestTimeoutException>(
                () => { LocalBus.EndPublishRequest <PingMessage>(callbackCalled.Message); },
                "A timeout exception should have been thrown");

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeFalse("The pong should not have been received");
        }
Beispiel #9
0
        public void A_response_should_be_sent_directly_if_a_reply_address_is_specified()
        {
            var ping = new PingMessage();

            var otherConsumer = new TestMessageConsumer <PongMessage>();

            RemoteBus.SubscribeInstance(otherConsumer);

            var consumer = new TestCorrelatedConsumer <PongMessage, Guid>(ping.CorrelationId);

            LocalBus.SubscribeInstance(consumer);

            var pong = new FutureMessage <PongMessage>();

            RemoteBus.SubscribeHandler <PingMessage>(message =>
            {
                pong.Set(new PongMessage(message.CorrelationId));

                RemoteBus.Context().Respond(pong.Message);
            });

            RemoteBus.ShouldHaveRemoteSubscriptionFor <PongMessage>();
            LocalBus.ShouldHaveRemoteSubscriptionFor <PongMessage>();
            LocalBus.ShouldHaveRemoteSubscriptionFor <PingMessage>();

            LocalBus.Publish(ping, context => context.SendResponseTo(LocalBus));

            Assert.IsTrue(pong.IsAvailable(8.Seconds()), "No pong generated");

            consumer.ShouldHaveReceivedMessage(pong.Message, 8.Seconds());
            otherConsumer.ShouldNotHaveReceivedMessage(pong.Message, 1.Seconds());
        }
        public void a_rat_is_sent_to_a_hungry_cat()
        {
            rat_id       = CombGuid.Generate();
            received_rat = new Future <Rat>();
            cat          = new ConsumerOf <Rat>(a_large_rat_actually =>
            {
                Console.WriteLine("Miaooo!!!");
                Console.WriteLine(a_large_rat_actually.Sound + "!!!");
                Console.WriteLine("Cat: chase! ...");
                Console.WriteLine("*silence*");
                Console.WriteLine("Cat: *Crunch chrunch*");
                received_rat.Complete(a_large_rat_actually);
            });

            cat_nap_unsubscribes = RemoteBus.SubscribeInstance(cat);

            // we need to make sure this bus is up before sending to it
            RemoteBus.Endpoint.InboundTransport.Receive(ctx => c => { }, 4.Seconds());

            LocalBus.GetEndpoint(RemoteUri).Send <Rat>(new
            {
                Sound         = "Eeeek",
                CorrelationId = rat_id
            });
        }
        public void The_message_should_be_delivered_to_a_remote_subscriber_with_a_reply()
        {
            ManualResetEvent _updateEvent = new ManualResetEvent(false);

            Action <UpdateMessage> handler =
                msg =>
            {
                _updateEvent.Set();

                RemoteBus.Publish(new UpdateAcceptedMessage());
            };

            ManualResetEvent _repliedEvent = new ManualResetEvent(false);

            RemoteBus.Subscribe(handler);

            LocalBus.Subscribe <UpdateAcceptedMessage>(
                delegate { _repliedEvent.Set(); });

            UpdateMessage um = new UpdateMessage();

            LocalBus.Publish(um);

            Assert.That(_updateEvent.WaitOne(TimeSpan.FromSeconds(3), true), Is.True,
                        "Timeout expired waiting for message");

            Assert.That(_repliedEvent.WaitOne(TimeSpan.FromSeconds(3), true), Is.True, "NO response message received");
        }
        public void Multiple_messages_should_be_delivered_to_the_appropriate_remote_subscribers()
        {
            ManualResetEvent _updateEvent = new ManualResetEvent(false);

            RemoteBus.Subscribe <UpdateMessage>(
                delegate { _updateEvent.Set(); });

            ManualResetEvent _deleteEvent = new ManualResetEvent(false);

            RemoteBus.Subscribe <DeleteMessage>(
                delegate { _deleteEvent.Set(); });

            DeleteMessage dm = new DeleteMessage();

            LocalBus.Publish(dm);

            UpdateMessage um = new UpdateMessage();

            LocalBus.Publish(um);

            Assert.That(_deleteEvent.WaitOne(TimeSpan.FromSeconds(6), true), Is.True,
                        "Timeout expired waiting for message");

            Assert.That(_updateEvent.WaitOne(TimeSpan.FromSeconds(6), true), Is.True,
                        "Timeout expired waiting for message");
        }
        public void A_response_should_be_published_if_no_reply_address_is_specified()
        {
            PingMessage ping = new PingMessage();

            TestMessageConsumer <PongMessage> otherConsumer = new TestMessageConsumer <PongMessage>();

            RemoteBus.Subscribe(otherConsumer);

            TestCorrelatedConsumer <PongMessage, Guid> consumer = new TestCorrelatedConsumer <PongMessage, Guid>(ping.CorrelationId);

            LocalBus.Subscribe(consumer);

            FutureMessage <PongMessage> pong = new FutureMessage <PongMessage>();

            RemoteBus.Subscribe <PingMessage>(message =>
            {
                pong.Set(new PongMessage(message.CorrelationId));

                CurrentMessage.Respond(pong.Message);
            });

            LocalBus.Publish(ping);

            Assert.IsTrue(pong.IsAvailable(3.Seconds()), "No pong generated");

            consumer.ShouldHaveReceivedMessage(pong.Message, 3.Seconds());
            otherConsumer.ShouldHaveReceivedMessage(pong.Message, 1.Seconds());
        }
        public void Should_throw_an_exception_if_a_fault_was_published()
        {
            var pongReceived  = new FutureMessage <PongMessage>();
            var faultReceived = new FutureMessage <Fault <PingMessage> >();
            var pingReceived  = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeContextHandler <PingMessage>(x =>
            {
                pingReceived.Set(x.Message);

                throw new InvalidOperationException("This should carry over to the calling request");
            });
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage();

            TimeSpan timeout = Debugger.IsAttached ? 5.Minutes() : 24.Seconds();

            LocalBus.PublishRequest(ping, x =>
            {
                x.Handle <PongMessage>(pongReceived.Set);
                x.HandleFault(faultReceived.Set);

                x.SetTimeout(timeout);
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            faultReceived.IsAvailable(timeout).ShouldBeTrue("The fault was not received");
            pongReceived.IsAvailable(1.Seconds()).ShouldBeFalse("The pong was not received");
        }
Beispiel #15
0
        public void LocalAndRemoteTestFixtureTeardown()
        {
            LocalBus.Dispose();
            LocalBus = null;

            RemoteBus.Dispose();
            RemoteBus = null;
        }
Beispiel #16
0
        protected override void EstablishContext()
        {
            base.EstablishContext();

            RemoteBus.ShouldHaveSubscriptionFor <MyMessage>();

            LocalBus.Publish(new MyMessage());
        }
 public void A_message_is_published()
 {
     RemoteBus.Publish(new A
     {
         StringA = "ValueA",
         StringB = "ValueB",
     });
 }
Beispiel #18
0
        public void Should_have_received_b()
        {
            var message = new B();

            RemoteBus.Publish(message);

            TestConsumerBase <B> .OnlyOneShouldHaveReceivedMessage(message, 8.Seconds());
        }
        public void Should_not_remove_any_existing_subscriptions()
        {
            RemoteBus.Subscribe <A>(x => { });
            RemoteBus.Dispose();

            ThreadUtil.Sleep(2.Seconds());

            LocalBus.ShouldHaveSubscriptionFor <A>();
        }
 public void A_pong_service()
 {
     PingService = new ConsumerOf <Ping>(ping =>
     {
         // Response with a pong to the ping
         RemoteBus.Context().Respond(new Pong(ping.CorrelationId));
     });
     RemoteBus.SubscribeInstance(PingService);
 }
Beispiel #21
0
        public void Should_call_the_ack_method_upon_delivery()
        {
            RemoteBus.Publish(new A
            {
                StringA = "ValueA",
            });

            _received.WaitUntilCompleted(8.Seconds()).ShouldBeTrue();
        }
 public void A_pong_service()
 {
     PingService = new ConsumerOf <Ping>(ping =>
     {
         // Response with a pong to the ping
         CurrentMessage.Respond(new Pong(ping.CorrelationId));
     });
     RemoteBus.Subscribe(PingService);
 }
Beispiel #23
0
        public void A_message_should_only_reach_the_consumer_if_the_filter_passes_it_forward()
        {
            MessageFilter <RequestMessage> filter = new MessageFilter <RequestMessage>(delegate { return(false); }, _consumer);

            LocalBus.Subscribe(filter);
            RemoteBus.Publish(_message);

            Assert.That(_passed.WaitOne(TimeSpan.FromSeconds(1), true), Is.False, "Timeout waiting for message handler to be called");
        }
Beispiel #24
0
        protected override void EstablishContext()
        {
            base.EstablishContext();

            AddFirstCommandInstance("A", "loopback://localhost/a");
            AddFirstCommandInstance("B", "loopback://localhost/b");
            AddFirstCommandInstance("C", "loopback://localhost/c");

            RemoteBus.ShouldHaveRemoteSubscriptionFor <Distributed <FirstCommand> >();
        }
        public void A_state_machine_based_saga_should_automatically_wire_up_subscriptions()
        {
            RemoteBus.Subscribe <AutoStateMachineSaga>();

            PipelineViewer.Trace(RemoteBus.InboundPipeline);

            PipelineViewer.Trace(RemoteBus.OutboundPipeline);


            Assert.AreEqual(0, _repository.Where(x => true).Count());

            RemoteBus.Publish(new RegisterUser(_transactionId, _username, _password, _displayName, _email));

            for (int i = 0; i < 20; i++)
            {
                if (_repository.Where(x => true).Count() == 1)
                {
                    break;
                }

                Thread.Sleep(100);
            }

            Assert.AreEqual(1, _repository.Where(x => true).Count());

            foreach (AutoStateMachineSaga saga in _repository.Where(x => true))
            {
                saga.CurrentState.ShouldEqual(AutoStateMachineSaga.WaitingForEmailValidation);
            }

            _repository
            .Where(x => x.CorrelationId == _transactionId)
            .Select(x => x.CurrentState)
            .First()
            .ShouldEqual(AutoStateMachineSaga.WaitingForEmailValidation);

            RemoteBus.Publish(new UserValidated(_transactionId));

            for (int i = 0; i < 20; i++)
            {
                if (_repository.Where(x => x.CorrelationId == _transactionId).Select(x => x.CurrentState).First() == AutoStateMachineSaga.Completed)
                {
                    break;
                }

                Thread.Sleep(100);
            }

            _repository
            .Where(x => x.CorrelationId == _transactionId)
            .Select(x => x.CurrentState)
            .First()
            .ShouldEqual(AutoStateMachineSaga.Completed);
        }
Beispiel #26
0
        public void Should_have_received_b_on_an_instance()
        {
            var consumer = new ConsumerOfAAndB();

            RemoteBus.SubscribeInstance(consumer);

            var message = new B();

            LocalBus.Publish(message);

            consumer.ConsumerB.ShouldHaveReceivedMessage(message, 8.Seconds());
        }
Beispiel #27
0
        public void Should_be_possible()
        {
            LocalBus.HasSubscription <IBusinessCommand>(8.Seconds()).Any().ShouldBeTrue();
            RemoteBus.HasSubscription <ISecureCommand <IBusinessCommand> >(8.Seconds()).Any().ShouldBeTrue();

            RemoteBus.Publish(new BusinessCommand
            {
                SqlText = "DROP TABLE [Users]",
            });

            CommandHandler.CommandReceived.IsAvailable(8.Seconds()).ShouldBeTrue();
        }
        protected override void TeardownContext()
        {
            LocalBus.Dispose();
            LocalBus = null;

            RemoteBus.Dispose();
            RemoteBus = null;

            SubscriptionService = null;

            base.TeardownContext();
        }
Beispiel #29
0
        public void A_message_is_published_one_the_local_bus()
        {
            _received = new Future <A>();
            RemoteBus.SubscribeHandler <A>(message => _received.Complete(message));

            Thread.Sleep(3.Seconds());

            LocalBus.Publish(new A
            {
                StringA = "ValueA",
            });
        }
        public void It_should_be_received_by_a_component()
        {
            RemoteBus.SubscribeConsumer <TestMessageConsumer <PingMessage> >();

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var message = new PingMessage();

            LocalBus.Publish(message);

            TestConsumerBase <PingMessage> .AnyShouldHaveReceivedMessage(message, _timeout);
        }