示例#1
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);
                    }
        }
        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);
            }
        }
示例#3
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);
            }
        }
示例#4
0
        public void Connect_with_timeout_to_non_existing_endpoint_should_throw_TimeoutException()
        {
            using (var subscriber = new Subscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);

                var sw = Stopwatch.StartNew();

                Assert.Throws<TimeoutException>(() =>
                    subscriber.Connect(endpoint, TimeSpan.FromMilliseconds(100)));

                sw.Stop();
                Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 100);
            }
        }
示例#5
0
        public void Connect_with_timeout_to_non_existing_endpoint_should_throw_TimeoutException()
        {
            using (var subscriber = new Subscriber())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);

                var sw = Stopwatch.StartNew();

                Assert.Throws <TimeoutException>(() =>
                                                 subscriber.Connect(endpoint, TimeSpan.FromMilliseconds(100)));

                sw.Stop();
                Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 100);
            }
        }
示例#6
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)));
                }
        }
示例#7
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Initializing receiver");

                using (Subscriber subscriber = new Subscriber())
                {
                    subscriber.OnReceive += OnReceive;
                    subscriber.Connect();

                    Console.WriteLine("Ready to receive");
                    Console.ReadLine();
                    Console.WriteLine("Press <ENTER> to shut down.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.ToString());
                Console.WriteLine("Press <ENTER> to shut down.");
                Console.ReadLine();
            }
        }
示例#8
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 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);
            }
        }
示例#10
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)));
            }
        }
示例#11
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)));
            }
        }