예제 #1
0
        public async Task QueueRemoved()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.True(client.IsConnected);
            bool received   = false;
            bool subscribed = await client.Queues.OnRemoved(q =>
            {
                Assert.Equal("pull-a", q.Name);
                received = true;
            });

            Assert.True(subscribed);

            var result = await client.Queues.Remove("pull-a");

            Assert.Equal(HorseResultCode.Ok, result.Code);
            await Task.Delay(250);

            Assert.True(received);
        }
예제 #2
0
        public async Task RequestAcknowledge(bool queueAckIsActive)
        {
            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(3);
            queue.Options.Acknowledge        = queueAckIsActive ? QueueAckDecision.JustRequest : QueueAckDecision.None;

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

            Assert.True(producer.IsConnected);

            HorseClient consumer = new HorseClient();

            consumer.AutoAcknowledge = true;
            consumer.ResponseTimeout = TimeSpan.FromSeconds(4);
            consumer.ClientId        = "consumer";
            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);

            HorseResult ack = await producer.Queues.Push("rr-a", "Hello, World!", true);

            Assert.Equal(queueAckIsActive, ack.Code == HorseResultCode.Ok);
        }
예제 #3
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());
        }
예제 #4
0
        public async Task FillData()
        {
            List <byte[]> items = new List <byte[]>();

            for (int i = 0; i < 10; i++)
            {
                items.Add(Encoding.UTF8.GetBytes("No #" + i));
            }

            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            server.Start(300, 300);

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

            Assert.NotNull(queue);

            QueueFiller filler = new QueueFiller(queue);

            filler.FillData(items, false, true);
            filler.FillData(items, false, false);

            await Task.Delay(500);

            Assert.NotEmpty(queue.PriorityMessages);
            Assert.NotEmpty(queue.Messages);
        }
예제 #5
0
        public async Task FillJson()
        {
            List <QueueMessageA> items = new List <QueueMessageA>();

            for (int i = 0; i < 10; i++)
            {
                items.Add(new QueueMessageA("No #" + i));
            }

            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            server.Start(300, 300);

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

            Assert.NotNull(route);
            Assert.NotNull(push);

            QueueFiller fillerRouteA = new QueueFiller(route);
            QueueFiller fillerPushA  = new QueueFiller(push);

            fillerRouteA.FillJson(items, false, false);
            fillerPushA.FillJson(items, false, false);

            await Task.Delay(500);

            Assert.NotEmpty(route.Messages);
            Assert.NotEmpty(push.Messages);
        }
예제 #6
0
        public async Task WithoutAnyResponse()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

            HorseClient client1 = new HorseClient();
            HorseClient client2 = new HorseClient();

            client1.ClientId = "client-1";
            client2.ClientId = "client-2";

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

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

            Assert.True(client1.IsConnected);
            Assert.True(client2.IsConnected);

            bool received = false;

            client2.MessageReceived += (c, m) => received = m.Source == "client-1";

            HorseMessage message = new HorseMessage(MessageType.DirectMessage, "client-2");

            message.SetStringContent("Hello, World!");

            HorseResult sent = await client1.SendAsync(message);

            Assert.Equal(HorseResultCode.Ok, sent.Code);
            await Task.Delay(1000);

            Assert.True(received);
        }
예제 #7
0
        public async Task WithAcknowledge()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

            HorseClient client1 = new HorseClient();
            HorseClient client2 = new HorseClient();

            client1.ClientId        = "client-1";
            client2.ClientId        = "client-2";
            client2.AutoAcknowledge = true;
            client1.ResponseTimeout = TimeSpan.FromSeconds(14);

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

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

            Assert.True(client1.IsConnected);
            Assert.True(client2.IsConnected);

            bool received = false;

            client2.MessageReceived += (c, m) => received = m.Source == "client-1";

            HorseMessage message = new HorseMessage(MessageType.DirectMessage, "client-2");

            message.SetStringContent("Hello, World!");

            HorseResult sent = await client1.SendAndGetAck(message);

            Assert.Equal(HorseResultCode.Ok, sent.Code);
            Assert.True(received);
        }
예제 #8
0
        public async Task PullCount(int count)
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

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

            for (int i = 0; i < 25; i++)
            {
                await queue.Push("Hello, World");
            }

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

            HorseResult joined = await client.Queues.Subscribe("pull-a", true);

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

            PullRequest request = new PullRequest
            {
                Queue = "pull-a",
                Count = count
            };

            PullContainer container = await client.Queues.Pull(request);

            Assert.Equal(count, container.ReceivedCount);
            Assert.Equal(PullProcess.Completed, container.Status);
        }
예제 #9
0
        public async Task CreateWithProperties()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

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

            Assert.True(client.IsConnected);

            HorseResult created = await client.Queues.Create("queue-test", o =>
            {
                o.AcknowledgeTimeout = 33000;
                o.Status             = MessagingQueueStatus.Pull;
            });

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

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

            Assert.NotNull(queue);

            Assert.Equal(TimeSpan.FromSeconds(33), queue.Options.AcknowledgeTimeout);
            Assert.Equal(QueueStatus.Pull, queue.Status);
        }
예제 #10
0
        public async Task MessageProduced()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

            server.SendAcknowledgeFromMQ = true;

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

            Assert.True(client.IsConnected);
            bool received   = false;
            bool subscribed = await client.Queues.OnMessageProduced("pull-a", q =>
            {
                Assert.Equal("pull-a", q.Queue);
                received = true;
            });

            Assert.True(subscribed);

            var result = await client.Queues.Push("pull-a", "Hello, World!", true);

            Assert.Equal(HorseResultCode.Ok, result.Code);
            await Task.Delay(250);

            Assert.True(received);
        }
예제 #11
0
        public async Task SendToOnlineConsumers(int onlineConsumerCount)
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.True(producer.IsConnected);

            int msgReceived = 0;

            for (int i = 0; i < onlineConsumerCount; i++)
            {
                HorseClient consumer = new HorseClient();
                consumer.ClientId = "consumer-" + i;
                await consumer.ConnectAsync("hmq://localhost:" + port);

                Assert.True(consumer.IsConnected);
                consumer.MessageReceived += (c, m) => Interlocked.Increment(ref msgReceived);
                HorseResult joined = await consumer.Queues.Subscribe("broadcast-a", true);

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

            await producer.Queues.Push("broadcast-a", "Hello, World!", false);

            await Task.Delay(1500);

            Assert.Equal(onlineConsumerCount, msgReceived);
        }
예제 #12
0
        public async Task SendToOfflineConsumers()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.True(producer.IsConnected);

            await producer.Queues.Push("broadcast-a", "Hello, World!", false);

            await Task.Delay(700);

            bool        msgReceived = false;
            HorseClient consumer    = new HorseClient();

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

            Assert.True(consumer.IsConnected);
            consumer.MessageReceived += (c, m) => msgReceived = true;
            HorseResult joined = await consumer.Queues.Subscribe("broadcast-a", true);

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

            await Task.Delay(800);

            Assert.False(msgReceived);
        }
예제 #13
0
        public async Task AutoNackWithPutBackDelay()
        {
            Registrar   registrar = new Registrar();
            TestHorseMq server    = new TestHorseMq();

            server.SendAcknowledgeFromMQ = false;
            await server.Initialize();

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

            HmqStickyConnector producer = new HmqAbsoluteConnector(TimeSpan.FromSeconds(10));

            producer.AddHost("hmq://localhost:" + port);
            producer.Run();

            HmqStickyConnector consumer = new HmqAbsoluteConnector(TimeSpan.FromSeconds(10));

            consumer.AddHost("hmq://localhost:" + port);
            registrar.Register(consumer);
            consumer.Run();

            await Task.Delay(500);

            Assert.True(producer.IsConnected);
            Assert.True(consumer.IsConnected);

            Model2      model = new Model2();
            HorseResult push  = await producer.Bus.Queue.PushJson(model, true);

            Assert.Equal(HorseResultCode.Failed, push.Code);
            Assert.Equal(1, QueueConsumer2.Instance.Count);
        }
예제 #14
0
        public async Task UnsubscribeToQueue()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

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

            HorseResult joined = await client.Queues.Subscribe("broadcast-a", true);

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

            HorseResult left = await client.Queues.Unsubscribe("broadcast-a", true);

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

            HorseQueue queue = server.Server.Queues.FirstOrDefault();

            Assert.NotNull(queue);

            List <QueueClient> clients = queue.ClientsClone;

            Assert.Empty(clients);
        }
예제 #15
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);
        }
예제 #16
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());
        }
예제 #17
0
        public async Task ConnectDisconnectStress(int concurrentClients, int connectionCount, int minAliveMs, int maxAliveMs)
        {
            Random rnd          = new Random();
            int    connected    = 0;
            int    disconnected = 0;

            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

            for (int i = 0; i < concurrentClients; i++)
            {
                Thread thread = new Thread(async() =>
                {
                    for (int j = 0; j < connectionCount; j++)
                    {
                        try
                        {
                            HorseClient client = new HorseClient();
                            client.Connect("hmq://localhost:" + port);
                            Assert.True(client.IsConnected);
                            Interlocked.Increment(ref connected);
                            await Task.Delay(rnd.Next(minAliveMs, maxAliveMs));
                            client.Disconnect();
                            Interlocked.Increment(ref disconnected);
                            await Task.Delay(50);
                            Assert.True(client.IsConnected);
                        }
                        catch
                        {
                        }
                    }
                });
                thread.Start();
            }

            TimeSpan total   = TimeSpan.FromMilliseconds(maxAliveMs * connectionCount);
            TimeSpan elapsed = TimeSpan.Zero;

            while (elapsed < total)
            {
                elapsed += TimeSpan.FromMilliseconds(100);
                await Task.Delay(100);
            }

            await Task.Delay(maxAliveMs);

            await Task.Delay(3000);

            Assert.Equal(connected, concurrentClients * connectionCount);
            Assert.Equal(disconnected, concurrentClients * connectionCount);
        }
예제 #18
0
        public async Task ConnectWithoutInfo()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

            List <TcpClient> clients = new List <TcpClient>();

            for (int i = 0; i < 50; i++)
            {
                TcpClient client = new TcpClient();
                client.Connect("127.0.0.1", port);
                clients.Add(client);
                Thread.Sleep(20);
                ThreadPool.UnsafeQueueUserWorkItem(async c =>
                {
                    byte[] buffer    = new byte[128];
                    NetworkStream ns = client.GetStream();
                    try
                    {
                        while (c.Connected)
                        {
                            int read = await ns.ReadAsync(buffer);
                            if (read == 0)
                            {
                                c.Close();
                                c.Dispose();
                                break;
                            }
                        }
                    }
                    catch
                    {
                        c.Close();
                        c.Dispose();
                    }
                }, client, false);

                Assert.Equal(0, server.ClientConnected);
            }

            int connectedClients = clients.Count(x => x.Connected);

            Assert.Equal(connectedClients, clients.Count);

            await Task.Delay(10000);

            connectedClients = clients.Count(x => x.Connected);
            Assert.Equal(0, server.ClientConnected);
            Assert.Equal(0, connectedClients);
        }
예제 #19
0
        public async Task RequestAcknowledge()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.NotNull(queue);
            queue.Options.Acknowledge        = QueueAckDecision.JustRequest;
            queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(15);

            HorseClient consumer = new HorseClient();

            consumer.AutoAcknowledge = true;
            consumer.ClientId        = "consumer";

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

            Assert.True(consumer.IsConnected);

            bool msgReceived = false;

            consumer.MessageReceived += (c, m) => msgReceived = true;
            HorseResult joined = await consumer.Queues.Subscribe("pull-a", true);

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

            HorseClient producer = new HorseClient();

            producer.ResponseTimeout = TimeSpan.FromSeconds(15);
            await producer.ConnectAsync("hmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            Task <HorseResult> taskAck = producer.Queues.Push("pull-a", "Hello, World!", true);

            await Task.Delay(500);

            Assert.False(taskAck.IsCompleted);
            Assert.False(msgReceived);
            Assert.Single(queue.Messages);

            consumer.PullTimeout = TimeSpan.FromDays(1);

            PullContainer pull = await consumer.Queues.Pull(PullRequest.Single("pull-a"));

            Assert.Equal(PullProcess.Completed, pull.Status);
            Assert.Equal(1, pull.ReceivedCount);
            Assert.NotEmpty(pull.ReceivedMessages);
        }
예제 #20
0
        public async Task PushWithCC()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.True(producer.IsConnected);

            HorseClient consumer1 = new HorseClient();

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

            HorseClient consumer2 = new HorseClient();

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

            Assert.True(consumer1.IsConnected);
            Assert.True(consumer2.IsConnected);

            int consumer1Msgs = 0;
            int consumer2Msgs = 0;

            consumer1.MessageReceived += (c, m) => { consumer1Msgs++; };
            consumer2.MessageReceived += (c, m) => { consumer2Msgs++; };

            HorseResult joined1 = await consumer1.Queues.Subscribe("push-a", true);

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

            HorseResult joined2 = await consumer2.Queues.Subscribe("push-a-cc", true);

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

            HorseMessage msg = new HorseMessage(MessageType.QueueMessage, "push-a");

            msg.AddHeader(HorseHeaders.CC, "push-a-cc");
            msg.SetStringContent("Hello, World!");

            await producer.SendAsync(msg);

            await Task.Delay(1500);

            Assert.Equal(1, consumer1Msgs);
            Assert.Equal(1, consumer2Msgs);
        }
예제 #21
0
        public async Task ClientUnsubscribed()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.True(client.IsConnected);
            bool received   = false;
            bool subscribed = await client.Queues.OnUnsubscribed("push-a", c =>
            {
                Assert.Equal("push-a", c.Queue);
                received = true;
            });

            Assert.True(subscribed);

            var result = await client.Queues.Subscribe("push-a", true);

            Assert.Equal(HorseResultCode.Ok, result.Code);
            await Task.Delay(250);

            Assert.False(received);
            result = await client.Queues.Unsubscribe("push-a", true);

            await Task.Delay(250);

            Assert.True(received);

            // ReSharper disable once HeuristicUnreachableCode
            received = false;

            bool unsubscribed = await client.Queues.OffUnsubscribed("push-a");

            Assert.True(unsubscribed);

            Assert.Equal(HorseResultCode.Ok, result.Code);
            result = await client.Queues.Subscribe("push-a", true);

            Assert.Equal(HorseResultCode.Ok, result.Code);
            result = await client.Queues.Unsubscribe("push-a", true);

            Assert.Equal(HorseResultCode.Ok, result.Code);
            await Task.Delay(250);

            Assert.False(received);
        }
예제 #22
0
        public async Task SendAndPull()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

            HorseClient consumer = new HorseClient();

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

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

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

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

            Assert.True(producer.IsConnected);

            await producer.Queues.Push("pull-a", "Hello, World!", false);

            await Task.Delay(700);

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

            Assert.NotNull(queue);
            Assert.Single(queue.Messages);

            PullRequest request = new PullRequest();

            request.Queue                 = "pull-a";
            request.Count                 = 1;
            request.ClearAfter            = ClearDecision.None;
            request.GetQueueMessageCounts = false;
            request.Order                 = MessageOrder.FIFO;

            PullContainer container1 = await consumer.Queues.Pull(request);

            Assert.Equal(PullProcess.Completed, container1.Status);
            Assert.NotEmpty(container1.ReceivedMessages);

            PullContainer container2 = await consumer.Queues.Pull(request);

            Assert.Equal(PullProcess.Empty, container2.Status);
            Assert.Empty(container2.ReceivedMessages);
        }
예제 #23
0
        public async Task WaitForAcknowledge()
        {
            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(5);
            queue.Options.Acknowledge        = QueueAckDecision.WaitForAcknowledge;

            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 Task.Delay(100);

                await consumer.SendAck(message);
            };

            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 < 50; i++)
            {
                await producer.Queues.Push("rr-a", "Hello, World!", false);
            }

            await Task.Delay(1050);

            Assert.True(received > 8);
            Assert.True(received < 12);
        }
예제 #24
0
        public async Task MultipleDirect()
        {
            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 DirectBinding("dbind-1", "client-1", 0, 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);

            bool client1Received = false;
            bool client2Received = false;

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

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

            Assert.Equal(HorseResultCode.Ok, result.Code);
            await Task.Delay(500);

            Assert.True(client1Received);
            Assert.True(client2Received);
        }
예제 #25
0
        public async Task UseUniqueMessageId(bool enabled)
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            server.Server.Options.UseMessageId = enabled;
            server.Server.FindQueue("push-a").Options.UseMessageId = false;
            int port = server.Start();

            HorseClient client = new HorseClient();

            client.UseUniqueMessageId = false;

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

            Assert.True(client.IsConnected);

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

            Assert.Equal(HorseResultCode.Ok, joined.Code);
            await Task.Delay(250);

            HorseMessage received = null;

            client.MessageReceived += (c, m) => received = m;

            QueueMessageA a          = new QueueMessageA("A");
            string        serialized = Newtonsoft.Json.JsonConvert.SerializeObject(a);
            MemoryStream  ms         = new MemoryStream(Encoding.UTF8.GetBytes(serialized));
            HorseResult   sent       = await client.Queues.Push("push-a", ms, false);

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

            await Task.Delay(1000);

            Assert.NotNull(received);

            if (enabled)
            {
                Assert.NotNull(received.MessageId);
            }
            else
            {
                Assert.Null(received.MessageId);
            }
        }
예제 #26
0
        public async Task KeepAliveWithPingPong()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

            HorseClient client = new HorseClient();

            client.Data.Properties.Add("Name", "Test-" + port);
            client.Connect("hmq://localhost:" + port + "/path");

            Thread.Sleep(25000);

            Assert.True(client.IsConnected);
            Assert.Equal(1, server.ClientConnected);
        }
예제 #27
0
        public async Task ClientDisconnected()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

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

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

            Assert.True(client.IsConnected);
            bool received   = false;
            bool subscribed = await client.Connections.OnClientDisconnected(c =>
            {
                if (c.Id == "client-2")
                {
                    received = true;
                }
            });

            Assert.True(subscribed);

            HorseClient client2 = new HorseClient();

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

            Assert.True(client2.IsConnected);
            client2.Disconnect();
            await Task.Delay(500);

            Assert.True(received);
            received = false;

            bool unsubscribed = await client.Connections.OffClientDisconnected();

            Assert.True(unsubscribed);

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

            Assert.True(client2.IsConnected);
            client2.Disconnect();
            await Task.Delay(500);

            Assert.False(received);
        }
예제 #28
0
        public async Task PullOrder(bool?fifo)
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

            HorseQueue queue = server.Server.FindQueue("pull-a");
            await queue.Push("First Message");

            await queue.Push("Second Message");

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

            HorseResult joined = await client.Queues.Subscribe("pull-a", true);

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

            PullRequest request = new PullRequest
            {
                Queue = "pull-a",
                Count = 1,
                Order = !fifo.HasValue || fifo.Value ? MessageOrder.FIFO : MessageOrder.LIFO
            };

            PullContainer container = await client.Queues.Pull(request);

            Assert.Equal(PullProcess.Completed, container.Status);

            HorseMessage msg = container.ReceivedMessages.FirstOrDefault();

            Assert.NotNull(msg);

            string content = msg.GetStringContent();

            if (fifo.HasValue && !fifo.Value)
            {
                Assert.Equal("Second Message", content);
            }
            else
            {
                Assert.Equal("First Message", content);
            }
        }
예제 #29
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());
        }
예제 #30
0
        public async Task GetQueueList()
        {
            TestHorseMq server = new TestHorseMq();
            await server.Initialize();

            int port = server.Start();

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

            var result = await client.Queues.List();

            Assert.Equal(HorseResultCode.Ok, result.Result.Code);
            Assert.NotNull(result.Model);
            var queue = result.Model.FirstOrDefault();

            Assert.NotNull(queue);
        }