示例#1
0
 public void publisher_can_bind_multiple_different_endpoints()
 {
     using (var publisher = new Publisher())
     {
         publisher.Bind(new RedFoxEndpoint("/path1"));
         publisher.Bind(new RedFoxEndpoint("/path2"));
     }
 }
示例#2
0
 public void publisher_can_bind_multiple_different_endpoints()
 {
     using (var publisher = new Publisher())
     {
         publisher.Bind(new RedFoxEndpoint("/path1"));
         publisher.Bind(new RedFoxEndpoint("/path2"));
     }
 }
示例#3
0
 public void publishers_bind_same_endpoint_twice_fails()
 {
     using (var publisher = new Publisher())
     {
         var endpoint = new RedFoxEndpoint("/path");
         publisher.Bind(endpoint);
         Assert.Throws <InvalidOperationException>(() => publisher.Bind(endpoint));
     }
 }
示例#4
0
 public void publishers_bind_same_endpoint_twice_fails()
 {
     using (var publisher = new Publisher())
     {
         var endpoint = new RedFoxEndpoint("/path");
         publisher.Bind(endpoint);
         Assert.Throws<InvalidOperationException>(() => publisher.Bind(endpoint));
     }
 }
示例#5
0
        public void publisher_dispose_unbinds_endpoints()
        {
            using (var publisher = new Publisher())
            {
                publisher.Bind(new RedFoxEndpoint("/path1"));
                publisher.Bind(new RedFoxEndpoint("/path2"));
            }

            using (var publisher = new Publisher())
            {
                publisher.Bind(new RedFoxEndpoint("/path1"));
                publisher.Bind(new RedFoxEndpoint("/path2"));
            }
        }
示例#6
0
        public void publisher_dispose_unbinds_endpoints()
        {
            using (var publisher = new Publisher())
            {
                publisher.Bind(new RedFoxEndpoint("/path1"));
                publisher.Bind(new RedFoxEndpoint("/path2"));
            }

            using (var publisher = new Publisher())
            {
                publisher.Bind(new RedFoxEndpoint("/path1"));
                publisher.Bind(new RedFoxEndpoint("/path2"));
            }
        }
        public void One_Publisher_One_Subscriber_Batch_Broadcast()
        {
            using (var publisher = new Publisher())
            using (var subscriber = new Subscriber())
            {
                var endpoint = GetEndpoint();
                publisher.Bind(endpoint);
                subscriber.Connect(endpoint);

                Thread.Sleep(100);

                var counterSignal = new CounterSignal(NumberOfMessagesToReceive);
                subscriber.MessageReceived += (s, m) => counterSignal.Increment();

                var messageSent = new TestMessage();

                var batch = new List<TestMessage>();
                for (var i = 0; i < NumberOfMessagesToReceive; i++)
                    batch.Add(messageSent);

                var sw = Stopwatch.StartNew();
                publisher.Broadcast(batch);
                Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message");
                sw.Stop();

                Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds);
            }
        }
示例#8
0
        public void One_Publisher_Hundred_Subscribers_Single_Broadcasts()
        {
            using (var publisher = new Publisher())
            {
                var endpoint = GetEndpoint();
                publisher.Bind(endpoint);

                var counterSignal = new CounterSignal(NumberOfMessagesToReceive);
                var subscribers   = Enumerable.Range(1, 100).Select(i =>
                {
                    var sub              = new Subscriber();
                    sub.MessageReceived += (s, m) => counterSignal.Increment();
                    sub.Connect(endpoint);
                    return(sub);
                }).ToList();

                Thread.Sleep(100);

                var messageSent = new TestMessage();

                var sw = Stopwatch.StartNew();
                for (var i = 0; i < NumberOfMessagesToReceive / 100; i++)
                {
                    publisher.Broadcast(messageSent);
                }
                Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message");
                sw.Stop();

                subscribers.ForEach(sub => sub.Dispose());

                Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds);
            }
        }
示例#9
0
        public void Subscriber_sends_message_Publisher_receives_message(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var subscriber = new TestSubscriber())
                {
                    var endpoint   = TestHelpers.CreateEndpointForTransport(transport);
                    var eventFired = new ManualResetEventSlim();

                    IMessage messageReceived = null;
                    ISocket  messageSocket   = null;
                    publisher.MessageReceived += (s, m) =>
                    {
                        messageSocket   = s;
                        messageReceived = m;
                        eventFired.Set();
                    };

                    ISocket connectedSocket = null;
                    publisher.ClientConnected += (s, m) => { connectedSocket = s; };
                    publisher.Bind(endpoint);

                    subscriber.Connect(endpoint);

                    IMessage messageSent = new TestMessage("test");
                    subscriber.SendMessage(messageSent);

                    Assert.IsTrue(eventFired.Wait(Timeout));
                    Assert.AreEqual(messageSent, messageReceived);
                    Assert.AreEqual(connectedSocket, messageSocket);
                }
        }
示例#10
0
        public void One_Publisher_Two_Subscribers_Single_Broadcasts()
        {
            using (var publisher = new Publisher())
                using (var subscriber1 = new Subscriber())
                    using (var subscriber2 = new Subscriber())
                    {
                        var endpoint = GetEndpoint();
                        publisher.Bind(endpoint);
                        subscriber1.Connect(endpoint);
                        subscriber2.Connect(endpoint);

                        Thread.Sleep(100);

                        var counterSignal = new CounterSignal(NumberOfMessagesToReceive);
                        subscriber1.MessageReceived += (s, m) => counterSignal.Increment();
                        subscriber2.MessageReceived += (s, m) => counterSignal.Increment();

                        var messageSent = new TestMessage();

                        var sw = Stopwatch.StartNew();
                        for (var i = 0; i < NumberOfMessagesToReceive / 2; i++)
                        {
                            publisher.Broadcast(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, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds);
                    }
        }
示例#11
0
        public void one_subscriber_connects_to_one_publisher_receives_message_then_second_subscriber_connects_both_receive_message(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var subscriber1 = new TestSubscriber())
                    using (var subscriber2 = new TestSubscriber())
                    {
                        var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                        publisher.Bind(endpoint);
                        subscriber1.Connect(endpoint);

                        Thread.Sleep(100);

                        var broadcastMessage = new TestMessage {
                            Text = "Hello"
                        };
                        publisher.Broadcast(broadcastMessage);

                        Assert.AreEqual(broadcastMessage, subscriber1.TestMustReceiveMessageWithin(Timeout));

                        subscriber2.Connect(endpoint);

                        Thread.Sleep(100);

                        publisher.Broadcast(broadcastMessage);

                        Assert.AreEqual(broadcastMessage, subscriber1.TestMustReceiveMessageWithin(Timeout));
                        Assert.AreEqual(broadcastMessage, subscriber2.TestMustReceiveMessageWithin(Timeout));
                    }
        }
示例#12
0
 public void two_publishers_same_endpoint_fails()
 {
     using (var publisher1 = new Publisher())
     using (var publisher2 = new Publisher())
     {
         var endpoint = new RedFoxEndpoint("/path");
         publisher1.Bind(endpoint);
         Assert.Throws<InvalidOperationException>(() => publisher2.Bind(endpoint));
     }
 }
示例#13
0
 public void two_publishers_same_endpoint_fails()
 {
     using (var publisher1 = new Publisher())
         using (var publisher2 = new Publisher())
         {
             var endpoint = new RedFoxEndpoint("/path");
             publisher1.Bind(endpoint);
             Assert.Throws <InvalidOperationException>(() => publisher2.Bind(endpoint));
         }
 }
示例#14
0
        public void Subscriber_Disconnect_doesnt_hang(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var subscriber = new TestSubscriber())
                {
                    var endpoint = TestHelpers.CreateEndpointForTransport(transport);
                    publisher.Bind(endpoint);

                    subscriber.Connect(endpoint);
                    subscriber.Disconnect(true, Timeout);
                }
        }
示例#15
0
        public void Subscriber_IsDisconnected_should_be_false_when_connected(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var subscriber = new TestSubscriber())
                {
                    var endpoint = TestHelpers.CreateEndpointForTransport(transport);
                    publisher.Bind(endpoint);
                    subscriber.Connect(endpoint);

                    Assert.IsFalse(subscriber.IsDisconnected);
                }
        }
示例#16
0
        public void Publisher_ClientConnected_event_fires(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var subscriber = new TestSubscriber())
                {
                    var endpoint   = TestHelpers.CreateEndpointForTransport(transport);
                    var eventFired = new ManualResetEventSlim();

                    publisher.Bind(endpoint);
                    publisher.ClientConnected += (s, c) => eventFired.Set();

                    subscriber.Connect(endpoint);

                    Assert.IsTrue(eventFired.Wait(Timeout));
                }
        }
        public void Subscribe_to_Publisher_twice_on_same_endpoint(RedFoxTransport transport)
        {
            for (var i = 0; i < 2; i++)
            {
                using (var publisher = new Publisher())
                using (var subscriber = new TestSubscriber())
                {
                    var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                    publisher.Bind(endpoint);
                    subscriber.Connect(endpoint);

                    var broadcastedMessage = new TestMessage { Text = "Hello" };
                    publisher.Broadcast(broadcastedMessage);
                }
            }
        }
示例#18
0
        public void Subscribe_to_Responder_should_cause_protocol_exception(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var requester = new Requester())
                {
                    var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                    publisher.Bind(endpoint);

                    try
                    {
                        requester.Connect(endpoint);
                    }
                    catch (AggregateException ex)
                    {
                        throw ex.InnerExceptions.First();
                    }
                }
        }
示例#19
0
        public void Subscribe_to_Responder_should_cause_protocol_exception(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var requester = new Requester())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                publisher.Bind(endpoint);

                try
                {
                    requester.Connect(endpoint);
                }
                catch (AggregateException ex)
                {
                    throw ex.InnerExceptions.First();
                }
            }
        }
        public void Subscribe_to_Publisher_receive_single_broadcasted_message(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var subscriber = new TestSubscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                publisher.Bind(endpoint);
                subscriber.Connect(endpoint);

                Thread.Sleep(100);

                var broadcastedMessage = new TestMessage { Text = "Hello" };

                publisher.Broadcast(broadcastedMessage);

                Assert.AreEqual(broadcastedMessage, subscriber.TestMustReceiveMessageWithin(Timeout));
            }
        }
示例#21
0
        public void Subscribe_to_Publisher_twice_on_same_endpoint(RedFoxTransport transport)
        {
            for (var i = 0; i < 2; i++)
            {
                using (var publisher = new Publisher())
                    using (var subscriber = new TestSubscriber())
                    {
                        var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                        publisher.Bind(endpoint);
                        subscriber.Connect(endpoint);

                        var broadcastedMessage = new TestMessage {
                            Text = "Hello"
                        };
                        publisher.Broadcast(broadcastedMessage);
                    }
            }
        }
示例#22
0
        public void publisher_should_ignore_ReceiveTimeout_in_socket_configuration_and_must_not_disconnect_on_timeout()
        {
            using (var publisher = new Publisher())
                using (var subscriber = new Subscriber())
                {
                    var endpoint            = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
                    var socketConfiguration = new SocketConfiguration {
                        ReceiveTimeout = TimeSpan.FromMilliseconds(100)
                    };

                    var disconnected = new ManualResetEventSlim();
                    publisher.ClientDisconnected += s => disconnected.Set();

                    publisher.Bind(endpoint, socketConfiguration);
                    subscriber.Connect(endpoint);

                    Assert.IsFalse(disconnected.Wait(TimeSpan.FromSeconds(1)));
                }
        }
示例#23
0
        public void Subscribe_to_Publisher_receive_single_broadcasted_message(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
                using (var subscriber = new TestSubscriber())
                {
                    var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                    publisher.Bind(endpoint);
                    subscriber.Connect(endpoint);

                    Thread.Sleep(100);

                    var broadcastedMessage = new TestMessage {
                        Text = "Hello"
                    };

                    publisher.Broadcast(broadcastedMessage);

                    Assert.AreEqual(broadcastedMessage, subscriber.TestMustReceiveMessageWithin(Timeout));
                }
        }
示例#24
0
        public void unbind_disconnects_client()
        {
            using (var publisher = new Publisher())
                using (var subscriber = new Subscriber())
                {
                    var endpoint = new RedFoxEndpoint("/path");

                    var connected    = new ManualResetEventSlim();
                    var disconnected = new ManualResetEventSlim();

                    publisher.ClientConnected    += (s, c) => connected.Set();
                    publisher.ClientDisconnected += s => disconnected.Set();

                    publisher.Bind(endpoint);
                    subscriber.Connect(endpoint);

                    Assert.IsTrue(connected.Wait(TimeSpan.FromSeconds(1)));

                    publisher.Unbind(endpoint);

                    Assert.IsTrue(disconnected.Wait(TimeSpan.FromSeconds(1)));
                }
        }
        public void Subscriber_Disconnect_doesnt_hang(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var subscriber = new TestSubscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);
                publisher.Bind(endpoint);

                subscriber.Connect(endpoint);
                subscriber.Disconnect(true, Timeout);
            }
        }
示例#26
0
        public void unbind_disconnects_client()
        {
            using (var publisher = new Publisher())
            using (var subscriber = new Subscriber())
            {
                var endpoint = new RedFoxEndpoint("/path");

                var connected = new ManualResetEventSlim();
                var disconnected = new ManualResetEventSlim();

                publisher.ClientConnected += (s, c) => connected.Set();
                publisher.ClientDisconnected += s => disconnected.Set();

                publisher.Bind(endpoint);
                subscriber.Connect(endpoint);

                Assert.IsTrue(connected.Wait(TimeSpan.FromSeconds(1)));

                publisher.Unbind(endpoint);

                Assert.IsTrue(disconnected.Wait(TimeSpan.FromSeconds(1)));
            }
        }
示例#27
0
        public void subscriber_should_ignore_ReceiveTimeout_in_socket_configuration_and_must_not_disconnect_on_timeout()
        {
            using (var publisher = new Publisher())
            using (var subscriber = new Subscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
                var socketConfiguration = new SocketConfiguration { ReceiveTimeout = TimeSpan.FromMilliseconds(100) };

                var disconnected = new ManualResetEventSlim();
                publisher.ClientDisconnected += s => disconnected.Set();

                publisher.Bind(endpoint);
                subscriber.Connect(endpoint, socketConfiguration);

                Assert.IsFalse(disconnected.Wait(TimeSpan.FromSeconds(1)));
            }
        }
        public void Publisher_Unbound_Subscriber_Disconnected_event_fires(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var subscriber = new TestSubscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);
                var eventFired = new ManualResetEventSlim();

                publisher.Bind(endpoint);

                subscriber.Disconnected += eventFired.Set;
                subscriber.Connect(endpoint);

                publisher.Unbind(endpoint);

                Assert.IsTrue(eventFired.Wait(Timeout));
                Assert.IsTrue(subscriber.IsDisconnected);
            }
        }
        public void One_Publisher_Hundred_Subscribers_Single_Broadcasts()
        {
            using (var publisher = new Publisher())
            {
                var endpoint = GetEndpoint();
                publisher.Bind(endpoint);

                var counterSignal = new CounterSignal(NumberOfMessagesToReceive);
                var subscribers = Enumerable.Range(1, 100).Select(i =>
                {
                    var sub = new Subscriber();
                    sub.MessageReceived += (s, m) => counterSignal.Increment();
                    sub.Connect(endpoint);
                    return sub;
                }).ToList();

                Thread.Sleep(100);

                var messageSent = new TestMessage();

                var sw = Stopwatch.StartNew();
                for (var i = 0; i < NumberOfMessagesToReceive / 100; i++)
                {
                    publisher.Broadcast(messageSent);
                }
                Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message");
                sw.Stop();

                subscribers.ForEach(sub => sub.Dispose());

                Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds);
            }
        }
        public void one_subscriber_connects_to_one_publisher_receives_message_then_second_subscriber_connects_both_receive_message(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var subscriber1 = new TestSubscriber())
            using (var subscriber2 = new TestSubscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                publisher.Bind(endpoint);
                subscriber1.Connect(endpoint);

                Thread.Sleep(100);

                var broadcastMessage = new TestMessage { Text = "Hello" };
                publisher.Broadcast(broadcastMessage);

                Assert.AreEqual(broadcastMessage, subscriber1.TestMustReceiveMessageWithin(Timeout));

                subscriber2.Connect(endpoint);

                Thread.Sleep(100);

                publisher.Broadcast(broadcastMessage);

                Assert.AreEqual(broadcastMessage, subscriber1.TestMustReceiveMessageWithin(Timeout));
                Assert.AreEqual(broadcastMessage, subscriber2.TestMustReceiveMessageWithin(Timeout));
            }
        }
        public void Subscriber_sends_message_Publisher_receives_message(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var subscriber = new TestSubscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);
                var eventFired = new ManualResetEventSlim();

                IMessage messageReceived = null;
                ISocket messageSocket = null;
                publisher.MessageReceived += (s, m) =>
                {
                    messageSocket = s;
                    messageReceived = m;
                    eventFired.Set();
                };

                ISocket connectedSocket = null;
                publisher.ClientConnected += (s, m) => { connectedSocket = s; };
                publisher.Bind(endpoint);
                
                subscriber.Connect(endpoint);

                IMessage messageSent = new TestMessage("test");
                subscriber.SendMessage(messageSent);

                Assert.IsTrue(eventFired.Wait(Timeout));
                Assert.AreEqual(messageSent, messageReceived);
                Assert.AreEqual(connectedSocket, messageSocket);
            }
        }
        public void Subscriber_IsDisconnected_should_be_true_when_disconnected(RedFoxTransport transport)
        {
            using (var publisher = new Publisher())
            using (var subscriber = new TestSubscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);
                publisher.Bind(endpoint);
                subscriber.Connect(endpoint);
                subscriber.Disconnect();

                Assert.IsTrue(subscriber.IsDisconnected);
            }
        }