Пример #1
0
        public async Task SimpleEnqueueWithLockSuccessTestApi()
        {
            var context = _workContext.WithTag(_tag);

            AgentContractV1 agent = await _utility.SetupAgentAndQueue(_queueName, _agentName);

            EnqueueMessageContractV1 message = _utility.CreateMessage(context, agent.AgentId, _queueName);

            await _client.Message.EnqueueMessage(context, message);

            await _utility.VerifyQueue(context, _queueName, 1, 0);

            MessageContractV1 readMessage = (await _client.Message.DequeueMessageWithLock(context, _queueName, agent, null)).Value;

            _utility.VerifyMessage(message, readMessage, agent.AgentId, true);

            var settleMessage = new SettleMessageContractV1
            {
                AgentId    = agent.AgentId,
                QueueName  = _queueName,
                SettleType = SettleType.Processed,
                MessageId  = readMessage.MessageId,
            };

            await _client.Message.SettleMessage(context, settleMessage);

            await _utility.VerifyQueue(context, _queueName, 0, 0);

            HistoryContractV1 history = (await _client.History.GetHistory(context, readMessage.MessageId)).Value;

            _utility.VerifyHistoryMessage(readMessage, history, _queueName, _agentName);
        }
        /// <summary>
        /// Settle message for Dequeue and lock operations
        /// </summary>
        /// <param name="context">context</param>
        /// <param name="messageId">message id</param>
        /// <param name="settleType">settle type</param>
        /// <param name="errorMessage">error message (optional)</param>
        /// <returns>task</returns>
        private async Task SettleMessage(IWorkContext context, long messageId, SettleType settleType, string errorMessage = null)
        {
            AgentContractV1 agent = await GetAgent(context);

            var settleMessage = new SettleMessageContractV1
            {
                AgentId      = agent.AgentId,
                QueueName    = QueueName,
                SettleType   = settleType,
                MessageId    = messageId,
                ErrorMessage = errorMessage
            };

            await Client.Message.SettleMessage(context, settleMessage);
        }
        /// <summary>
        /// Settle a message that is locked by an agent
        /// </summary>
        /// <param name="context"></param>
        /// <param name="contract"></param>
        /// <returns>response</returns>
        public async Task <RestResponse> SettleMessage(IWorkContext context, SettleMessageContractV1 contract)
        {
            Verify.IsNotNull(nameof(context), context);
            Verify.IsNotNull(nameof(contract), contract);
            Verify.IsNotEmpty(nameof(contract.QueueName), contract.QueueName);
            context = context.WithTag(_tag);

            return(await CreateClient()
                   .AddPath(contract.QueueName)
                   .AddPath("message")
                   .AddPath(contract.MessageId.ToString())
                   .SetContent(contract)
                   .PostAsync(context)
                   .ToRestResponseAsync(context)
                   .EnsureSuccessStatusCodeAsync(context));
        }
Пример #4
0
        public async Task SimpleEnqueueWithLockTimeoutSuccessTestApi()
        {
            var context = _workContext.WithTag(_tag);

            AgentContractV1 agent = await _utility.SetupAgentAndQueue(_queueName, _agentName, lockValidForSec : 5);

            EnqueueMessageContractV1 message = _utility.CreateMessage(context, agent.AgentId, _queueName);

            await _client.Message.EnqueueMessage(context, message);

            await _utility.VerifyQueue(context, _queueName, 1, 0);

            MessageContractV1 readMessage = (await _client.Message.DequeueMessageWithLock(context, _queueName, agent, null)).Value;
            long saveMessageId            = readMessage.MessageId;

            _utility.VerifyMessage(message, readMessage, agent.AgentId, true);

            MessageContractV1 tempMessage = (await _client.Message.DequeueMessageWithLock(context, _queueName, agent, null)).Value;

            tempMessage.Should().BeNull();

            await Task.Delay(TimeSpan.FromSeconds(10));

            readMessage = (await _client.Message.DequeueMessageWithLock(context, _queueName, agent, null)).Value;
            _utility.VerifyMessage(message, readMessage, agent.AgentId, true);
            readMessage.MessageId.Should().Be(saveMessageId);

            var settleMessage = new SettleMessageContractV1
            {
                AgentId    = agent.AgentId,
                QueueName  = _queueName,
                SettleType = SettleType.Processed,
                MessageId  = readMessage.MessageId,
            };

            await _client.Message.SettleMessage(context, settleMessage);

            await _utility.VerifyQueue(context, _queueName, 0, 0);

            HistoryContractV1 history = (await _client.History.GetHistory(context, readMessage.MessageId)).Value;

            _utility.VerifyHistoryMessage(readMessage, history, _queueName, _agentName, retryCount: 2);
        }
Пример #5
0
        public async Task <IActionResult> SettleMessage(string queueName, long messageId, [FromBody] SettleMessageContractV1 contract)
        {
            Verify.IsNotEmpty(nameof(queueName), queueName);
            Verify.IsNotNull(nameof(contract), contract);
            Verify.Assert(messageId == contract.MessageId, $"{nameof(contract.MessageId)} does not match resource URI");

            RequestContext requestContext = HttpContext.GetRequestContext();
            var            context        = requestContext.Context.WithTag(_tag);

            await _message.SettleMessage(context, messageId, contract.AgentId, contract.SettleType, contract.ErrorMessage);

            return(new StandardActionResult(context));
        }
        private async Task RandomClient(IWorkContext context, MonitorReport report, int clientNumber, CancellationToken token)
        {
            string          agentName = $"{_options.AgentName}_Receive_{clientNumber}";
            AgentContractV1 agent     = (await _client.Message.GetAgentId(context, _options.QueueName, agentName)).Value;

            TimeSpan?waitFor = _options.NoWait ? (TimeSpan?)null : TimeSpan.FromSeconds(30);

            using (var monitor = new MonitorRate(report, agentName))
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        RestResponse <MessageContractV1> readMessage;

                        if (_options.NoLock)
                        {
                            readMessage = await _client.Message.DequeueMessageAndDelete(context, _options.QueueName, waitFor, token);
                        }
                        else
                        {
                            readMessage = await _client.Message.DequeueMessageWithLock(context, _options.QueueName, agent, waitFor, token);
                        }

                        if (readMessage.StatusCode == HttpStatusCode.NoContent)
                        {
                            monitor.AddRetry();
                            await Utility.Delay(TimeSpan.FromMilliseconds(500), token);

                            continue;
                        }

                        WorkRequest workRequest = readMessage.Value.Deserialize <WorkRequest>();

                        lock (_lock)
                        {
                            if (workRequest.MessageCount > _highMessageCount)
                            {
                                _highMessageCount = workRequest.MessageCount;
                            }
                        }

                        await Utility.Delay(_options.Delay, token);

                        if (!_options.NoLock)
                        {
                            var settleMessage = new SettleMessageContractV1
                            {
                                AgentId    = agent.AgentId,
                                QueueName  = _options.QueueName,
                                SettleType = SettleType.Processed,
                                MessageId  = readMessage.Value.MessageId,
                            };

                            await _client.Message.SettleMessage(context, settleMessage);
                        }

                        monitor.IncrementRead();
                    }
                    catch (Exception ex)
                    {
                        monitor.IncrementError(ex.ToString());
                    }
                }
            }
        }
Пример #7
0
        public async Task QueueSizeLimitTestApi()
        {
            const int messageSize = 10;
            var       context     = _workContext.WithTag(_tag);

            AgentContractV1 agent = await _utility.SetupAgentAndQueue(_queueName, _agentName, queueSize : 10);

            var messageList     = new List <EnqueueMessageContractV1>();
            var readMessageList = new List <MessageContractV1>();

            foreach (int index in Enumerable.Range(0, messageSize))
            {
                EnqueueMessageContractV1 message = _utility.CreateMessage(context, agent.AgentId, _queueName);
                messageList.Add(message);

                await _client.Message.EnqueueMessage(context, message);
            }

            await Verify.AssertExceptionAsync <RestConflictException>(async() =>
            {
                EnqueueMessageContractV1 message = _utility.CreateMessage(context, agent.AgentId, _queueName);
                await _client.Message.EnqueueMessage(context, message);
            });

            await _utility.VerifyQueue(context, _queueName, messageSize, 0);

            MessageContractV1 readMessage;

            foreach (int index in Enumerable.Range(0, messageSize))
            {
                readMessage = (await _client.Message.DequeueMessageWithLock(context, _queueName, agent, null)).Value;
                readMessage.Should().NotBeNull();
                readMessageList.Add(readMessage);
            }

            readMessage = (await _client.Message.DequeueMessageWithLock(context, _queueName, agent, null)).Value;
            readMessage.Should().BeNull();

            var zip = messageList
                      .OrderBy(x => x.ClientMessageId)
                      .Zip(readMessageList.OrderBy(x => x.ClientMessageId), (f, s) => new Tuple <EnqueueMessageContractV1, MessageContractV1>(f, s));

            foreach (var item in zip)
            {
                _utility.VerifyMessage(item.Item1, item.Item2, agent.AgentId, true);
            }

            foreach (var item in readMessageList)
            {
                var settleMessage = new SettleMessageContractV1
                {
                    AgentId    = agent.AgentId,
                    QueueName  = _queueName,
                    SettleType = SettleType.Processed,
                    MessageId  = item.MessageId,
                };

                await _client.Message.SettleMessage(context, settleMessage);
            }

            await _utility.VerifyQueue(context, _queueName, 0, 0);

            foreach (var item in readMessageList)
            {
                HistoryContractV1 history = (await _client.History.GetHistory(context, item.MessageId)).Value;
                _utility.VerifyHistoryMessage(item, history, _queueName, _agentName);
            }
        }