Ejemplo n.º 1
0
 public MessageEvent(T message, MessageContractV1 contract, AgentContractV1 agent, bool useSettle)
 {
     Message   = message;
     Contract  = contract;
     Agent     = agent;
     UseSettle = useSettle;
 }
Ejemplo n.º 2
0
        public async Task SimpleScheduleSuccessTestApi()
        {
            var context = _workContext.WithTag(_tag);

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

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

            message.ScheduleDate = DateTime.UtcNow.AddSeconds(2);

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

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

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

            readMessage.Should().BeNull();

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

            readMessage = (await _client.Message.DequeueMessageAndDelete(context, _queueName, null)).Value;
            _utility.VerifyMessage(message, readMessage, agent.AgentId, false);

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

            _utility.VerifyHistoryMessage(readMessage, history, _queueName, _agentName);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public async Task MultipleScheduleSuccessTestApi()
        {
            const int scheduleSize = 10;
            var       context      = _workContext.WithTag(_tag);

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

            var list = new List <EnqueueMessageContractV1>();

            foreach (var item in Enumerable.Range(0, scheduleSize))
            {
                EnqueueMessageContractV1 message = _utility.CreateMessage(context, agent.AgentId, _queueName);
                message.ScheduleDate = DateTime.UtcNow.AddSeconds(2);

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

                list.Add(message);
            }

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

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

            readMessage.Should().BeNull();

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

            var readList = new List <MessageContractV1>();

            foreach (var item in Enumerable.Range(0, scheduleSize))
            {
                readMessage = (await _client.Message.DequeueMessageAndDelete(context, _queueName, null)).Value;
                readMessage.Should().NotBeNull();

                readList.Add(readMessage);
            }

            readList.Count.Should().Be(scheduleSize);

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

            var zip = list
                      .OrderBy(x => x.ClientMessageId)
                      .Zip(readList.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, false);
            }

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

            foreach (var item in readList)
            {
                HistoryContractV1 history = (await _client.History.GetHistory(context, item.MessageId)).Value;
                _utility.VerifyHistoryMessage(item, history, _queueName, _agentName);
            }
        }
 private async Task ReadQueue(IWorkContext context, CancellationToken token)
 {
     while (!token.IsCancellationRequested)
     {
         MessageContractV1 readMessage = (await _client.Message.DequeueMessageAndDelete(context, _queueName, null)).Value;
         if (readMessage != null)
         {
             Interlocked.Increment(ref _readCount);
         }
     }
 }
Ejemplo n.º 6
0
        public async Task ClientMultipleExplicitEnqueueDequeueProcessTest()
        {
            var       context      = _workContext.WithTag(_tag);
            var       receiveList  = new List <Tuple <Payload, MessageEvent> >();
            var       sendList     = new List <Payload>();
            const int messageCount = 10;

            CancellationTokenSource tokenSource = new CancellationTokenSource();

            int count = 0;

            Task t = Task.Run(() => _client.ProcessDequeue(
                                  context,
                                  (c, m, e) =>
            {
                receiveList.Add(new Tuple <Payload, MessageEvent>(m, e));
                Interlocked.Increment(ref count);
                return(Task.FromResult(true));
            },
                                  tokenSource.Token,
                                  useSettle: true,
                                  wait: false)
                              );

            foreach (var index in Enumerable.Range(0, messageCount))
            {
                var payload = new Payload
                {
                    ProcessName = Guid.NewGuid().ToString(),
                    IntValue    = index,
                };

                await _client.Enqueue(context, payload);

                sendList.Add(payload);
            }

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

            tokenSource.Cancel();

            t.Wait();
            count.Should().Be(messageCount);

            foreach (var index in Enumerable.Range(0, messageCount))
            {
                VerifyMessage(sendList[index], receiveList[index].Item1);

                MessageContractV1 contract = receiveList[index].Item2.Contract;
                HistoryContractV1 history  = (await _client.Client.History.GetHistory(context, contract.MessageId)).Value;
                _utility.VerifyHistoryMessage(contract, history, _queueName, _agentName);
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
 public void VerifyHistoryMessage(
     MessageContractV1 readMessage,
     HistoryContractV1 history,
     string queueName,
     string agentName,
     int retryCount        = 1,
     SettleType settleType = SettleType.Processed
     )
 {
     history.Should().NotBeNull();
     history.HistoryId.Should().BeGreaterOrEqualTo(0);
     history.MessageId.Should().Be(readMessage.MessageId);
     history.ActivityType.Should().Be(settleType.ToString());
     history.QueueName.Should().Be(queueName);
     history.Cv.Should().Be(readMessage.Cv);
     history.ClientMessageId.Should().Be(readMessage.ClientMessageId);
     history.Payload.Should().Be(readMessage.Payload);
     history.SettleByAgent.Should().Be(agentName);
     history.ErrorMesage.Should().BeNullOrEmpty();
     history.RetryCount.Should().Be(retryCount);
 }
Ejemplo n.º 9
0
        public void VerifyMessage(EnqueueMessageContractV1 message, MessageContractV1 readMessage, int agentId, bool testLock)
        {
            readMessage.Should().NotBeNull();
            readMessage.QueueId.Should().BeGreaterOrEqualTo(0);
            readMessage.MessageId.Should().BeGreaterOrEqualTo(0);
            readMessage.CreatedByAgentId.Should().Be(agentId);
            readMessage.ClientMessageId.Should().Be(message.ClientMessageId);
            readMessage.Cv.Should().Be(message.Cv);
            readMessage.Payload.Should().Be(message.Payload);

            if (testLock)
            {
                readMessage.LockedByAgentId.Should().Be(agentId);
            }

            WorkRequest readWorkRequest = readMessage.Deserialize <WorkRequest>();

            readWorkRequest.Should().NotBeNull();
            readWorkRequest.ProcessName.Should().Be("this process");
            readWorkRequest.Parameters.Should().NotBeNull();
            readWorkRequest.Parameters.Count().Should().Be(3);
        }
Ejemplo n.º 10
0
        public async Task SimpleEnqueueSuccessTestApi()
        {
            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.DequeueMessageAndDelete(context, _queueName, null)).Value;

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

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

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

            _utility.VerifyHistoryMessage(readMessage, history, _queueName, _agentName);
        }
Ejemplo n.º 11
0
 public MessageEvent(MessageContractV1 contract, CancellationToken token, AgentContractV1 agent)
 {
     Contract = contract;
     Token    = token;
     Agent    = agent;
 }