示例#1
0
        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");
        }
示例#2
0
        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");
        }
示例#3
0
        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_receive_a_response_from_a_valid_request()
        {
            bool responseHandled = false;
            bool faultHandled    = false;

            CompleteEvent.WaitOne(TimeSpan.FromSeconds(2), true);

            LocalBus.PublishRequest(new ValidRequest(), x =>
            {
                x.Handle <Replay>(r =>
                {
                    responseHandled = true;
                    CompleteEvent.Set();
                });
                x.HandleFault((c, f) =>
                {
                    faultHandled = true;
                    CompleteEvent.Set();
                });
            });

            CompleteEvent.WaitOne(TimeSpan.FromSeconds(20), true);

            Assert.That(responseHandled, Is.True);
            Assert.That(faultHandled, Is.False);
        }
        public void Should_receive_a_fault_from_a_failed_request()
        {
            bool responedHendeled = false;
            bool faultHendeled    = false;

            CompleteEvent.WaitOne(TimeSpan.FromSeconds(2), true);

            LocalBus.PublishRequest(new InvalidRequest(), x =>
            {
                x.Handle <Replay>(r =>
                {
                    responedHendeled = true;
                    CompleteEvent.Set();
                });
                x.HandleFault((c, f) =>
                {
                    faultHendeled = true;
                    CompleteEvent.Set();
                });
            });

            CompleteEvent.WaitOne(Debugger.IsAttached ? 5.Minutes() : 20.Seconds(), true);

            Assert.That(responedHendeled, Is.False);
            Assert.That(faultHendeled, Is.True);
        }
示例#6
0
        public void Should_throw_a_handler_exception_on_the_calling_thread()
        {
            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 = 24.Seconds();

            var exception = Assert.Throws <RequestException>(() =>
            {
                LocalBus.PublishRequest(ping, x =>
                {
                    x.Handle <PongMessage>(message =>
                    {
                        pongReceived.Set(message);

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

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

            exception.Response.ShouldBeAnInstanceOf <PongMessage>();
            exception.InnerException.ShouldBeAnInstanceOf <InvalidOperationException>();

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");
        }
示例#7
0
        public void Should_allow_publish_request_with_custom_headers()
        {
            var pongReceived = new FutureMessage <PongMessage>();
            var pingReceived = new FutureMessage <PingMessage>();

            RemoteBus.SubscribeContextHandler <PingMessage>(x =>
            {
                pingReceived.Set(x.Message);
                var transactionIdFromHeader = new Guid(x.Headers["PingTransactionId"]);
                x.Respond(new PongMessage {
                    TransactionId = transactionIdFromHeader
                });
            });
            LocalBus.ShouldHaveSubscriptionFor <PingMessage>();

            var ping = new PingMessage {
                TransactionId = Guid.NewGuid()
            };

            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);
            }, ctx =>
            {
                ctx.SetHeader("PingTransactionId", ping.TransactionId.ToString());
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");
        }
示例#8
0
        public void Should_call_the_timeout_handler_and_not_throw_an_exception()
        {
            var pongReceived  = new FutureMessage <PongMessage>();
            var pingReceived  = new FutureMessage <PingMessage>();
            var timeoutCalled = new FutureMessage <bool>();

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

            var ping = new PingMessage();

            TimeSpan timeout = 2.Seconds();

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

                x.HandleTimeout(timeout, () => timeoutCalled.Set(true));
            });

            pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
            pongReceived.IsAvailable(timeout).ShouldBeFalse("The pong should not have been received");
            timeoutCalled.IsAvailable(timeout).ShouldBeTrue("The timeout handler was not called");
        }