Example #1
0
        public async Task Delayed()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start(300, 300);

            HorseQueue queue = server.Server.FindQueue("push-a");

            queue.Options.PutBackDelay = 2000;
            queue.Options.Acknowledge  = QueueAckDecision.WaitForAcknowledge;
            server.PutBack             = PutBackDecision.Start;

            HorseClient producer = new HorseClient();
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            await producer.Queues.Push("push-a", "First", false);

            await Task.Delay(100);

            await producer.Queues.Push("push-a", "Second", false);

            await Task.Delay(200);

            Assert.Equal(2, queue.MessageCount());

            int         receivedMessages = 0;
            HorseClient consumer         = new HorseClient();

            consumer.ClientId = "consumer";
            await consumer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(consumer.IsConnected);
            consumer.MessageReceived += async(c, m) =>
            {
                receivedMessages++;
                await consumer.Queues.Unsubscribe("push-a", true);

                await Task.Delay(1000);

                await consumer.SendNegativeAck(m);
            };

            HorseResult joined = await consumer.Queues.Subscribe("push-a", true);

            Assert.Equal(HorseResultCode.Ok, joined.Code);

            await Task.Delay(1500);

            Assert.Equal(1, receivedMessages);
            Assert.Equal(1, queue.MessageCount());
            await Task.Delay(3000);

            Assert.Equal(2, queue.MessageCount());
        }
Example #2
0
        public async Task MultipleQueue()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start(300, 300);

            Router router = new Router(server.Server, "router", RouteMethod.Distribute);

            router.AddBinding(new QueueBinding("qbind-1", "push-a", 0, BindingInteraction.None));
            router.AddBinding(new QueueBinding("qbind-2", "push-a-cc", 0, BindingInteraction.None));
            server.Server.AddRouter(router);

            HorseClient producer = new HorseClient();
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            HorseResult result = await producer.Routers.Publish("router", "Hello, World!", true);

            Assert.Equal(HorseResultCode.Ok, result.Code);

            HorseQueue queue1 = server.Server.FindQueue("push-a");
            HorseQueue queue2 = server.Server.FindQueue("push-a-cc");

            Assert.Equal(1, queue1.MessageCount());
            Assert.Equal(1, queue2.MessageCount());
        }
Example #3
0
        public async Task RoundRobin()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start(300, 300);

            Router router = new Router(server.Server, "router", RouteMethod.RoundRobin);

            router.AddBinding(new QueueBinding("qbind-1", "push-a", 5, BindingInteraction.None));
            router.AddBinding(new QueueBinding("qbind-2", "push-a-cc", 10, BindingInteraction.None));
            router.AddBinding(new DirectBinding("dbind-1", "client-1", 20, BindingInteraction.None));
            router.AddBinding(new DirectBinding("dbind-2", "client-2", 0, BindingInteraction.None));
            server.Server.AddRouter(router);

            HorseClient producer = new HorseClient();
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            HorseClient client1 = new HorseClient();

            client1.ClientId = "client-1";
            await client1.ConnectAsync("hmq://localhost:" + port);

            Assert.True(client1.IsConnected);

            HorseClient client2 = new HorseClient();

            client2.ClientId = "client-2";
            await client2.ConnectAsync("hmq://localhost:" + port);

            Assert.True(client2.IsConnected);

            int client1Received = 0;
            int client2Received = 0;

            client1.MessageReceived += (c, m) => client1Received++;
            client2.MessageReceived += (c, m) => client2Received++;

            for (int i = 0; i < 5; i++)
            {
                HorseResult result = await producer.Routers.Publish("router", "Hello, World!", true);

                Assert.Equal(HorseResultCode.Ok, result.Code);
            }

            await Task.Delay(500);

            HorseQueue queue1 = server.Server.FindQueue("push-a");
            HorseQueue queue2 = server.Server.FindQueue("push-a-cc");

            Assert.Equal(1, queue1.MessageCount());
            Assert.Equal(1, queue2.MessageCount());

            Assert.Equal(1, client2Received);
            Assert.Equal(2, client1Received);
        }
Example #4
0
 /// <summary>
 /// Triggers queue created, updated or deleted events
 /// </summary>
 public void Trigger(HorseQueue queue, string node = null)
 {
     base.Trigger(new QueueEvent
     {
         Name             = queue.Name,
         Topic            = queue.Topic,
         Status           = queue.Status.ToString(),
         Messages         = queue.MessageCount(),
         PriorityMessages = queue.PriorityMessageCount(),
         Node             = node
     });
 }
Example #5
0
        public async Task AcknowledgeTimeout()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start(300, 300);

            HorseQueue queue = server.Server.FindQueue("rr-a");

            Assert.NotNull(queue);
            queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(2);
            queue.Options.Acknowledge        = QueueAckDecision.WaitForAcknowledge;
            server.PutBack = PutBackDecision.End;

            HorseClient producer = new HorseClient();
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            int         received = 0;
            HorseClient consumer = new HorseClient();

            consumer.ResponseTimeout  = TimeSpan.FromSeconds(4);
            consumer.ClientId         = "consumer";
            consumer.MessageReceived += async(client, message) => received++;

            await consumer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(consumer.IsConnected);
            HorseResult joined = await consumer.Queues.Subscribe("rr-a", true);

            Assert.Equal(HorseResultCode.Ok, joined.Code);

            for (int i = 0; i < 10; i++)
            {
                await producer.Queues.Push("rr-a", "Hello, World!", false);
            }

            await Task.Delay(3500);

            Assert.Equal(2, received);

            //1 msg is pending ack, 1 msg is prepared and ready to send (waiting for ack) and 8 msgs are in queue
            Assert.Equal(8, queue.MessageCount());
        }
Example #6
0
        public async Task SingleQueueSingleDirectAckFromQueue()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start(300, 300);

            server.SendAcknowledgeFromMQ = true;

            Router router = new Router(server.Server, "router", RouteMethod.Distribute);

            router.AddBinding(new QueueBinding("qbind-1", "push-a", 0, BindingInteraction.Response));
            router.AddBinding(new DirectBinding("dbind-1", "client-1", 0, BindingInteraction.None));
            server.Server.AddRouter(router);

            HorseClient producer = new HorseClient();
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            bool        client1Received = false;
            HorseClient client1         = new HorseClient();

            client1.ClientId = "client-1";
            await client1.ConnectAsync("hmq://localhost:" + port);

            client1.MessageReceived += (c, m) => client1Received = true;
            Assert.True(client1.IsConnected);

            HorseQueue queue1 = server.Server.FindQueue("push-a");

            HorseResult result = await producer.Routers.Publish("router", "Hello, World!", true);

            Assert.Equal(HorseResultCode.Ok, result.Code);

            await Task.Delay(500);

            Assert.Equal(1, queue1.MessageCount());
            Assert.True(client1Received);
        }
Example #7
0
        public async Task SingleQueueSingleDirectResponseFromDirect()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start(300, 300);

            Router router = new Router(server.Server, "router", RouteMethod.Distribute);

            router.AddBinding(new QueueBinding("qbind-1", "push-a", 0, BindingInteraction.None));
            router.AddBinding(new DirectBinding("dbind-1", "client-1", 0, BindingInteraction.Response));
            server.Server.AddRouter(router);

            HorseClient producer = new HorseClient();
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            HorseClient client1 = new HorseClient();

            client1.ClientId = "client-1";
            await client1.ConnectAsync("hmq://localhost:" + port);

            client1.MessageReceived += (c, m) =>
            {
                HorseMessage response = m.CreateResponse(HorseResultCode.Ok);
                response.SetStringContent("Response");
                client1.SendAsync(response);
            };
            Assert.True(client1.IsConnected);

            HorseQueue queue1 = server.Server.FindQueue("push-a");

            HorseMessage message = await producer.Routers.PublishRequest("router", "Hello, World!");

            Assert.NotNull(message);
            Assert.Equal("Response", message.GetStringContent());
            Assert.Equal(1, queue1.MessageCount());
        }
Example #8
0
        /// <summary>
        /// Finds and dequeues a message from the queue.
        /// If there is not message, returns null
        /// </summary>
        private async Task <Tuple <QueueMessage, int, int> > DequeueMessage(bool fifo, bool getInfo, ClearDecision clear)
        {
            QueueMessage message          = null;
            int          prioMessageCount = 0;
            int          messageCount     = 0;

            await _queue.RunInListSync(() =>
            {
                //pull from prefential messages
                if (_queue.PriorityMessagesList.Count > 0)
                {
                    if (fifo)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.PriorityMessagesList.First.Value;
                        _queue.PriorityMessagesList.RemoveFirst();
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.PriorityMessagesList.Last.Value;
                        _queue.PriorityMessagesList.RemoveLast();
                    }

                    if (message != null)
                    {
                        message.IsInQueue = false;
                    }
                }

                //if there is no prefential message, pull from standard messages
                if (message == null && _queue.MessagesList.Count > 0)
                {
                    if (fifo)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.MessagesList.First.Value;
                        _queue.MessagesList.RemoveFirst();
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.MessagesList.Last.Value;
                        _queue.MessagesList.RemoveLast();
                    }

                    if (message != null)
                    {
                        message.IsInQueue = false;
                    }
                }

                if (getInfo)
                {
                    prioMessageCount = _queue.PriorityMessageCount();
                    messageCount     = _queue.MessageCount();
                }

                if (clear == ClearDecision.All)
                {
                    _queue.ClearAllMessages();
                }

                else if (clear == ClearDecision.HighPriority)
                {
                    _queue.ClearHighPriorityMessages();
                }

                else if (clear == ClearDecision.DefaultPriority)
                {
                    _queue.ClearRegularMessages();
                }
            });

            return(new Tuple <QueueMessage, int, int>(message, prioMessageCount, messageCount));
        }