示例#1
0
        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));
        }
示例#2
0
        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);
        }
示例#3
0
        protected async Task StoreLimitValidationTestAsync()
        {
            int        messagesCount = DefaultMessageCount;
            TestModule sender        = null;
            TestModule receiver      = null;

            string edgeDeviceConnectionString = await SecretsHelper.GetSecretFromConfigKey("edgeCapableDeviceConnStrKey");

            IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(edgeDeviceConnectionString);
            RegistryManager rm   = RegistryManager.CreateFromConnectionString(edgeDeviceConnectionString);
            Guid            guid = Guid.NewGuid();

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

                sender = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "sender1forstorelimits", StoreLimitTestTransportSettings, 0);

                // Send messages to ensure that the max storage size limit is reached.
                int sentMessagesCount = 0;
                await Assert.ThrowsAsync <IotHubThrottledException>(
                    async() => sentMessagesCount = await sender.SendMessagesByCountAndSizeAsync("output1", 0, messagesCount, MessageSize, TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(2)));

                // Wait some more time to allow the delivered messages to be cleaned up due to TTL expiry.
                await Task.Delay(TimeSpan.FromSeconds(60));

                // Sending a few more messages should now succeed.
                messagesCount     = 2;
                sentMessagesCount = await sender.SendMessagesByCountAndSizeAsync("output1", 0, messagesCount, MessageSize, TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(2));

                Assert.Equal(messagesCount, sentMessagesCount);
            }
            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));
        }
示例#4
0
        async Task SendLargeMessageHandleExceptionTest(ITransportSettings[] transportSettings)
        {
            TestModule sender = 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, "sender1", transportSettings);

                Exception ex = null;
                try
                {
                    // create a large message
                    var message = new Message(new byte[400 * 1000]);
                    await sender.SendMessageAsync("output1", message);
                }
                catch (Exception e)
                {
                    ex = e;
                }

                Assert.NotNull(ex);
            }
            finally
            {
                if (rm != null)
                {
                    await rm.CloseAsync();
                }
            }

            // wait for the connection to be closed on the Edge side
            await Task.Delay(TimeSpan.FromSeconds(10));
        }
示例#5
0
        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));
        }