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 message_frame_should_have_MessageTypeId_from_serialized_message()
 {
     var messageFrameCreator = new MessageFrameCreator(DefaultMessageSerialization.Instance);
     var message = new TestMessage();
     var messageFrame = messageFrameCreator.CreateFromMessage(message);
     Assert.AreEqual(message.MessageTypeId, messageFrame.MessageTypeId);
 }
        public void GetResponse_typed_constructor_passed_nothing_Echo_function_is_called()
        {
            var responderWorker = new ResponderWorker<TestMessage>();

            var requestMessage = new TestMessage();
            var response = (TestMessage)responderWorker.GetResponse(requestMessage, null);
            Assert.AreEqual(requestMessage, response);
        }
        public void Map_typed_request_message()
        {
            Func<TestMessage, IMessage> echoFunc = m => new TestMessage("response");
            var factory = new TypeMappedResponderWorkerFactory();

            factory.Map(echoFunc);

            var requestMessage = new TestMessage();
            var response = factory.GetWorkerFor(requestMessage).GetResponse(requestMessage, null);

            Assert.AreEqual("response", ((TestMessage)response).Text);
        }
        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);
                }
            }
        }
        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));
            }
        }
        public void RequestAsync_Response_single_message(RedFoxTransport transport)
        {
            using (var responder = TestHelpers.CreateTestResponder())
            using (var requester = new Requester())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                responder.Bind(endpoint);

                requester.Connect(endpoint);

                Thread.Sleep(100);

                var messageSent = new TestMessage { Text = "Hello" };
                var messageReceived = (TestMessage)requester.RequestAsync(messageSent).Result;

                Assert.AreEqual(messageSent.Text, messageReceived.Text);
            }
        }
        public void RequestAsync_Response_two_messages_wait_for_each_message(RedFoxTransport transport)
        {
            using (var responder = TestHelpers.CreateTestResponder())
                using (var requester = new Requester())
                {
                    var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                    responder.Bind(endpoint);
                    requester.Connect(endpoint);

                    var messagesReceived = new List <TestMessage>();
                    Thread.Sleep(100);

                    var messageSent = new TestMessage {
                        Text = "Hello"
                    };
                    messagesReceived.Add((TestMessage)requester.RequestAsync(messageSent).Result);
                    messagesReceived.Add((TestMessage)requester.RequestAsync(messageSent).Result);

                    Assert.AreEqual(messageSent.Text, messagesReceived[0].Text);
                    Assert.AreEqual(messageSent.Text, messagesReceived[1].Text);
                }
        }
        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 RequestAsync_Response_two_messages_simultaneous(RedFoxTransport transport)
        {
            using (var responder = TestHelpers.CreateTestResponder())
            using (var requester = new Requester())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                responder.Bind(endpoint);
                requester.Connect(endpoint);

                var messagesReceived = new List<TestMessage>();
                Thread.Sleep(100);

                var messageSent = new TestMessage { Text = "Hello" };
                var task1 = requester.RequestAsync(messageSent);
                var task2 = requester.RequestAsync(messageSent);
                Assert.IsTrue(Task.WhenAll(task1, task2).Wait(TimeSpan.FromSeconds(1)));

                messagesReceived.Add((TestMessage)task1.Result);
                messagesReceived.Add((TestMessage)task2.Result);

                Assert.AreEqual(messageSent.Text, messagesReceived[0].Text);
                Assert.AreEqual(messageSent.Text, messagesReceived[1].Text);
            }
        }
        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 IMessage SomeMethodName(TestMessage message)
 {
     return(new TestMessage("hub"));
 }
 public IMessage SomeMethodName(TestMessage message)
 {
     return new TestMessage("hub");
 }
        public void Request_Response_two_messages(RedFoxTransport transport)
        {
            using (var responder = TestHelpers.CreateTestResponder())
            using (var requester = new Requester())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                responder.Bind(endpoint);
                requester.Connect(endpoint);

                var messagesReceived = new List<TestMessage>();
                Thread.Sleep(100);

                var messageSent = new TestMessage { Text = "Hello" };
                messagesReceived.Add((TestMessage)requester.Request(messageSent));
                messagesReceived.Add((TestMessage)requester.Request(messageSent));

                Assert.AreEqual(messageSent.Text, messagesReceived[0].Text);
                Assert.AreEqual(messageSent.Text, messagesReceived[1].Text);
            }
        }
        public void RequestAsync_Cancel_single_message(RedFoxTransport transport)
        {
            using (var responder = TestHelpers.CreateTestResponder(1000))
            using (var requester = new Requester())
            {
                var endpoint = TestHelpers.CreateEndpointForTransport(transport);

                responder.Bind(endpoint);

                requester.Connect(endpoint);

                Thread.Sleep(100);

                var messageSent = new TestMessage { Text = "Hello" };
                var cancellationToken = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)).Token;
                Assert.Throws<AggregateException>(() => requester.RequestAsync(messageSent, cancellationToken).Wait());
            }
        }
        public void Request_Response_different_threads_large_message(RedFoxTransport transport)
        {
            var endpoint = TestHelpers.CreateEndpointForTransport(transport);
            var started = new ManualResetEventSlim();
            var stop = new ManualResetEventSlim();

            Task.Run(() =>
            {
                using (var responder = TestHelpers.CreateTestResponder())
                {
                    responder.Bind(endpoint);
                    started.Set();
                    stop.Wait();
                }
            });

            var largeMessage = new TestMessage { Text = new string('x', 1024 * 1024) };

            TestMessage messageReceived = null;
            Task.Run(() =>
            {
                using (var requester = new Requester())
                {
                    started.Wait(Timeout);

                    requester.Connect(endpoint);

                    messageReceived = (TestMessage)requester.Request(largeMessage);

                    stop.Set();
                }
            });

            try
            {
                Assert.IsTrue(stop.Wait(Timeout));
                Assert.AreEqual(largeMessage.Text, messageReceived.Text);
            }
            finally
            {
                stop.Set();
            }
        }
 public IMessage SomeOtherMethod(TestMessage message)
 {
     return(SomeMethodName(message));
 }
 public IMessage SomeOtherMethod(TestMessage message)
 {
     return SomeMethodName(message);
 }