Exemple #1
0
        private async Task Listener2(object parameter)
        {
            CancellationToken cancellationToken = (CancellationToken)parameter;

            QueueStream queue = new QueueStream("localhost:50000", "Demo.Test.Listener", null);

            PollRequest pollRequest = new()
            {
                Queue       = "Order.Test.OutQ.Listener",
                WaitTimeout = 1000,
                MaxItems    = 100,
                AutoAck     = false
            };

            var messagesToAck = new ConcurrentBag <Message>();

            do
            {
                PollResponse response = await queue.Poll(pollRequest);

                if (!response.HasMessages)
                {
                    continue;
                }

                Logger.Technical().Debug($"{response.Messages.Count()} message(s) received from the PollResponse.").Log();

                Parallel.ForEach(response.Messages, async msg =>
                {
                    try
                    {
                        Assert.Equal(1, msg.Tags.Count());

                        Assert.True(Guid.TryParse(Encoding.UTF8.GetString(msg.Body), out var guid));
                        await Task.Delay(100);

                        msg.Ack();
                        Interlocked.Increment(ref touched);
                    }
                    catch (Exception ex)
                    {
                        Logger.Technical().Exception(ex).Log();
                        msg.NAck();
                    }
                });
            } while (!cancellationToken.IsCancellationRequested);

            queue.Close();
        }
    }
Exemple #2
0
        public async Task TestBasicSendAndReceive()
        {
            QueueStream queue = new QueueStream("localhost:50000", "Demo.Test.Basic", null);

            var messages = new List <Message>();
            var tags     = new Dictionary <string, string>
            {
                { "key", "value" }
            };

            for (int i = 0; i < 1000; i++)
            {
                messages.Add(
                    new Message("Order.Test.OutQ.Basic", Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()), string.Empty, Guid.NewGuid().ToString(), tags)
                    );
            }

            var res = await queue.Send(new SendRequest(messages));

            PollRequest pollRequest = new()
            {
                Queue       = "Order.Test.OutQ.Basic",
                WaitTimeout = 1000,
                MaxItems    = 100,
                AutoAck     = false
            };

            var read = 0;

            while (read < 1000)
            {
                PollResponse response = await queue.Poll(pollRequest);

                if (!response.HasMessages)
                {
                    continue;
                }

                Logger.Technical().Debug($"{response.Messages.Count()} message(s) received from the PollResponse.").Log();

                Parallel.ForEach(response.Messages, msg =>
                {
                    try
                    {
                        Assert.Equal(1, msg.Tags.Count());

                        Assert.True(Guid.TryParse(Encoding.UTF8.GetString(msg.Body), out var guid));

                        msg.Ack();
                    }
                    catch (Exception)
                    {
                        msg.NAck();
                    }
                });

                read += response.Messages.Count();
            }

            Assert.Equal(1000, read);
            queue.Close();
        }