Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
        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);
        }
Esempio n. 6
0
        /// <summary>
        /// Clears messages in a queue.
        /// Required administration permission.
        /// If server has no implementation for administration authorization, request is not allowed.
        /// </summary>
        public Task <HorseResult> ClearMessages(string queue, bool clearPriorityMessages, bool clearMessages)
        {
            if (!clearPriorityMessages && !clearMessages)
            {
                return(Task.FromResult(HorseResult.Failed()));
            }

            HorseMessage message = new HorseMessage();

            message.Type        = MessageType.Server;
            message.ContentType = KnownContentTypes.ClearMessages;
            message.SetTarget(queue);
            message.WaitResponse = true;
            message.SetMessageId(_client.UniqueIdGenerator.Create());
            message.AddHeader(HorseHeaders.QUEUE_NAME, queue);

            if (clearPriorityMessages)
            {
                message.AddHeader(HorseHeaders.PRIORITY_MESSAGES, "yes");
            }

            if (clearMessages)
            {
                message.AddHeader(HorseHeaders.MESSAGES, "yes");
            }

            return(_client.WaitResponse(message, true));
        }
        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);
        }
        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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
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());
        }
Esempio n. 12
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);
        }
Esempio n. 13
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());
        }
Esempio n. 14
0
        /// <inheritdoc />
        public override void Set(bool successful, object value)
        {
            if (SourceCompleted)
            {
                return;
            }

            SourceCompleted = true;
            Source.SetResult(successful ? HorseResult.Ok() : (HorseResult)value);
        }
Esempio n. 15
0
        protected override async Task Handle(SampleMessage model)
        {
            GiveMeGuidRequest request = new GiveMeGuidRequest
            {
                Foo = "Hello from sample direct message consumer"
            };
            HorseResult <GiveMeGuidResponse> guidResponse = await Program.RouteBus.PublishRequestJson <GiveMeGuidRequest, GiveMeGuidResponse>(request);

            Console.WriteLine($"SAMPLE DIRECT MESSAGE CONSUMED [{guidResponse.Model.Guid}]");
        }
        /// <summary>
        /// Sends a message
        /// </summary>
        public Task <HorseResult> SendAsync(HorseMessage message)
        {
            HorseClient client = GetClient();

            if (client != null && client.IsConnected)
            {
                return(client.SendAsync(message));
            }

            return(Task.FromResult(HorseResult.Failed()));
        }
        /// <summary>
        /// Pushes a message to the queue
        /// </summary>
        public Task <HorseResult> Push(string queue, MemoryStream content, bool waitAcknowledge)
        {
            HorseClient client = GetClient();

            if (client != null && client.IsConnected)
            {
                return(client.Queues.Push(queue, content, waitAcknowledge));
            }

            return(Task.FromResult(HorseResult.Failed()));
        }
        /// <summary>
        /// Pushes a message to the queue
        /// </summary>
        public Task <HorseResult> PushJson(string queue, object jsonObject, bool waitAcknowledge)
        {
            HorseClient client = GetClient();

            if (client != null && client.IsConnected)
            {
                return(client.Queues.PushJson(queue, jsonObject, waitAcknowledge));
            }

            return(Task.FromResult(HorseResult.Failed()));
        }
        /// <summary>
        /// Publishes a message to the router
        /// </summary>
        public Task <HorseResult> Publish(string routerName, MemoryStream content, bool waitAcknowledge)
        {
            HorseClient client = GetClient();

            if (client != null && client.IsConnected)
            {
                return(client.Routers.Publish(routerName, content.ToArray(), null, waitAcknowledge));
            }

            return(Task.FromResult(HorseResult.Failed()));
        }
        /// <summary>
        /// Publishes a message to the router
        /// </summary>
        public Task <HorseResult> PublishJson(string routerName, object jsonObject, bool waitAcknowledge)
        {
            HorseClient client = GetClient();

            if (client != null && client.IsConnected)
            {
                return(client.Routers.PublishJson(routerName, jsonObject, null, waitAcknowledge));
            }

            return(Task.FromResult(HorseResult.Failed()));
        }
Esempio n. 21
0
        /// <summary>
        /// Request a message from Pull queue
        /// </summary>
        public async Task <PullContainer> Pull(PullRequest request, Func <int, HorseMessage, Task> actionForEachMessage = null)
        {
            HorseMessage message = new HorseMessage(MessageType.QueuePullRequest, request.Queue);

            message.SetMessageId(_client.UniqueIdGenerator.Create());
            message.AddHeader(HorseHeaders.COUNT, request.Count);

            if (request.ClearAfter == ClearDecision.AllMessages)
            {
                message.AddHeader(HorseHeaders.CLEAR, "all");
            }
            else if (request.ClearAfter == ClearDecision.PriorityMessages)
            {
                message.AddHeader(HorseHeaders.CLEAR, "High-Priority");
            }
            else if (request.ClearAfter == ClearDecision.Messages)
            {
                message.AddHeader(HorseHeaders.CLEAR, "Default-Priority");
            }

            if (request.GetQueueMessageCounts)
            {
                message.AddHeader(HorseHeaders.INFO, "yes");
            }

            if (request.Order == MessageOrder.LIFO)
            {
                message.AddHeader(HorseHeaders.ORDER, HorseHeaders.LIFO);
            }

            foreach (KeyValuePair <string, string> pair in request.RequestHeaders)
            {
                message.AddHeader(pair.Key, pair.Value);
            }

            PullContainer container = new PullContainer(message.MessageId, request.Count, actionForEachMessage);

            lock (PullContainers)
                PullContainers.Add(message.MessageId, container);

            HorseResult sent = await _client.SendAsync(message);

            if (sent.Code != HorseResultCode.Ok)
            {
                lock (PullContainers)
                    PullContainers.Remove(message.MessageId);

                container.Complete("Error");
            }

            return(await container.GetAwaitableTask());
        }
Esempio n. 22
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);
        }
Esempio n. 23
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);
        }
Esempio n. 24
0
        /// <summary>
        /// Subscribes to all implemenetd queues (Implemented with IQueueConsumer interface)
        /// </summary>
        /// <param name="verify">If true, waits response from server for each join operation</param>
        /// <param name="disconnectOnFail">If any of queues fails to subscribe, disconnected from server</param>
        /// <param name="silent">If true, errors are hidden, no exception thrown</param>
        /// <returns></returns>
        /// <exception cref="NullReferenceException">Thrown if there is no consumer initialized</exception>
        /// <exception cref="HorseSocketException">Thrown if not connected to server</exception>
        public async Task <bool> SubscribeToAllImplementedQueues(bool verify, bool disconnectOnFail = true, bool silent = true)
        {
            if (_observer == null)
            {
                if (silent)
                {
                    return(false);
                }

                throw new NullReferenceException("Consumer is null. Please init consumer first with InitReader methods");
            }

            HorseClient client = GetClient();

            if (client == null)
            {
                if (silent)
                {
                    return(false);
                }

                throw new HorseSocketException("There is no active connection");
            }

            Tuple <string, TypeDeliveryDescriptor>[] items = _observer.GetSubscribedQueues();
            foreach (Tuple <string, TypeDeliveryDescriptor> item in items)
            {
                HorseMessage msg        = item.Item2.CreateMessage(MessageType.Server, item.Item1, KnownContentTypes.Subscribe);
                HorseResult  joinResult = await client.SendAndGetAck(msg);

                if (joinResult.Code == HorseResultCode.Ok)
                {
                    continue;
                }

                if (disconnectOnFail)
                {
                    client.Disconnect();
                }

                if (!silent)
                {
                    throw new HorseQueueException($"Can't subscribe to {item.Item1} queue: {joinResult.Reason} ({joinResult.Code})");
                }

                return(false);
            }

            return(true);
        }
Esempio n. 25
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);
        }
Esempio n. 26
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);
        }
Esempio n. 27
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);
            }
        }
Esempio n. 28
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);
        }
Esempio n. 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());
        }
Esempio n. 30
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);
            }
        }