private static void MockReceiveMessages(IMessengerService messengerService, string uniqueId, string entityPathBase, string queueName) { messengerService.ReceiveMessages <string>(queueName, _timeout) .Returns(new string[] { uniqueId }); messengerService.ReceiveMessage <SmokeResponse>(entityPathBase, Arg.Any <Predicate <SmokeResponse> >(), _timeout) .Returns(new SmokeResponse { InvocationId = uniqueId }); }
private async Task <SmokeResponse> RunSmokeTest(IMessengerService messengerService, string queueName, Func <Message, Task> action, string topicName, bool useSession) { Guard.IsNullOrWhiteSpace(queueName, nameof(queueName)); Guard.ArgumentNotNull(action, nameof(action)); string uniqueId = Guid.NewGuid().ToString(); IDictionary <string, string> properties = new Dictionary <string, string> { { "smoketest", uniqueId } }; string entityPathBase = !IsDevelopment ? $"{ServiceBusConstants.TopicNames.SmokeTest}/Subscriptions/{uniqueId}" : uniqueId; if (_useMocking) { MockReceiveMessages(messengerService, uniqueId, entityPathBase, queueName); } try { if (!IsDevelopment) { await((IServiceBusService)messengerService).CreateSubscription("smoketest", uniqueId, new TimeSpan(1, 0, 0, 0)); } if (!IsDevelopment && topicName != null) { if (useSession) { await messengerService.SendToTopic(topicName, uniqueId, properties, sessionId : uniqueId); } else { await messengerService.SendToTopic(topicName, uniqueId, properties); } } else { if (useSession) { await messengerService.SendToQueue(queueName, uniqueId, properties, sessionId : uniqueId); } else { await messengerService.SendToQueue(queueName, uniqueId, properties); } } if (IsDevelopment) { IEnumerable <string> smokeResponsesFromFunction = await messengerService.ReceiveMessages <string>(queueName, _timeout); Message message = new Message(); message.UserProperties.Add("smoketest", smokeResponsesFromFunction?.FirstOrDefault(_ => _ == uniqueId)); action = _useMocking ? async(msg) => { await Task.FromResult(msg.UserProperties["smoketest"].Equals(uniqueId)); } : action; await action(message); } return(await messengerService.ReceiveMessage <SmokeResponse>(entityPathBase, _ => _.InvocationId == uniqueId, _timeout)); } finally { if (!IsDevelopment) { await((IServiceBusService)messengerService).DeleteSubscription("smoketest", uniqueId); } else { await((IQueueService)messengerService).DeleteQueue(uniqueId); } if (_useMocking) { CheckServiceBusCalls(messengerService, uniqueId, queueName, topicName, entityPathBase, useSession); } } }