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);
            }
        }
        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);

            }
        }
Exemple #3
0
        public ServiceQueue(ServiceQueueRotationAlgorithm rotationAlgorithm)
        {
            _messageQueue = new MessageQueueSingle();
            _messageQueueDistributor = new MessageQueueSingleDistributor(_messageQueue, rotationAlgorithm);

            _servers = new ConcurrentDictionary<RedFoxEndpoint, ISocketAccepter>();
            _readerClientSockets = new ConcurrentDictionary<ISocket, IMessageFrameWriter>();
            _writerClientSockets = new ConcurrentDictionary<ISocket, MessageFrameReceiver>();

            _disposedToken = _disposedCancellationTokenSource.Token;
        }
 private Action<CancellationToken> GetRotationFunc(ServiceQueueRotationAlgorithm rotationAlgorithm)
 {
     switch (rotationAlgorithm)
     {
         case ServiceQueueRotationAlgorithm.FirstIdle:
             return LoopFirstIdleAsync;
         case ServiceQueueRotationAlgorithm.LoadBalance:
             return LoopLoadBalanceAsync;
         default:
             throw new NotSupportedException(String.Format("Unsupported ServiceQueueRotationAlgorithm: {0}",
                 rotationAlgorithm));
     }
 }
 public void ServiceQueue_1_Writer_8_Readers(ServiceQueueRotationAlgorithm algorithm)
 {
     ServiceQueue_1_Writer_n_Readers(algorithm, 8);
 }
 public MessageQueueSingleDistributor(MessageQueueSingle messageQueue, ServiceQueueRotationAlgorithm rotationAlgorithm)
 {
     if (messageQueue == null) throw new ArgumentNullException("messageQueue");
     _messageQueue = messageQueue;
     _rotationFunc = GetRotationFunc(rotationAlgorithm);
 }
        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);
            }
        }