Ejemplo n.º 1
0
        public void Many_publish_request_should_work()
        {
            const int repeatCount = 5000;

            LocalBus.SubscribeHandler <PingMessage>(x => LocalBus.Context().Respond(new PongMessage()));

            ManualResetEvent completed = new ManualResetEvent(false);
            int responsesReceived      = 0;

            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < repeatCount; i++)
            {
                LocalBus.PublishRequestAsync(new PingMessage(), x =>
                {
                    x.Handle <PongMessage>(message =>
                    {
                        if (Interlocked.Increment(ref responsesReceived) == repeatCount)
                        {
                            completed.Set();
                        }
                    });
                });
            }

            bool success = completed.WaitOne(60.Seconds());

            Assert.IsTrue(success, "The repeat series did not complete");

            Trace.WriteLine(string.Format("Elapsed Time for {0} requests = {1}", repeatCount, stopwatch.Elapsed));
            Trace.WriteLine(string.Format("Requests Per Second = {0}", repeatCount * 1000 / stopwatch.ElapsedMilliseconds));

            Trace.WriteLine(string.Format("Elapsed Time for {0} messages = {1}", repeatCount * 2, stopwatch.Elapsed));
            Trace.WriteLine(string.Format("Messages Per Second = {0}", repeatCount * 2000 / stopwatch.ElapsedMilliseconds));
        }
Ejemplo n.º 2
0
        public void Perform_a_large_request_response_pool()
        {
            const int repeatCount = 20000;

            int responsesReceived = 0;

            ManualResetEvent completed = new ManualResetEvent(false);

            LocalBus.SubscribeHandler <PingMessage>(x => LocalBus.Context().Respond(new PongMessage(x.CorrelationId)));
            LocalBus.SubscribeHandler <PongMessage>(x =>
            {
                if (Interlocked.Increment(ref responsesReceived) == repeatCount)
                {
                    completed.Set();
                }
            });

            Thread.Sleep(3.Seconds());

            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int index = 0; index < repeatCount; index++)
            {
                LocalBus.Publish(new PingMessage());
            }

            completed.WaitOne(60.Seconds(), true);

            stopwatch.Stop();

            Trace.WriteLine(string.Format("Elapsed Time for {0} messages = {1}", repeatCount * 2, stopwatch.Elapsed));
            Trace.WriteLine(string.Format("Messages Per Second = {0}", repeatCount * 2000 / stopwatch.ElapsedMilliseconds));
        }
Ejemplo n.º 3
0
        public void The_retry_count_should_be_set_on_the_message()
        {
            var future = new FutureMessage <PingMessage>();

            bool first = true;

            LocalBus.SubscribeHandler <PingMessage>(message =>
            {
                if (first)
                {
                    Assert.AreEqual(0, LocalBus.Context().RetryCount);

                    LocalBus.MessageContext <PingMessage>().RetryLater();

                    first = false;
                }
                else
                {
                    Assert.AreEqual(1, LocalBus.Context().RetryCount);

                    future.Set(message);
                }
            });

            LocalBus.ShouldHaveRemoteSubscriptionFor <PingMessage>();

            LocalBus.Publish(new PingMessage());

            Assert.IsTrue(future.IsAvailable(20.Seconds()));
        }
Ejemplo n.º 4
0
        Action <FirstCommand> FirstCommandConsumer(FirstCommand message)
        {
            return(command =>
            {
                ThreadUtil.Sleep(10.Milliseconds());

                var response = new FirstResponse(command.CorrelationId);

                LocalBus.Context().Respond(response);
            });
        }
Ejemplo n.º 5
0
        public void The_source_address_should_pass()
        {
            var received = new FutureMessage <PingMessage>();

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

                received.Set(message);
            });

            LocalBus.Publish(new PingMessage());

            Assert.IsTrue(received.IsAvailable(5.Seconds()), "No message was received");
        }
Ejemplo n.º 6
0
        public void A_random_header_should_pass()
        {
            Guid id = Guid.NewGuid();

            var received = new FutureMessage <PingMessage>();

            LocalBus.SubscribeHandler <PingMessage>(message =>
            {
                Assert.AreEqual(id.ToString(), LocalBus.Context().Headers["RequestId"]);

                received.Set(message);
            });

            LocalBus.Publish(new PingMessage(), context => context.SetHeader("RequestId", id.ToString()));

            Assert.IsTrue(received.IsAvailable(5.Seconds()), "No message was received");
        }
Ejemplo n.º 7
0
        public void The_correlation_id_should_pass()
        {
            Guid id = Guid.NewGuid();

            var received = new FutureMessage <PingMessage>();

            LocalBus.SubscribeHandler <PingMessage>(message =>
            {
                Assert.AreEqual(id.ToString(), LocalBus.Context().CorrelationId);

                received.Set(message);
            });

            LocalBus.Publish(new PingMessage(), context => context.SetCorrelationId(id.ToString()));

            Assert.IsTrue(received.IsAvailable(5.Seconds()), "No message was received");
        }
Ejemplo n.º 8
0
        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.Publish(ping, context => context.SendResponseTo(LocalBus.Endpoint.Address.Uri));

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }
Ejemplo n.º 9
0
        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.Publish(ping);

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }
        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);
            });

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

            Assert.IsTrue(received.IsAvailable(10.Seconds()), "Timeout waiting for message");
        }
Ejemplo n.º 11
0
        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.Publish(ping, context => context.SetRetryCount(retryCount));

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