public void The_message_type_should_be_properly_set_on_the_message_envelope()
        {
            var ping = new PingMessage();

            var received = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeHandler <PingMessage>(message =>
            {
                Assert.AreEqual(typeof(PingMessage).ToMessageName(), LocalBus.Context().MessageType);

                received.Set(message);
            });

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();
            LocalBus.Publish(ping);

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }
Exemplo n.º 2
0
        public void I_should_be_able_to_split_a_group_of_messages_into_parts()
        {
            Consumer c = new Consumer(LocalBus);

            LocalBus.Subscribe(c);

            SpecialGroup group = MessageGroup.Build <SpecialGroup>()
                                 .Add(new PingMessage())
                                 .Add(new PongMessage());

            group.SplitOnConsume = true;

            LocalBus.Publish(group);

            Assert.That(c.Received.WaitOne(TimeSpan.FromSeconds(300), true), Is.True, "No message received by consumer");
            Assert.That(c.GotPing.WaitOne(TimeSpan.FromSeconds(300), true), Is.True, "No ping received by consumer");
            Assert.That(c.GotPong.WaitOne(TimeSpan.FromSeconds(300), true), Is.True, "No pong received by consumer");
        }
Exemplo n.º 3
0
        public void The_method_should_be_called_for_each_destination_endpoint_when_there_are_multiple()
        {
            RemoteBus.SubscribeHandler <PingMessage>(x => { });

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            LocalBus.SubscribeHandler <PingMessage>(x => { });

            var ping = new PingMessage();

            var consumers = new List <Uri>();

            LocalBus.Publish(ping, x => { x.ForEachSubscriber(address => consumers.Add(address.Uri)); });

            Assert.AreEqual(2, consumers.Count);
            Assert.IsTrue(consumers.Contains(LocalBus.Endpoint.Address.Uri));
            Assert.IsTrue(consumers.Contains(RemoteBus.Endpoint.Address.Uri));
        }
        public void The_response_address_should_be_properly_set_on_the_message_envelope()
        {
            var ping = new PingMessage();

            var received = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeHandler <PingMessage>(message =>
            {
                Assert.AreEqual(LocalBus.Endpoint.Address.Uri, LocalBus.Context().ResponseAddress);

                received.Set(message);
            });

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();
            LocalBus.Publish(ping, context => context.SendResponseTo(LocalBus.Endpoint.Address.Uri));

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }
Exemplo n.º 5
0
        public void Should_throw_a_handler_exception_on_the_calling_thread_using_async()
        {
            var pongReceived   = new FutureMessage <PongMessage>();
            var pingReceived   = new FutureMessage <PingMessage>();
            var callbackCalled = new FutureMessage <IAsyncResult>();

            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 = 18.Seconds();

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

                    throw new InvalidOperationException("I got it, but I am naughty with it.");
                });

                x.SetTimeout(timeout);
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");

            callbackCalled.IsAvailable(timeout).ShouldBeTrue("Called was not called");

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

            exception.Response.ShouldBeAnInstanceOf <PongMessage>();
            exception.InnerException.ShouldBeAnInstanceOf <InvalidOperationException>();
        }
        public void Then_the_claim_is_rejected_when_no_verification_is_received_within_the_timeout()
        {
            var timer      = Stopwatch.StartNew();
            var controller = new ClaimRequestTestCoordinator(LocalBus, 1000);

            using (LocalBus.SubscribeInstance(controller).Disposable())
            {
                RemoteBus.ShouldHaveSubscriptionFor <ClaimRequestCreatedPendingVerificationEvent>();

                bool complete = controller.CreateClaimRequest(1200, "ClaimType", DateTime.Today.AddDays(-10), "provider", "reason");
                Assert.IsTrue(complete, "There should be a timeout scheduled for this claim request.");

                timer.Stop();
                Debug.WriteLine(string.Format("Time to handle message: {0}ms", timer.ElapsedMilliseconds));

                complete = controller.Timeout();
                Assert.IsTrue(complete, "The claim request timed out but the claim was not rejected.");
            }
        }
Exemplo n.º 7
0
        T Publish <T>()
            where T : class, new()
        {
            LocalBus.HasSubscription <T>().Any()
            .ShouldBeTrue("Message subscription was not found");
            LocalBus.HasSubscription <WorkerAvailable <T> >().Any()
            .ShouldBeTrue("Worker available subscription was not found.");
            RemoteBus.HasSubscription <Distributed <T> >().Any()
            .ShouldBeTrue("Message subscription was not found");

            T message = new T();

            LocalBus.Endpoint.Send(message, context =>
            {
                context.SendResponseTo(LocalBus);
            });

            return(message);
        }
        protected override void TeardownContext()
        {
            RemoteBus.Dispose();
            RemoteBus        = null;
            RemoteControlBus = null;

            LocalBus.Dispose();
            LocalBus        = null;
            LocalControlBus = null;

            SubscriptionService.Stop();
            SubscriptionService.Dispose();
            SubscriptionService = null;

            SubscriptionBus.Dispose();
            SubscriptionBus = null;

            base.TeardownContext();
        }
        public void It_should_not_rollback_a_send_if_an_exception_is_thrown()
        {
            var consumer = new TestMessageConsumer <PongMessage>();

            LocalBus.Subscribe(consumer);

            var message  = new PingMessage();
            var response = new PongMessage(message.CorrelationId);

            RemoteBus.Subscribe <PingMessage>(m =>
            {
                RemoteBus.Publish(response);
                throw new ApplicationException("Boing!");
            });

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(response, _timeout);
        }
        public void It_should_leave_the_message_in_the_queue_if_an_exception_is_thrown()
        {
            FutureMessage <PingMessage> future = new FutureMessage <PingMessage>();

            RemoteBus.Subscribe <PingMessage>(m =>
            {
                future.Set(m);

                throw new ApplicationException("Boing!");
            });

            var message = new PingMessage();

            LocalBus.Publish(message);

            future.IsAvailable(_timeout).ShouldBeTrue("Message was not received");

            RemoteEndpoint.ShouldNotContain(message);
        }
Exemplo n.º 11
0
        public void A_reply_should_be_received_by_the_requestor()
        {
            RemoteBus.ShouldHaveRemoteSubscriptionFor <PingMessage>();
            LocalBus.ShouldHaveRemoteSubscriptionFor <PingMessage>();

            var message = new PingMessage();

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

            LocalBus.SubscribeInstance(consumer);

            RemoteBus.ShouldHaveRemoteSubscriptionFor <PongMessage>();

            LocalBus.Publish(message);

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

            consumer.ShouldHaveReceivedMessage(new PongMessage(message.CorrelationId), _timeout);
        }
Exemplo n.º 12
0
        public void Should_execute()
        {
            var received = new TaskCompletionSource<MagicMade>(TestCancellationToken);
            LocalBus.SubscribeHandler<MagicMade>(x => received.SetResult(x));
            Assert.IsTrue(WaitForSubscription<MagicMade>());


            Uri commandUri = GetActivityContext<MakeMagicHappen>().ExecuteUri;
            var command = new MakeMagicHappenCommand("Hello, World.");


            DispatchMessageHandle<MakeMagicHappenCommand> handle = DispatchEndpoint.DispatchMessage(command, commandUri);

            Assert.IsTrue(_accepted.Task.Wait(TestTimeout));
            DispatchAccepted accepted = _accepted.Task.Result;
            Assert.AreEqual(handle.DispatchId, accepted.DispatchId);

            Assert.IsTrue(received.Task.Wait(TestTimeout));
        }
        public void The_destination_address_should_be_properly_set_on_the_message_envelope()
        {
            var ping = new PingMessage();

            var received = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeHandler <PingMessage>(message =>
            {
                Assert.AreEqual(RemoteBus.Endpoint.Address.Uri, LocalBus.Context().DestinationAddress);

                received.Set(message);
            });

            Thread.Sleep(1.Seconds());

            LocalBus.Publish(ping);

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }
        public void A_clean_method_of_a_request_reply_should_be_possible()
        {
            FutureMessage <PongMessage> ponged = new FutureMessage <PongMessage>();

            LocalBus.Subscribe <PingMessage>(x => LocalBus.Publish(new PongMessage(x.CorrelationId)));

            PingMessage ping = new PingMessage();

            LocalBus.MakeRequest(bus => bus.Publish(ping, context => context.SendResponseTo(bus)))
            .When <PongMessage>().RelatedTo(ping.CorrelationId).IsReceived(pong =>
            {
                Assert.AreEqual(ping.CorrelationId, pong.CorrelationId);
                ponged.Set(pong);
            })
            .TimeoutAfter(5.Seconds())
            .Send();

            Assert.IsTrue(ponged.IsAvailable(1.Seconds()), "No response received");
        }
Exemplo n.º 15
0
        public void Ensure_workers_will_respond_to_ping_request()
        {
            int workerAvaiableCountRecieved = 0;
            var messageRecieved             = new ManualResetEvent(false);

            UnsubscribeAction unsubscribe = LocalBus.Subscribe <WorkerAvailable <FirstCommand> >(message =>
            {
                Interlocked.Increment(ref workerAvaiableCountRecieved);
                messageRecieved.Set();
            });

            Instances.ToList().ForEach(x => { x.Value.ControlBus.Endpoint.Send(new PingWorker()); });

            messageRecieved.WaitOne(3.Seconds());

            unsubscribe();

            workerAvaiableCountRecieved.ShouldBeGreaterThan(0);
        }
        public void Multiple_Local_Services_Should_Be_Available()
        {
            var updated = new Future <UpdateMessage>();
            var deleted = new Future <DeleteMessage>();

            LocalBus.SubscribeHandler <UpdateMessage>(updated.Complete);
            LocalBus.SubscribeHandler <DeleteMessage>(deleted.Complete);

            var dm = new DeleteMessage();

            LocalBus.Publish(dm);

            var um = new UpdateMessage();

            LocalBus.Publish(um);

            updated.WaitUntilCompleted(8.Seconds()).ShouldBeTrue("Update not received");
            deleted.WaitUntilCompleted(8.Seconds()).ShouldBeTrue("Delete not received");
        }
        public void The_retry_count_should_be_properly_set_on_the_message_envelope()
        {
            PingMessage ping = new PingMessage();

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

            var retryCount = 69;

            RemoteBus.Subscribe <PingMessage>(message =>
            {
                Assert.AreEqual(retryCount, CurrentMessage.Headers.RetryCount);

                received.Set(message);
            });

            LocalBus.Publish(ping, context => context.SetRetryCount(retryCount));

            Assert.IsTrue(received.IsAvailable(3.Seconds()), "Timeout waiting for message");
        }
Exemplo n.º 18
0
        public void Can_collect_iworkeravaiable_messages()
        {
            int workerAvaiableCountRecieved = 0;
            var messageRecieved             = new ManualResetEvent(false);

            UnsubscribeAction unsubscribe = LocalBus.Subscribe <IWorkerAvailable>(message =>
            {
                Interlocked.Increment(ref workerAvaiableCountRecieved);
                messageRecieved.Set();
            });

            Instances.ToList().ForEach(x => { x.Value.ControlBus.Endpoint.Send(new PingWorker()); });

            messageRecieved.WaitOne(3.Seconds());

            unsubscribe();

            workerAvaiableCountRecieved.ShouldBeGreaterThan(0);
        }
Exemplo n.º 19
0
        public void It_should_be_received_by_multiple_subscribed_consumers()
        {
            var consumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.SubscribeInstance(consumer);

            var messageConsumer = new TestMessageConsumer <PingMessage>();

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

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var message = new PingMessage();

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(message, _timeout);
            messageConsumer.ShouldHaveReceivedMessage(message, _timeout);
        }
Exemplo n.º 20
0
        public void It_should_not_be_received_by_an_uninterested_consumer()
        {
            var messageConsumer = new TestMessageConsumer <PingMessage>();

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

            var consumer = new TestMessageConsumer <PingMessage>();

            RemoteBus.SubscribeInstance(consumer);

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var message = new PingMessage();

            LocalBus.Publish(message);

            consumer.ShouldHaveReceivedMessage(message, _timeout);
            messageConsumer.ShouldNotHaveReceivedMessage(message, TimeSpan.FromSeconds(1));
        }
        public void Any_type_of_send_should_be_supported()
        {
            RemoteBus.Subscribe <PingMessage>(x => RemoteBus.Publish(new PongMessage(x.CorrelationId)));

            PingMessage ping = new PingMessage();

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

            LocalBus.MakeRequest(bus => RemoteBus.Endpoint.Send(ping, context => context.SendResponseTo(bus)))
            .When <PongMessage>().RelatedTo(ping.CorrelationId).IsReceived(pong =>
            {
                Assert.AreEqual(ping.CorrelationId, pong.CorrelationId);
                ponged.Set(pong);
            })
            .TimeoutAfter(5.Seconds())
            .Send();

            Assert.IsTrue(ponged.IsAvailable(1.Seconds()), "No response received");
        }
Exemplo n.º 22
0
        public void The_timeout_should_be_added_to_the_storage()
        {
            var timedOut = new FutureMessage <TimeoutExpired>();

            LocalBus.SubscribeHandler <TimeoutExpired>(timedOut.Set);

            LocalBus.HasSubscription <TimeoutExpired>(8.Seconds()).Any()
            .ShouldBeTrue("No subscription");

            LocalBus.Publish(new ScheduleTimeout(_correlationId, 2.Seconds()));

            Stopwatch watch = Stopwatch.StartNew();

            timedOut.IsAvailable(8.Seconds()).ShouldBeTrue("Did not get timeout message");

            watch.Stop();

            Debug.WriteLine(string.Format("Timeout took {0}ms", watch.ElapsedMilliseconds));
        }
        public void Should_deliver_the_message_to_an_both_interested_consumers()
        {
            var first  = new Future <FirstMessageContract>();
            var second = new Future <SecondMessageContract>();

            // These can't be on the same bus, because we only send a message to an endpoint once
            // maybe we can do something here by changing the outbound context to keep track of tmessage/endpoint uri
            RemoteBus.SubscribeHandler <FirstMessageContract>(first.Complete);
            LocalBus.SubscribeHandler <SecondMessageContract>(second.Complete);

            LocalBus.ShouldHaveSubscriptionFor <FirstMessageContract>();

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

            LocalBus.Publish(message);

            first.WaitUntilCompleted(1.Seconds()).ShouldBeTrue();
            second.WaitUntilCompleted(1.Seconds()).ShouldBeTrue();
        }
        public void Setup()
        {
            _completed = new TaskCompletionSource <RoutingSlipCompleted>(TestCancellationToken);
            _firstActivityCompleted  = new TaskCompletionSource <RoutingSlipActivityCompleted>(TestCancellationToken);
            _secondActivityCompleted = new TaskCompletionSource <RoutingSlipActivityCompleted>(TestCancellationToken);

            LocalBus.SubscribeHandler <RoutingSlipCompleted>(x => _completed.SetResult(x));
            Assert.IsTrue(WaitForSubscription <RoutingSlipCompleted>());

            LocalBus.SubscribeHandler <RoutingSlipActivityCompleted>(x =>
            {
                if (x.ActivityName.Equals("Test"))
                {
                    _firstActivityCompleted.SetResult(x);
                }
                if (x.ActivityName.Equals("SecondTest"))
                {
                    _secondActivityCompleted.SetResult(x);
                }
            });
            Assert.IsTrue(WaitForSubscription <RoutingSlipActivityCompleted>());

            var builder = new RoutingSlipBuilder(Guid.NewGuid());

            ActivityTestContext testActivity = GetActivityContext <TestActivity>();

            builder.AddActivity(testActivity.Name, testActivity.ExecuteUri, new
            {
                Value     = "Hello",
                NullValue = (string)null,
            });

            testActivity = GetActivityContext <SecondTestActivity>();
            builder.AddActivity(testActivity.Name, testActivity.ExecuteUri);

            builder.AddVariable("Variable", "Knife");
            builder.AddVariable("Nothing", null);

            _routingSlip = builder.Build();

            LocalBus.Execute(_routingSlip);
        }
        public void Should_publish_the_completed_event()
        {
            var handled = new ManualResetEvent(false);

            LocalBus.SubscribeHandler <RoutingSlipCompleted>(message => { handled.Set(); });

            Assert.IsTrue(WaitForSubscription <RoutingSlipCompleted>());

            ActivityTestContext testActivity = GetActivityContext <TestActivity>();

            var builder = new RoutingSlipBuilder(Guid.NewGuid());

            builder.AddActivity(testActivity.Name, testActivity.ExecuteUri, new
            {
                Value = "Hello",
            });
            LocalBus.Execute(builder.Build());

            Assert.IsTrue(handled.WaitOne(Debugger.IsAttached ? 5.Minutes() : 30.Seconds()));
        }
        public void Then_a_new_timeout_is_scheduled_and_cancelled_when_the_claim_is_verified()
        {
            var timer      = Stopwatch.StartNew();
            var controller = new ClaimRequestTestCoordinator(LocalBus, 4000);

            using (LocalBus.SubscribeInstance(controller).Disposable())
            {
                RemoteBus.ShouldHaveSubscriptionFor <ClaimRequestCreatedPendingVerificationEvent>();
                RemoteBus.ShouldHaveSubscriptionFor <CardUseVerifiedEvent>();

                bool complete = controller.CreateClaimRequest(1200, "ClaimType", DateTime.Today.AddDays(-10), "provider", "reason");
                Assert.IsTrue(complete, "No timeout scheduled message was received for this claim request.");

                timer.Stop();
                Debug.WriteLine(string.Format("Time to handle message: {0}ms", timer.ElapsedMilliseconds));

                complete = controller.VerifyCardUse();
                Assert.IsTrue(complete, "The timeout should have been cancelled and the saga should be complete.");
            }
        }
        public void A_timeout_handler_should_be_supported()
        {
            bool called = false;

            PingMessage ping = new PingMessage();

            LocalBus.MakeRequest(bus => bus.Publish(ping, context => context.SendResponseTo(bus)))
            .When <PongMessage>().RelatedTo(ping.CorrelationId).IsReceived(pong =>
            {
                Assert.Fail("Should not have gotten a response");
            })
            .TimeoutAfter(1.Seconds())
            .OnTimeout(() =>
            {
                called = true;
            })
            .Send();

            Assert.IsTrue(called, "Did not receive timeout invoker");
        }
Exemplo n.º 28
0
        protected override void TeardownContext()
        {
            RemoteBus.Dispose();
            RemoteBus = null;

            LocalBus.Dispose();
            LocalBus = null;

            Thread.Sleep(500);

            HealthService.Stop();
            HealthService.Dispose();
            HealthService = null;

            SubscriptionService.Stop();
            SubscriptionService.Dispose();
            SubscriptionService = null;

            base.TeardownContext();
        }
Exemplo n.º 29
0
        protected override void EstablishContext()
        {
            base.EstablishContext();

            InMemorySagaRepository <RegisterUserSaga> sagaRepository = SetupSagaRepository <RegisterUserSaga>();

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

            RemoteBus.SubscribeSaga(sagaRepository);

            LocalBus.ShouldHaveSubscriptionFor <RegisterUser>();
            LocalBus.ShouldHaveSubscriptionFor <UserVerificationEmailSent>();
            LocalBus.ShouldHaveSubscriptionFor <UserValidated>();
        }
        public void The_retry_count_should_be_properly_set_on_the_message_envelope()
        {
            var ping = new PingMessage();

            var received = new FutureMessage <PingMessage>();

            int retryCount = 69;

            RemoteBus.SubscribeHandler <PingMessage>(message =>
            {
                Assert.AreEqual(retryCount, LocalBus.Context().RetryCount);

                received.Set(message);
            });

            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();
            LocalBus.Publish(ping, context => context.SetRetryCount(retryCount));

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }