async Task SendTelemetryMultipleInputsTest(ITransportSettings[] transportSettings) { int messagesCount = 30; TestModule sender = null; TestModule receiver = null; string edgeDeviceConnectionString = await SecretsHelper.GetSecretFromConfigKey("edgeCapableDeviceConnStrKey"); IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(edgeDeviceConnectionString); RegistryManager rm = RegistryManager.CreateFromConnectionString(edgeDeviceConnectionString); try { sender = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "sender11", transportSettings); receiver = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "receiver11", transportSettings); await receiver.SetupReceiveMessageHandler("input1"); await receiver.SetupReceiveMessageHandler("input2"); Task <int> task1 = sender.SendMessagesByCountAsync("output1", 0, messagesCount, TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(2)); Task <int> task2 = sender.SendMessagesByCountAsync("output2", 0, messagesCount, TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(3)); int[] sentMessagesCounts = await Task.WhenAll(task1, task2); Assert.Equal(messagesCount, sentMessagesCounts[0]); Assert.Equal(messagesCount, sentMessagesCounts[1]); await Task.Delay(TimeSpan.FromSeconds(20)); ISet <int> receivedMessages = receiver.GetReceivedMessageIndices("input1"); Assert.Equal(messagesCount, receivedMessages.Count); receivedMessages = receiver.GetReceivedMessageIndices("input2"); Assert.Equal(messagesCount, receivedMessages.Count); } finally { if (rm != null) { await rm.CloseAsync(); } if (sender != null) { await sender.Disconnect(); } if (receiver != null) { await receiver.Disconnect(); } } // wait for the connection to be closed on the Edge side await Task.Delay(TimeSpan.FromSeconds(10)); }
async Task <TestModule> GetModule(RegistryManager rm, string hostName, string deviceId, string moduleId, bool isReceiver, ITransportSettings[] transportSettings) { string connStr = await RegistryManagerHelper.GetOrCreateModule(rm, hostName, deviceId, moduleId); TestModule module = await TestModule.CreateAndConnect(connStr, transportSettings); if (isReceiver) { await module.SetupReceiveMessageHandler(); } return(module); }
async Task BackupAndRestoreMessageDeliveryTestBase( ITransportSettings[] transportSettings, int beforeBackupMessageCount, int afterBackupMessageCount, int expectedMessageCountAfterRestore, Action postBackupModifier) { ProtocolHeadFixture protocolHeadFixture = EdgeHubFixtureCollection.GetFixture(); TestModule sender = null; TestModule receiver = null; string edgeDeviceConnectionString = await SecretsHelper.GetSecretFromConfigKey("edgeCapableDeviceConnStrKey"); IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(edgeDeviceConnectionString); RegistryManager rm = RegistryManager.CreateFromConnectionString(edgeDeviceConnectionString); Func <int, TimeSpan> waitTimeComputer = (numberOfMessages) => TimeSpan.FromMinutes(Math.Ceiling(numberOfMessages / 2000d) + 2); try { sender = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "sender1", transportSettings); receiver = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "receiver1", transportSettings); Console.WriteLine($"Sending {beforeBackupMessageCount} messages."); // Send 10 messages before a receiver is registered. Task <int> task1 = sender.SendMessagesByCountAsync("output1", 0, beforeBackupMessageCount, waitTimeComputer(beforeBackupMessageCount)); int sentMessagesCount = await task1; Assert.Equal(beforeBackupMessageCount, sentMessagesCount); TimeSpan waitTime = TimeSpan.FromMinutes(2); Console.WriteLine($"Waiting {waitTime.TotalSeconds} seconds before validating receipt of messages."); // Wait for a while and then close the test fixture which will in turn close the protocol heads and the in-memory DB store thus creating a backup. await Task.Delay(TimeSpan.FromMinutes(2)); await protocolHeadFixture.CloseAsync(); Console.WriteLine("Protocol heads closed."); postBackupModifier(); // Get new fixture to re-initialize the edge hub container. protocolHeadFixture = EdgeHubFixtureCollection.GetFixture(); // Reconnect clients due to C# SDK bug where it illegally attempts to send through closed amqp link sender = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "sender1", transportSettings); receiver = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "receiver1", transportSettings); // Register the message handler now. await receiver.SetupReceiveMessageHandler(); Console.WriteLine($"Sending {afterBackupMessageCount} messages."); // Send more messages after the receiver is registered. Task <int> task2 = sender.SendMessagesByCountAsync("output1", beforeBackupMessageCount, afterBackupMessageCount, TimeSpan.FromMinutes(2)); sentMessagesCount = await task2; Assert.Equal(afterBackupMessageCount, sentMessagesCount); waitTime = waitTimeComputer(expectedMessageCountAfterRestore); Console.WriteLine($"Waiting {waitTime.TotalSeconds} seconds before validating receipt of messages."); // Validate that all the messages were received (both sent earlier and the new messages). await Task.Delay(waitTime); ISet <int> receivedMessages = receiver.GetReceivedMessageIndices(); Assert.Equal(expectedMessageCountAfterRestore, receivedMessages.Count); } finally { if (rm != null) { await rm.CloseAsync(); rm.Dispose(); } if (sender != null) { await sender.Disconnect(); sender.Dispose(); } if (receiver != null) { await receiver.Disconnect(); receiver.Dispose(); } await protocolHeadFixture.CloseAsync(); } // wait for the connection to be closed on the Edge side await Task.Delay(TimeSpan.FromSeconds(10)); }