コード例 #1
0
        private async Task ReceiveMessageRecoveryPoolOverAmqp(
            TestDeviceType type,
            Client.TransportType transport,
            int poolSize,
            int devicesCount,
            string faultType,
            string reason,
            int delayInSec    = FaultInjection.DefaultDelayInSec,
            int durationInSec = FaultInjection.DefaultDurationInSec,
            ConnectionStringAuthScope authScope = ConnectionStringAuthScope.Device)
        {
            // Initialize the service client
            ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString);

            Func <DeviceClient, TestDevice, Task> testOperation = async(deviceClient, testDevice) =>
            {
                (Message msg, string payload, string p1Value) = MessageReceiveE2ETests.ComposeC2dTestMessage(Logger);
                Logger.Trace($"{nameof(FaultInjectionPoolAmqpTests)}: Sending message to device {testDevice.Id}: payload='{payload}' p1Value='{p1Value}'");
                await serviceClient.SendAsync(testDevice.Id, msg)
                .ConfigureAwait(false);

                Logger.Trace($"{nameof(FaultInjectionPoolAmqpTests)}: Preparing to receive message for device {testDevice.Id}");
                await deviceClient.OpenAsync()
                .ConfigureAwait(false);

                await MessageReceiveE2ETests.VerifyReceivedC2DMessageAsync(transport, deviceClient, testDevice.Id, msg, payload, Logger)
                .ConfigureAwait(false);
            };

            Func <IList <DeviceClient>, Task> cleanupOperation = async(deviceClients) =>
            {
                await serviceClient.CloseAsync()
                .ConfigureAwait(false);

                serviceClient.Dispose();

                foreach (DeviceClient deviceClient in deviceClients)
                {
                    deviceClient.Dispose();
                }
            };

            await FaultInjectionPoolingOverAmqp
            .TestFaultInjectionPoolAmqpAsync(
                MessageReceive_DevicePrefix,
                transport,
                poolSize,
                devicesCount,
                faultType,
                reason,
                delayInSec,
                durationInSec,
                (d, t) => { return(Task.FromResult(false)); },
                testOperation,
                cleanupOperation,
                authScope,
                Logger)
            .ConfigureAwait(false);
        }
        private async Task ReceiveMessageRecoveryPoolOverAmqpAsync(
            TestDeviceType type,
            Client.TransportType transport,
            int poolSize,
            int devicesCount,
            string faultType,
            string reason,
            TimeSpan delayInSec    = default,
            TimeSpan durationInSec = default,
            ConnectionStringAuthScope authScope = ConnectionStringAuthScope.Device,
            string proxyAddress = null)
        {
            // Initialize the service client
            using var serviceClient = ServiceClient.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString);

            async Task TestOperationAsync(DeviceClient deviceClient, TestDevice testDevice, TestDeviceCallbackHandler _)
            {
                (Message msg, string payload, string p1Value) = MessageReceiveE2ETests.ComposeC2dTestMessage(Logger);
                Logger.Trace($"{nameof(FaultInjectionPoolAmqpTests)}: Sending message to device {testDevice.Id}: payload='{payload}' p1Value='{p1Value}'");
                await serviceClient.SendAsync(testDevice.Id, msg)
                .ConfigureAwait(false);

                Logger.Trace($"{nameof(FaultInjectionPoolAmqpTests)}: Preparing to receive message for device {testDevice.Id}");
                await deviceClient.OpenAsync()
                .ConfigureAwait(false);

                await MessageReceiveE2ETests.VerifyReceivedC2DMessageAsync(transport, deviceClient, testDevice.Id, msg, payload, Logger)
                .ConfigureAwait(false);
            }

            async Task CleanupOperationAsync(List <DeviceClient> deviceClients, List <TestDeviceCallbackHandler> testDeviceCallbackHandlers)
            {
                await serviceClient.CloseAsync()
                .ConfigureAwait(false);

                serviceClient.Dispose();

                deviceClients.ForEach(deviceClient => deviceClient.Dispose());
                testDeviceCallbackHandlers.ForEach(testDeviceCallbackHandler => testDeviceCallbackHandler.Dispose());
            }

            await FaultInjectionPoolingOverAmqp
            .TestFaultInjectionPoolAmqpAsync(
                MessageReceive_DevicePrefix,
                transport,
                proxyAddress,
                poolSize,
                devicesCount,
                faultType,
                reason,
                delayInSec == TimeSpan.Zero?FaultInjection.DefaultFaultDelay : delayInSec,
                durationInSec == TimeSpan.Zero?FaultInjection.DefaultFaultDuration : durationInSec,
                (d, t, h) => { return(Task.FromResult(false)); },
                TestOperationAsync,
                CleanupOperationAsync,
                authScope,
                Logger)
            .ConfigureAwait(false);
        }
        private async Task ReceiveMessagePoolOverAmqp(
            Client.TransportType transport,
            int poolSize,
            int devicesCount,
            ConnectionStringAuthScope authScope = ConnectionStringAuthScope.Device)
        {
            Dictionary <string, List <string> > messagesSent = new Dictionary <string, List <string> >();

            // Initialize the service client
            ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString);

            Func <DeviceClient, TestDevice, Task> initOperation = async(deviceClient, testDevice) =>
            {
                (Message msg, string messageId, string payload, string p1Value) = MessageReceiveE2ETests.ComposeC2DTestMessage();
                messagesSent.Add(testDevice.Id, new List <string> {
                    payload, p1Value
                });

                await serviceClient.SendAsync(testDevice.Id, msg).ConfigureAwait(false);
            };

            Func <DeviceClient, TestDevice, Task> testOperation = async(deviceClient, testDevice) =>
            {
                _log.WriteLine($"{nameof(MessageReceiveE2EPoolAmqpTests)}: Preparing to receive message for device {testDevice.Id}");
                await deviceClient.OpenAsync().ConfigureAwait(false);

                List <string> msgSent = messagesSent[testDevice.Id];
                string        payload = msgSent[0];
                string        p1Value = msgSent[1];

                await MessageReceiveE2ETests.VerifyReceivedC2DMessageAsync(transport, deviceClient, testDevice.Id, payload, p1Value).ConfigureAwait(false);
            };

            Func <IList <DeviceClient>, Task> cleanupOperation = async(deviceClients) =>
            {
                await serviceClient.CloseAsync().ConfigureAwait(false);

                serviceClient.Dispose();

                foreach (DeviceClient deviceClient in deviceClients)
                {
                    deviceClient.Dispose();
                }

                messagesSent.Clear();
            };

            await PoolingOverAmqp.TestPoolAmqpAsync(
                DevicePrefix,
                transport,
                poolSize,
                devicesCount,
                initOperation,
                testOperation,
                cleanupOperation,
                authScope,
                true).ConfigureAwait(false);
        }
        private async Task ReceiveMessageRecovery(
            TestDeviceType type,
            Client.TransportType transport,
            string faultType,
            string reason,
            int delayInSec)
        {
            using (ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString))
            {
                Func <DeviceClient, TestDevice, Task> init = async(deviceClient, testDevice) =>
                {
                    await serviceClient.OpenAsync().ConfigureAwait(false);

                    if (transport == Client.TransportType.Mqtt_Tcp_Only ||
                        transport == Client.TransportType.Mqtt_WebSocket_Only)
                    {
                        // Dummy ReceiveAsync to ensure mqtt subscription registration before SendAsync() is called on service client.
                        await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
                    }
                };

                Func <DeviceClient, TestDevice, Task> testOperation = async(deviceClient, testDevice) =>
                {
                    string payload, messageId, p1Value;
                    await serviceClient.SendAsync(
                        testDevice.Id,
                        MessageReceiveE2ETests.ComposeC2DTestMessage(out payload, out messageId, out p1Value)).ConfigureAwait(false);

                    await MessageReceiveE2ETests.VerifyReceivedC2DMessageAsync(transport, deviceClient, payload, p1Value).ConfigureAwait(false);
                };

                Func <Task> cleanupOperation = () =>
                {
                    return(serviceClient.CloseAsync());
                };

                await FaultInjection.TestErrorInjectionAsync(
                    DevicePrefix,
                    type,
                    transport,
                    faultType,
                    reason,
                    delayInSec,
                    FaultInjection.DefaultDurationInSec,
                    init,
                    testOperation,
                    cleanupOperation).ConfigureAwait(false);
            }
        }
コード例 #5
0
        private async Task DeviceCombinedClientOperations(
            Client.TransportType transport,
            int poolSize,
            int devicesCount,
            ConnectionStringAuthScope authScope)
        {
            // Initialize service client for service-side operations
            ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString);

            // Message payload for C2D operation
            Dictionary <string, List <string> > messagesSent = new Dictionary <string, List <string> >();

            // Twin properties
            Dictionary <string, List <string> > twinPropertyMap = new Dictionary <string, List <string> >();

            Func <DeviceClient, TestDevice, Task> initOperation = async(deviceClient, testDevice) =>
            {
                IList <Task> initOperations = new List <Task>();

                // Send C2D Message
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Send C2D for device={testDevice.Id}");
                (Message msg, string messageId, string payload, string p1Value) = MessageReceiveE2ETests.ComposeC2DTestMessage();
                messagesSent.Add(testDevice.Id, new List <string> {
                    payload, p1Value
                });
                var sendC2DMessage = serviceClient.SendAsync(testDevice.Id, msg);
                initOperations.Add(sendC2DMessage);

                // Set method handler
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Set direct method {MethodName} for device={testDevice.Id}");
                var methodReceivedTask = MethodE2ETests.SetDeviceReceiveMethod(deviceClient, MethodName);
                initOperations.Add(methodReceivedTask);

                // Set the twin desired properties callback
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Set desired property callback for device={testDevice.Id}");
                var propName  = Guid.NewGuid().ToString();
                var propValue = Guid.NewGuid().ToString();
                twinPropertyMap.Add(testDevice.Id, new List <string> {
                    propName, propValue
                });
                var updateReceivedTask = TwinE2ETests.SetTwinPropertyUpdateCallbackHandlerAsync(deviceClient, propName, propValue);
                initOperations.Add(updateReceivedTask);

                await Task.WhenAll(initOperations).ConfigureAwait(false);
            };

            Func <DeviceClient, TestDevice, Task> testOperation = async(deviceClient, testDevice) =>
            {
                IList <Task> clientOperations = new List <Task>();
                await deviceClient.OpenAsync().ConfigureAwait(false);

                // D2C Operation
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Operation 1: Send D2C for device={testDevice.Id}");
                var sendD2CMessage = MessageSendE2ETests.SendSingleMessageAndVerifyAsync(deviceClient, testDevice.Id);
                clientOperations.Add(sendD2CMessage);

                // C2D Operation
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Operation 2: Receive C2D for device={testDevice.Id}");
                List <string> msgSent = messagesSent[testDevice.Id];
                var           payload = msgSent[0];
                var           p1Value = msgSent[1];
                var           verifyDeviceClientReceivesMessage = MessageReceiveE2ETests.VerifyReceivedC2DMessageAsync(transport, deviceClient, payload, p1Value);
                clientOperations.Add(verifyDeviceClientReceivesMessage);

                // Invoke direct methods
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Operation 3: Direct methods test for device={testDevice.Id}");
                var serviceInvokeMethod = MethodE2ETests.ServiceSendMethodAndVerifyResponse(testDevice.Id, MethodName, MethodE2ETests.DeviceResponseJson, MethodE2ETests.ServiceRequestJson);
                clientOperations.Add(serviceInvokeMethod);

                // Set reported twin properties
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Operation 4: Set reported property for device={testDevice.Id}");
                var setReportedProperties = TwinE2ETests.Twin_DeviceSetsReportedPropertyAndGetsItBack(deviceClient);
                clientOperations.Add(setReportedProperties);

                // Receive set desired twin properties
                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: Operation 5: Receive desired property for device={testDevice.Id}");
                List <string> twinProperties          = twinPropertyMap[testDevice.Id];
                var           propName                = twinProperties[0];
                var           propValue               = twinProperties[1];
                var           updateDesiredProperties = TwinE2ETests.RegistryManagerUpdateDesiredPropertyAsync(testDevice.Id, propName, propValue);
                clientOperations.Add(updateDesiredProperties);

                await Task.WhenAll(clientOperations).ConfigureAwait(false);

                _log.WriteLine($"{nameof(CombinedClientOperationsMultiplexingOverAmqpTests)}: All operations completed for device={testDevice.Id}");
            };

            Func <IList <DeviceClient>, Task> cleanupOperation = async(deviceClients) =>
            {
                await serviceClient.CloseAsync().ConfigureAwait(false);

                serviceClient.Dispose();

                foreach (DeviceClient deviceClient in deviceClients)
                {
                    deviceClient.Dispose();
                }

                messagesSent.Clear();
                twinPropertyMap.Clear();
            };

            await MultiplexingOverAmqp.TestMultiplexingOperationAsync(
                DevicePrefix,
                transport,
                poolSize,
                devicesCount,
                initOperation,
                testOperation,
                cleanupOperation).ConfigureAwait(false);
        }
        private async Task DeviceCombinedClientOperationsAsync(
            Client.TransportType transport,
            int poolSize,
            int devicesCount,
            ConnectionStringAuthScope authScope = ConnectionStringAuthScope.Device)
        {
            // Initialize service client for service-side operations
            using var serviceClient = ServiceClient.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString);

            // Message payload and properties for C2D operation
            var messagesSent = new Dictionary <string, Tuple <Message, string> >();

            // Twin properties
            var twinPropertyMap = new Dictionary <string, List <string> >();

            async Task InitOperationAsync(DeviceClient deviceClient, TestDevice testDevice, TestDeviceCallbackHandler _)
            {
                IList <Task> initOperations = new List <Task>();

                // Send C2D Message
                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Send C2D for device={testDevice.Id}");
                (Message msg, string payload, string p1Value) = MessageReceiveE2ETests.ComposeC2dTestMessage(Logger);
                using (msg)
                {
                    messagesSent.Add(testDevice.Id, Tuple.Create(msg, payload));
                    Task sendC2dMessage = serviceClient.SendAsync(testDevice.Id, msg);
                    initOperations.Add(sendC2dMessage);

                    // Set method handler
                    Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Set direct method {MethodName} for device={testDevice.Id}");
                    Task <Task> methodReceivedTask = MethodE2ETests.SetDeviceReceiveMethodAsync(deviceClient, MethodName, Logger);
                    initOperations.Add(methodReceivedTask);

                    // Set the twin desired properties callback
                    Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Set desired property callback for device={testDevice.Id}");
                    string propName  = Guid.NewGuid().ToString();
                    string propValue = Guid.NewGuid().ToString();
                    twinPropertyMap.Add(testDevice.Id, new List <string> {
                        propName, propValue
                    });
                    Task <Task> updateReceivedTask = TwinE2ETests.SetTwinPropertyUpdateCallbackHandlerAsync(deviceClient, propName, propValue, Logger);
                    initOperations.Add(updateReceivedTask);

                    await Task.WhenAll(initOperations).ConfigureAwait(false);
                }
            }

            async Task TestOperationAsync(DeviceClient deviceClient, TestDevice testDevice, TestDeviceCallbackHandler _)
            {
                IList <Task> clientOperations = new List <Task>();
                await deviceClient.OpenAsync().ConfigureAwait(false);

                // D2C Operation
                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Operation 1: Send D2C for device={testDevice.Id}");
                Task sendD2cMessage = MessageSendE2ETests.SendSingleMessageAsync(deviceClient, testDevice.Id, Logger);

                clientOperations.Add(sendD2cMessage);

                // C2D Operation
                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Operation 2: Receive C2D for device={testDevice.Id}");
                Tuple <Message, string> msgSent = messagesSent[testDevice.Id];
                Message msg     = msgSent.Item1;
                string  payload = msgSent.Item2;

                Task verifyDeviceClientReceivesMessage = MessageReceiveE2ETests.VerifyReceivedC2DMessageAsync(transport, deviceClient, testDevice.Id, msg, payload, Logger);

                clientOperations.Add(verifyDeviceClientReceivesMessage);

                // Invoke direct methods
                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Operation 3: Direct methods test for device={testDevice.Id}");
                Task serviceInvokeMethod = MethodE2ETests.ServiceSendMethodAndVerifyResponseAsync(testDevice.Id, MethodName, MethodE2ETests.DeviceResponseJson, MethodE2ETests.ServiceRequestJson, Logger);

                clientOperations.Add(serviceInvokeMethod);

                // Set reported twin properties
                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Operation 4: Set reported property for device={testDevice.Id}");
                Task setReportedProperties = TwinE2ETests.Twin_DeviceSetsReportedPropertyAndGetsItBackAsync(deviceClient, testDevice.Id, Guid.NewGuid().ToString(), Logger);

                clientOperations.Add(setReportedProperties);

                // Receive set desired twin properties
                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Operation 5: Receive desired property for device={testDevice.Id}");
                List <string> twinProperties          = twinPropertyMap[testDevice.Id];
                string        propName                = twinProperties[0];
                string        propValue               = twinProperties[1];
                Task          updateDesiredProperties = TwinE2ETests.RegistryManagerUpdateDesiredPropertyAsync(testDevice.Id, propName, propValue);

                clientOperations.Add(updateDesiredProperties);

                await Task.WhenAll(clientOperations).ConfigureAwait(false);

                Logger.Trace($"{nameof(CombinedClientOperationsPoolAmqpTests)}: All operations completed for device={testDevice.Id}");
            }

            Task CleanupOperationAsync()
            {
                messagesSent.Clear();
                twinPropertyMap.Clear();
                return(Task.FromResult(0));
            }

            await PoolingOverAmqp
            .TestPoolAmqpAsync(
                _devicePrefix,
                transport,
                poolSize,
                devicesCount,
                InitOperationAsync,
                TestOperationAsync,
                CleanupOperationAsync,
                authScope,
                false,
                Logger)
            .ConfigureAwait(false);
        }