Example #1
0
 public void ServiceQueue_can_bind_multiple_different_endpoints()
 {
     using (var serviceQueue = new ServiceQueue())
     {
         serviceQueue.Bind(new RedFoxEndpoint("/path1"));
         serviceQueue.Bind(new RedFoxEndpoint("/path2"));
     }
 }
Example #2
0
 public void ServiceQueue_can_bind_multiple_different_endpoints()
 {
     using (var serviceQueue = new ServiceQueue())
     {
         serviceQueue.Bind(new RedFoxEndpoint("/path1"));
         serviceQueue.Bind(new RedFoxEndpoint("/path2"));
     }
 }
Example #3
0
 public void ServiceQueues_bind_same_endpoint_twice_fails()
 {
     using (var serviceQueue = new ServiceQueue())
     {
         var endpoint = new RedFoxEndpoint("/path");
         serviceQueue.Bind(endpoint);
         Assert.Throws <InvalidOperationException>(() => serviceQueue.Bind(endpoint));
     }
 }
Example #4
0
 public void ServiceQueues_bind_same_endpoint_twice_fails()
 {
     using (var serviceQueue = new ServiceQueue())
     {
         var endpoint = new RedFoxEndpoint("/path");
         serviceQueue.Bind(endpoint);
         Assert.Throws<InvalidOperationException>(() => serviceQueue.Bind(endpoint));
     }
 }
Example #5
0
        public void ServiceQueue_AddMessageFrame_connect_multiple_readers_LoadBalance()
        {
            var endpoint1 = new RedFoxEndpoint("/path1");
            var endpoint2 = new RedFoxEndpoint("/path2");

            using (var serviceQueue = new ServiceQueue(ServiceQueueRotationAlgorithm.LoadBalance))
                using (var serviceQueueReader1 = new ServiceQueueReader())
                    using (var serviceQueueReader2 = new ServiceQueueReader())
                    {
                        const int count   = 1000;
                        var       counter = new CounterSignal(count, 0);

                        var messagesReceived1 = new List <IMessage>();
                        serviceQueueReader1.MessageReceived += (s, m) =>
                        {
                            messagesReceived1.Add(m);
                            counter.Increment();
                        };

                        var messagesReceived2 = new List <IMessage>();
                        serviceQueueReader2.MessageReceived += (s, m) =>
                        {
                            messagesReceived2.Add(m);
                            counter.Increment();
                        };

                        var testMessage      = new TestMessage();
                        var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage);

                        serviceQueue.Bind(endpoint1);
                        serviceQueue.Bind(endpoint2);

                        serviceQueueReader1.Connect(endpoint1);
                        serviceQueueReader2.Connect(endpoint2);

                        for (var i = 0; i < count; i++)
                        {
                            serviceQueue.AddMessageFrame(testMessageFrame);
                        }

                        Assert.IsTrue(counter.Wait());
                        Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count);

                        Assert.AreNotEqual(0, messagesReceived1.Count);
                        Assert.AreNotEqual(0, messagesReceived2.Count);

                        var ratio = (decimal)messagesReceived1.Count / messagesReceived2.Count;
                        Assert.Greater(ratio, 0.9);
                        Assert.Less(ratio, 1.1);
                    }
        }
Example #6
0
        public void ServiceQueue_dispose_unbinds_endpoints()
        {
            using (var serviceQueue = new ServiceQueue())
            {
                serviceQueue.Bind(new RedFoxEndpoint("/path1"));
                serviceQueue.Bind(new RedFoxEndpoint("/path2"));
            }

            using (var serviceQueue = new ServiceQueue())
            {
                serviceQueue.Bind(new RedFoxEndpoint("/path1"));
                serviceQueue.Bind(new RedFoxEndpoint("/path2"));
            }
        }
Example #7
0
        public void ServiceQueue_dispose_unbinds_endpoints()
        {
            using (var serviceQueue = new ServiceQueue())
            {
                serviceQueue.Bind(new RedFoxEndpoint("/path1"));
                serviceQueue.Bind(new RedFoxEndpoint("/path2"));
            }

            using (var serviceQueue = new ServiceQueue())
            {
                serviceQueue.Bind(new RedFoxEndpoint("/path1"));
                serviceQueue.Bind(new RedFoxEndpoint("/path2"));
            }
        }
Example #8
0
        private void ServiceQueue_1_Writer_n_Readers(ServiceQueueRotationAlgorithm algorithm, int readers)
        {
            using (var serviceQueue = new ServiceQueue(algorithm))
                using (var writer = new ServiceQueueWriter())
                {
                    var endpoint = GetEndpoint();
                    serviceQueue.Bind(endpoint);

                    var counterSignal = new CounterSignal(NumberOfMessages, 0);

                    for (var i = 0; i < readers; i++)
                    {
                        var reader = new ServiceQueueReader();
                        reader.MessageReceived += (s, m) => counterSignal.Increment();

                        reader.Connect(endpoint);
                    }

                    writer.Connect(endpoint);

                    var messageSent = new TestMessage();

                    var sw = Stopwatch.StartNew();
                    for (var i = 0; i < NumberOfMessages; i++)
                    {
                        writer.SendMessage(messageSent);
                    }
                    Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message");
                    sw.Stop();

                    Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessages, NumberOfMessages / sw.Elapsed.TotalSeconds);
                }
        }
        public void ServiceQueue_connect_AddMessageFrame_single_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm)
        {
            var endpoint = new RedFoxEndpoint("/path");

            using (var serviceQueue = new ServiceQueue(rotationAlgorithm))
            using (var serviceQueueReader = new ServiceQueueReader())
            {
                IMessage messageReceived = null;
                var received = new ManualResetEventSlim();
                serviceQueueReader.MessageReceived += (s, m) =>
                {
                    messageReceived = m;
                    received.Set();
                };

                serviceQueue.Bind(endpoint);
                serviceQueueReader.Connect(endpoint);

                var testMessage = new TestMessage();
                var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage);

                serviceQueue.AddMessageFrame(testMessageFrame);

                Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1)));
                Assert.AreEqual(testMessage, messageReceived);
            }
        }
        public void ServiceQueue_1_Writer_1_Reader(ServiceQueueRotationAlgorithm algorithm)
        {
            using (var serviceQueue = new ServiceQueue(algorithm))
            using (var reader = new ServiceQueueReader())
            using (var writer = new ServiceQueueWriter())
            {
                var endpoint = GetEndpoint();
                serviceQueue.Bind(endpoint);

                var counterSignal = new CounterSignal(NumberOfMessages, 0);
                reader.MessageReceived += (s, m) => counterSignal.Increment();

                reader.Connect(endpoint);
                writer.Connect(endpoint);

                var messageSent = new TestMessage();

                var sw = Stopwatch.StartNew();
                for (var i = 0; i < NumberOfMessages; i++)
                {
                    writer.SendMessage(messageSent);
                }
                Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message");
                sw.Stop();

                Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessages, NumberOfMessages / sw.Elapsed.TotalSeconds);

            }
        }
        public void ServiceQueue_connect_Writer_send_multiple_message_received()
        {
            const int count = 1000;

            using (var serviceQueue = new ServiceQueue())
                using (var writer = new ServiceQueueWriter())
                {
                    var endpoint = new RedFoxEndpoint("/path");

                    var messageFrames = new List <MessageFrame>();
                    var counterSignal = new CounterSignal(count, 0);
                    serviceQueue.MessageFrameReceived += m =>
                    {
                        messageFrames.Add(m);
                        counterSignal.Increment();
                    };

                    serviceQueue.Bind(endpoint);
                    writer.Connect(endpoint);

                    for (var i = 0; i < count; i++)
                    {
                        writer.SendMessage(new TestMessage());
                    }

                    Assert.IsTrue(counterSignal.Wait(TimeSpan.FromSeconds(30)));
                    Assert.AreEqual(count, messageFrames.Count);
                }
        }
Example #12
0
        public void ServiceQueue_AddMessageFrame_connect_single_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm)
        {
            var endpoint = new RedFoxEndpoint("/path");

            using (var serviceQueue = new ServiceQueue(rotationAlgorithm))
                using (var serviceQueueReader = new ServiceQueueReader())
                {
                    IMessage messageReceived = null;
                    var      received        = new ManualResetEventSlim();
                    serviceQueueReader.MessageReceived += (s, m) =>
                    {
                        messageReceived = m;
                        received.Set();
                    };

                    var testMessage      = new TestMessage();
                    var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage);

                    serviceQueue.AddMessageFrame(testMessageFrame);

                    serviceQueue.Bind(endpoint);
                    serviceQueueReader.Connect(endpoint);

                    Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1)));
                    Assert.AreEqual(testMessage, messageReceived);
                }
        }
Example #13
0
 public void two_ServiceQueues_same_endpoint_fails()
 {
     using (var serviceQueue1 = new ServiceQueue())
     using (var serviceQueue2 = new ServiceQueue())
     {
         var endpoint = new RedFoxEndpoint("/path");
         serviceQueue1.Bind(endpoint);
         Assert.Throws<InvalidOperationException>(() => serviceQueue2.Bind(endpoint));
     }
 }
Example #14
0
 public void two_ServiceQueues_same_endpoint_fails()
 {
     using (var serviceQueue1 = new ServiceQueue())
         using (var serviceQueue2 = new ServiceQueue())
         {
             var endpoint = new RedFoxEndpoint("/path");
             serviceQueue1.Bind(endpoint);
             Assert.Throws <InvalidOperationException>(() => serviceQueue2.Bind(endpoint));
         }
 }
        public void ServiceQueueReader_should_obey_ReceiveTimeout_in_socket_configuration_and_disconnects_on_timeout()
        {
            using (var serviceQueue = new ServiceQueue())
            using (var serviceQueueReader = new ServiceQueueReader())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
                var socketConfiguration = new SocketConfiguration { ReceiveTimeout = TimeSpan.FromMilliseconds(100) };

                serviceQueue.Bind(endpoint);
                serviceQueueReader.Connect(endpoint, socketConfiguration);

                var disconnected = new ManualResetEventSlim();
                serviceQueueReader.Disconnected += disconnected.Set;

                Assert.IsTrue(disconnected.Wait(TimeSpan.FromSeconds(1)));
            }
        }
Example #16
0
        public void ServiceQueueReader_should_obey_ReceiveTimeout_in_socket_configuration_and_disconnects_on_timeout()
        {
            using (var serviceQueue = new ServiceQueue())
                using (var serviceQueueReader = new ServiceQueueReader())
                {
                    var endpoint            = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
                    var socketConfiguration = new SocketConfiguration {
                        ReceiveTimeout = TimeSpan.FromMilliseconds(100)
                    };

                    serviceQueue.Bind(endpoint);
                    serviceQueueReader.Connect(endpoint, socketConfiguration);

                    var disconnected = new ManualResetEventSlim();
                    serviceQueueReader.Disconnected += disconnected.Set;

                    Assert.IsTrue(disconnected.Wait(TimeSpan.FromSeconds(1)));
                }
        }
Example #17
0
        public void ServiceQueue_AddMessageFrame_connect_multiple_readers_multiple_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm)
        {
            var endpoint = new RedFoxEndpoint("/path");

            using (var serviceQueue = new ServiceQueue(rotationAlgorithm))
                using (var serviceQueueReader1 = new ServiceQueueReader())
                    using (var serviceQueueReader2 = new ServiceQueueReader())
                    {
                        const int count   = 1000;
                        var       counter = new CounterSignal(count, 0);

                        var messagesReceived1 = new List <IMessage>();
                        serviceQueueReader1.MessageReceived += (s, m) =>
                        {
                            messagesReceived1.Add(m);
                            counter.Increment();
                        };

                        var messagesReceived2 = new List <IMessage>();
                        serviceQueueReader2.MessageReceived += (s, m) =>
                        {
                            messagesReceived2.Add(m);
                            counter.Increment();
                        };

                        var testMessage      = new TestMessage();
                        var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage);

                        for (var i = 0; i < count; i++)
                        {
                            serviceQueue.AddMessageFrame(testMessageFrame);
                        }

                        serviceQueue.Bind(endpoint);

                        serviceQueueReader1.Connect(endpoint);
                        serviceQueueReader2.Connect(endpoint);

                        Assert.IsTrue(counter.Wait(TimeSpan.FromSeconds(10)));
                        Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count);
                    }
        }
        public void ServiceQueue_connect_Writer_send_single_message_MessageFramesCount_is_one()
        {
            using (var serviceQueue = new ServiceQueue())
                using (var writer = new ServiceQueueWriter())
                {
                    var endpoint = new RedFoxEndpoint("/path");

                    var received = new ManualResetEventSlim();
                    serviceQueue.MessageFrameReceived += m => received.Set();

                    serviceQueue.Bind(endpoint);
                    writer.Connect(endpoint);

                    Assert.AreEqual(0, serviceQueue.MessageFramesCount);
                    writer.SendMessage(new TestMessage());

                    Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1)));
                    Assert.AreEqual(1, serviceQueue.MessageFramesCount);
                }
        }
        public void ServiceQueue_connect_Writer_send_single_message_MessageFramesCount_is_one()
        {
            using (var serviceQueue = new ServiceQueue())
            using (var writer = new ServiceQueueWriter())
            {
                var endpoint = new RedFoxEndpoint("/path");

                var received = new ManualResetEventSlim();
                serviceQueue.MessageFrameReceived += m => received.Set();

                serviceQueue.Bind(endpoint);
                writer.Connect(endpoint);

                Assert.AreEqual(0, serviceQueue.MessageFramesCount);
                writer.SendMessage(new TestMessage());

                Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1)));
                Assert.AreEqual(1, serviceQueue.MessageFramesCount);
            }
        }
        public void ServiceQueue_connect_Writer_send_single_message_received_fires_MessageFrameReceived_event()
        {
            using (var serviceQueue = new ServiceQueue())
                using (var writer = new ServiceQueueWriter())
                {
                    var endpoint = new RedFoxEndpoint("/path");

                    MessageFrame messageFrame = null;
                    var          received     = new ManualResetEventSlim();
                    serviceQueue.MessageFrameReceived += m =>
                    {
                        messageFrame = m;
                        received.Set();
                    };

                    serviceQueue.Bind(endpoint);
                    writer.Connect(endpoint);

                    writer.SendMessage(new TestMessage());

                    Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1)));
                    Assert.IsNotNull(messageFrame);
                }
        }
        public void ServiceQueue_connect_Writer_send_single_message_received_fires_MessageFrameReceived_event()
        {
            using (var serviceQueue = new ServiceQueue())
            using (var writer = new ServiceQueueWriter())
            {
                var endpoint = new RedFoxEndpoint("/path");

                MessageFrame messageFrame = null;
                var received = new ManualResetEventSlim();
                serviceQueue.MessageFrameReceived += m =>
                {
                    messageFrame = m;
                    received.Set();
                };

                serviceQueue.Bind(endpoint);
                writer.Connect(endpoint);

                writer.SendMessage(new TestMessage());

                Assert.IsTrue(received.Wait(TimeSpan.FromSeconds(1)));
                Assert.IsNotNull(messageFrame);
            }
        }
        public void ServiceQueue_AddMessageFrame_connect_multiple_readers_LoadBalance()
        {
            var endpoint1 = new RedFoxEndpoint("/path1");
            var endpoint2 = new RedFoxEndpoint("/path2");

            using (var serviceQueue = new ServiceQueue(ServiceQueueRotationAlgorithm.LoadBalance))
            using (var serviceQueueReader1 = new ServiceQueueReader())
            using (var serviceQueueReader2 = new ServiceQueueReader())
            {
                const int count = 1000;
                var counter = new CounterSignal(count, 0);

                var messagesReceived1 = new List<IMessage>();
                serviceQueueReader1.MessageReceived += (s, m) =>
                {
                    messagesReceived1.Add(m);
                    counter.Increment();
                };

                var messagesReceived2 = new List<IMessage>();
                serviceQueueReader2.MessageReceived += (s, m) =>
                {
                    messagesReceived2.Add(m);
                    counter.Increment();
                };

                var testMessage = new TestMessage();
                var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage);

                serviceQueue.Bind(endpoint1);
                serviceQueue.Bind(endpoint2);

                serviceQueueReader1.Connect(endpoint1);
                serviceQueueReader2.Connect(endpoint2);

                for (var i = 0; i < count; i++)
                    serviceQueue.AddMessageFrame(testMessageFrame);

                Assert.IsTrue(counter.Wait());
                Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count);

                Assert.AreNotEqual(0, messagesReceived1.Count);
                Assert.AreNotEqual(0, messagesReceived2.Count);

                var ratio = (decimal)messagesReceived1.Count / messagesReceived2.Count;
                Assert.Greater(ratio, 0.9);
                Assert.Less(ratio, 1.1);
            }
        }
        public void ServiceQueue_AddMessageFrame_connect_multiple_readers_multiple_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm)
        {
            var endpoint = new RedFoxEndpoint("/path");

            using (var serviceQueue = new ServiceQueue(rotationAlgorithm))
            using (var serviceQueueReader1 = new ServiceQueueReader())
            using (var serviceQueueReader2 = new ServiceQueueReader())
            {
                const int count = 1000;
                var counter = new CounterSignal(count, 0);

                var messagesReceived1 = new List<IMessage>();
                serviceQueueReader1.MessageReceived += (s, m) =>
                {
                    messagesReceived1.Add(m);
                    counter.Increment();
                };

                var messagesReceived2 = new List<IMessage>();
                serviceQueueReader2.MessageReceived += (s, m) =>
                {
                    messagesReceived2.Add(m);
                    counter.Increment();
                };

                var testMessage = new TestMessage();
                var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage);

                for (var i = 0; i < count; i++)
                    serviceQueue.AddMessageFrame(testMessageFrame);

                serviceQueue.Bind(endpoint);

                serviceQueueReader1.Connect(endpoint);
                serviceQueueReader2.Connect(endpoint);

                Assert.IsTrue(counter.Wait(TimeSpan.FromSeconds(10)));
                Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count);
            }
        }
        public void ServiceQueue_connect_Writer_send_multiple_message_received()
        {
            const int count = 1000;

            using (var serviceQueue = new ServiceQueue())
            using (var writer = new ServiceQueueWriter())
            {
                var endpoint = new RedFoxEndpoint("/path");

                var messageFrames = new List<MessageFrame>();
                var counterSignal = new CounterSignal(count, 0);
                serviceQueue.MessageFrameReceived += m =>
                {
                    messageFrames.Add(m);
                    counterSignal.Increment();
                };

                serviceQueue.Bind(endpoint);
                writer.Connect(endpoint);

                for (var i = 0; i < count; i++)
                    writer.SendMessage(new TestMessage());

                Assert.IsTrue(counterSignal.Wait(TimeSpan.FromSeconds(30)));
                Assert.AreEqual(count, messageFrames.Count);
            }
        }