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

            LocalBus.ShouldHaveSubscriptionFor <SimpleMessage>();
        }
        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");
        }
Ejemplo n.º 4
0
        public void A_response_should_be_sent_directly_if_a_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, context => context.SendResponseTo(LocalBus));

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

            consumer.ShouldHaveReceivedMessage(pong.Message, 3.Seconds());
            otherConsumer.ShouldNotHaveReceivedMessage(pong.Message, 1.Seconds());
        }
        public void Should_not_remove_any_existing_subscriptions()
        {
            RemoteBus.Subscribe <A>(x => { });
            RemoteBus.Dispose();

            ThreadUtil.Sleep(2.Seconds());

            LocalBus.ShouldHaveSubscriptionFor <A>();
        }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        protected override void EstablishContext()
        {
            base.EstablishContext();

            var sagaRepository = SetupSagaRepository <RegisterUserSaga>(ObjectBuilder);

            // this just shows that you can easily respond to the message
            RemoteBus.Subscribe <SendUserVerificationEmail>(
                x => RemoteBus.Publish(new UserVerificationEmailSent(x.CorrelationId, x.Email)));

            RemoteBus.Subscribe <RegisterUserSaga>();
        }
Ejemplo n.º 9
0
        public void It_should_be_received_by_one_subscribed_message_handler()
        {
            TestMessageConsumer <PingMessage> messageConsumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.Subscribe <PingMessage>(messageConsumer.MessageHandler);

            PingMessage message = new PingMessage();

            LocalBus.Publish(message);

            messageConsumer.ShouldHaveReceivedMessage(message, _timeout);
        }
Ejemplo n.º 10
0
        public void It_should_be_received_by_an_interested_correlated_consumer()
        {
            PingMessage message = new PingMessage();

            TestCorrelatedConsumer <PingMessage, Guid> consumer = new TestCorrelatedConsumer <PingMessage, Guid>(message.CorrelationId);

            RemoteBus.Subscribe(consumer);

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(message, _timeout);
        }
Ejemplo n.º 11
0
        public void It_should_be_received_by_a_component()
        {
            ObjectBuilder.Stub(x => x.GetInstance <TestMessageConsumer <PingMessage> >()).Return(new TestMessageConsumer <PingMessage>());

            RemoteBus.Subscribe <TestMessageConsumer <PingMessage> >();

            PingMessage message = new PingMessage();

            LocalBus.Publish(message);

            TestConsumerBase <PingMessage> .AnyShouldHaveReceivedMessage(message, _timeout);
        }
Ejemplo n.º 12
0
        public void It_should_not_be_received_by_an_uninterested_correlated_consumer()
        {
            TestCorrelatedConsumer <PingMessage, Guid> consumer = new TestCorrelatedConsumer <PingMessage, Guid>(Guid.NewGuid());

            RemoteBus.Subscribe(consumer);

            PingMessage message = new PingMessage();

            LocalBus.Publish(message);

            consumer.ShouldNotHaveReceivedMessage(message, _timeout);
        }
Ejemplo n.º 13
0
        public void It_should_be_received_by_one_subscribed_consumer()
        {
            var consumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.Subscribe(consumer);

            var message = new PingMessage();

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(message, _timeout);
        }
        public void Should_remove_any_previous_subscriptions()
        {
            RemoteBus.Subscribe <A>(x => { });

            LocalBus.ShouldHaveSubscriptionFor <A>();

            RemoteBus.Dispose();

            ThreadUtil.Sleep(1.Seconds());

            SetupRemoteBus();

            LocalBus.ShouldNotHaveSubscriptionFor <A>();
        }
        public void Should_deliver_the_message_to_an_interested_consumer()
        {
            var first = new Future <FirstMessageContract>();

            RemoteBus.Subscribe <FirstMessageContract>(first.Complete);

            PipelineViewer.Trace(RemoteBus.InboundPipeline);

            var message = new SomeMessageContract("Joe", 27);

            LocalBus.Publish(message);

            first.IsAvailable(1.Seconds()).ShouldBeTrue();
        }
        public void The_message_should_be_delivered_to_a_remote_subscriber()
        {
            ManualResetEvent _updateEvent = new ManualResetEvent(false);

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

            UpdateMessage um = new UpdateMessage();

            LocalBus.Publish(um);

            Assert.That(_updateEvent.WaitOne(TimeSpan.FromSeconds(3), true), Is.True,
                        "Timeout expired waiting for message");
        }
        public void Should_deliver_the_message_once_the_endpoint_is_available()
        {
            var received = new Future <PingMessage>();

            using (RemoteBus.Subscribe <PingMessage>(received.Complete).Disposable())
            {
                Thread.Sleep(100);

                var ping = new PingMessage();
                LocalBus.Publish(ping);

                received.IsAvailable(5.Seconds()).ShouldBeTrue();
            }
        }
Ejemplo n.º 18
0
        protected override void EstablishContext()
        {
            base.EstablishContext();

            _repository = SetupSagaRepository <AutoStateMachineSaga>(ObjectBuilder);

            // this just shows that you can easily respond to the message
            RemoteBus.Subscribe <SendUserVerificationEmail>(
                x => RemoteBus.Publish(new UserVerificationEmailSent(x.CorrelationId, x.Email)));

            _transactionId = CombGuid.Generate();
            _username      = "******";
            _password      = "******";
            _email         = "*****@*****.**";
            _displayName   = "Joe Blow";
        }
Ejemplo n.º 19
0
        private void MakeSagaDown()
        {
            MakeSagaSuspect();
            var fm = new FutureMessage <TimeoutExpired>();

            RemoteBus.Subscribe <TimeoutExpired>(fm.Set);
            Thread.Sleep(500);
            LocalBus.Publish(new TimeoutExpired {
                CorrelationId = _id, Tag = 2
            });
            fm.IsAvailable(1.Seconds()).ShouldBeTrue();

            var saga = Repository.Where(x => x.CorrelationId == _id).First();

            saga.CurrentState.ShouldEqual(HealthSaga.Down, "MakeSagaDown failed");
        }
Ejemplo n.º 20
0
        public void It_should_be_received_by_multiple_subscribed_consumers()
        {
            var localConsumer = new TestMessageConsumer <PingMessage>();

            LocalBus.Subscribe(localConsumer);

            var remoteConsumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.Subscribe(remoteConsumer);

            PingMessage message = new PingMessage();

            LocalBus.Publish(message);

            localConsumer.ShouldHaveReceivedMessage(message, _timeout);
            remoteConsumer.ShouldHaveReceivedMessage(message, _timeout);
        }
        public void The_system_should_be_ready_to_use_before_getting_underway()
        {
            var consumer          = new TestMessageConsumer <PingMessage>();
            var unsubscribeAction = RemoteBus.Subscribe(consumer);

            Thread.Sleep(1000);

            DumpPipelines();

            var message = new PingMessage();

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(message, 500.Milliseconds());

            unsubscribeAction();
        }
        public void The_response_address_should_be_properly_set_on_the_message_envelope()
        {
            PingMessage ping = new PingMessage();

            FutureMessage <PingMessage> received = new FutureMessage <PingMessage>();

            RemoteBus.Subscribe <PingMessage>(message =>
            {
                Assert.AreEqual(LocalBus.Endpoint.Uri, CurrentMessage.Headers.ResponseAddress);

                received.Set(message);
            });

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

            Assert.IsTrue(received.IsAvailable(3.Seconds()), "Timeout waiting for message");
        }
        public void The_message_type_should_be_properly_set_on_the_message_envelope()
        {
            PingMessage ping = new PingMessage();

            FutureMessage <PingMessage> received = new FutureMessage <PingMessage>();

            RemoteBus.Subscribe <PingMessage>(message =>
            {
                Assert.AreEqual(typeof(PingMessage).ToMessageName(), CurrentMessage.Headers.MessageType);

                received.Set(message);
            });

            LocalBus.Publish(ping);

            Assert.IsTrue(received.IsAvailable(3.Seconds()), "Timeout waiting for message");
        }
Ejemplo n.º 24
0
        protected void RunTest()
        {
            var batchConsumer = new TestBatchConsumer <IndividualBatchMessage, Guid>();

            RemoteBus.Subscribe(batchConsumer);

            Guid batchId = Guid.NewGuid();

            for (int i = 0; i < _batchSize; i++)
            {
                IndividualBatchMessage message = new IndividualBatchMessage(batchId, _batchSize);

                LocalBus.Publish(message);
            }

            batchConsumer.ShouldHaveReceivedBatch(_timeout);
        }
Ejemplo n.º 25
0
        public void It_should_not_be_received_by_an_uninterested_consumer()
        {
            TestMessageConsumer <PingMessage> messageConsumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.Subscribe <PingMessage>(messageConsumer.MessageHandler, x => false);

            TestMessageConsumer <PingMessage> consumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.Subscribe(consumer);

            PingMessage message = new PingMessage();

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(message, _timeout);
            messageConsumer.ShouldNotHaveReceivedMessage(message, TimeSpan.FromSeconds(1));
        }
Ejemplo n.º 26
0
        public void It_should_leave_the_message_in_the_queue_if_an_exception_is_thrown()
        {
            var consumer = new TestSelectiveConsumer <PingMessage>(x => false);

            RemoteBus.Subscribe(consumer);

            var realConsumer = new TestMessageConsumer <PingMessage>();

            LocalBus.Subscribe(realConsumer);

            var message = new PingMessage();

            LocalBus.Publish(message);

            realConsumer.ShouldHaveReceivedMessage(message, _timeout);
            consumer.ShouldNotHaveReceivedMessage(message, _timeout);
        }
Ejemplo n.º 27
0
        public void MakeSagaSuspect()
        {
            LocalBus.Publish(new EndpointCameOnline(_id, LocalBus.ControlBus.Endpoint.Uri, LocalBus.Endpoint.Uri, 0));
            var fm = new FutureMessage <TimeoutExpired>();

            RemoteBus.Subscribe <TimeoutExpired>(fm.Set);
            Thread.Sleep(5000);
            LocalBus.Publish(new TimeoutExpired {
                CorrelationId = _id, Tag = 1
            });
            fm.IsAvailable(5.Seconds()).ShouldBeTrue();

            var saga = Repository.Where(x => x.CorrelationId == _id).FirstOrDefault();

            saga.ShouldNotBeNull();
            saga.CurrentState.ShouldEqual(HealthSaga.Suspect, "MakeSagaSuspect failed");
        }
Ejemplo n.º 28
0
        protected void RunTest()
        {
            ObjectBuilder.Stub(x => x.GetInstance <TestBatchConsumer <IndividualBatchMessage, Guid> >())
            .Return(new TestBatchConsumer <IndividualBatchMessage, Guid>());

            RemoteBus.Subscribe <TestBatchConsumer <IndividualBatchMessage, Guid> >();

            Guid batchId = Guid.NewGuid();

            for (int i = 0; i < _batchSize; i++)
            {
                IndividualBatchMessage message = new IndividualBatchMessage(batchId, _batchSize);

                LocalBus.Publish(message);
            }

            TestBatchConsumer <IndividualBatchMessage, Guid> .AnyShouldHaveReceivedBatch(batchId, _timeout);
        }
Ejemplo n.º 29
0
//        [ExpectedException(typeof(SemaphoreFullException))] //TODO: Bad Exception
        public void The_batch_should_throw_an_error()
        {
            _batchSize = 1;

            var timeoutConsumer = new TestMessageConsumer <BatchTimeout <IndividualBatchMessage, Guid> >();

            RemoteBus.Subscribe(timeoutConsumer);

            var batchConsumer = new TestBatchConsumer <IndividualBatchMessage, Guid>();

            RemoteBus.Subscribe(batchConsumer);

            Guid batchId = Guid.NewGuid();
            IndividualBatchMessage message = new IndividualBatchMessage(batchId, _batchSize);

            LocalBus.Publish(message);
            LocalBus.Publish(message);
        }
Ejemplo n.º 30
0
        public void If_a_saga_is_suspect_a_pingtimeout_should_make_it_down()
        {
            MakeSagaSuspect();

            var fm = new FutureMessage <TimeoutExpired>();

            RemoteBus.Subscribe <TimeoutExpired>(fm.Set);
            Thread.Sleep(250);
            LocalBus.Publish(new TimeoutExpired {
                CorrelationId = _id, Tag = 2
            });

            fm.IsAvailable(100.Seconds()).ShouldBeTrue("never got message");
            Thread.Sleep(100000);
            var saga = Repository.Where(x => x.CorrelationId == _id).First();

            saga.CurrentState.ShouldEqual(HealthSaga.Down);
        }