コード例 #1
0
        static async Task CheckMessageInEventHub(IList <IMessage> sentMessages, DateTime startTime)
        {
            string eventHubConnectionString = await SecretsHelper.GetSecretFromConfigKey("eventHubConnStrKey");

            var  eventHubReceiver = new EventHubReceiver(eventHubConnectionString);
            var  cloudMessages    = new List <EventData>();
            bool messagesFound    = false;

            //Add retry mechanism to make sure all the messages sent reached Event Hub. Retry 3 times.
            for (int i = 0; i < EventHubMessageReceivedRetry; i++)
            {
                cloudMessages.AddRange(await eventHubReceiver.GetMessagesFromAllPartitions(startTime));
                messagesFound = MessageHelper.CompareMessagesAndEventData(sentMessages, cloudMessages);
                if (messagesFound)
                {
                    break;
                }
                await Task.Delay(TimeSpan.FromSeconds(20));
            }

            await eventHubReceiver.Close();

            Assert.NotNull(cloudMessages);
            Assert.NotEmpty(cloudMessages);
            Assert.True(messagesFound);
        }
コード例 #2
0
        static async Task CheckMessageInEventHub(Dictionary <string, IList <IMessage> > sentMessagesByDevice, DateTime startTime)
        {
            string eventHubConnectionString = await SecretsHelper.GetSecretFromConfigKey("eventHubConnStrKey");

            var eventHubReceiver            = new EventHubReceiver(eventHubConnectionString);
            var receivedMessagesByPartition = new Dictionary <string, List <EventData> >();

            bool messagesFound = false;

            // If this test becomes flaky, use PartitionReceiver as a background Task to continuously retrieve messages.
            for (int i = 0; i < EventHubMessageReceivedRetry; i++) // Retry for event hub being slow to process messages.
            {
                foreach (string deviceId in sentMessagesByDevice.Keys)
                {
                    if (receivedMessagesByPartition.ContainsKey(deviceId))
                    {
                        receivedMessagesByPartition[deviceId].AddRange(await eventHubReceiver.GetMessagesForDevice(deviceId, startTime));
                    }
                    else
                    {
                        receivedMessagesByPartition[deviceId] = await eventHubReceiver.GetMessagesForDevice(deviceId, startTime);
                    }
                }

                messagesFound = MessageHelper.ValidateSentMessagesWereReceived(sentMessagesByDevice, receivedMessagesByPartition);

                if (messagesFound)
                {
                    break;
                }
                else
                {
                    logger.LogInformation($"Messages not found in event hub for attempt {i}");
                }

                await Task.Delay(TimeSpan.FromSeconds(20));
            }

            foreach (string device in receivedMessagesByPartition.Keys)
            {
                Assert.NotNull(receivedMessagesByPartition[device]);
                Assert.NotEmpty(receivedMessagesByPartition[device]);
            }

            Assert.True(messagesFound);
        }