protected async Task VerifyDirectMethodAsync()

in smoke/LeafDevice/details/Details.cs [315:388]


        protected async Task VerifyDirectMethodAsync()
        {
            // Leaf device without parent not expected to succed dm
            if (!this.edgeDeviceId.HasValue)
                return;

            // User Service SDK to invoke Direct Method on the device.
            var settings = new ServiceClientTransportSettings();
            this.proxy.ForEach(p => settings.HttpProxy = p);
            ServiceClient serviceClient =
                ServiceClient.CreateFromConnectionString(this.context.IotHubConnectionString, this.serviceClientTransportType, settings);

            // Call a direct method
            TimeSpan testDuration = TimeSpan.FromSeconds(300);
            DateTime endTime = DateTime.UtcNow + testDuration;

            CloudToDeviceMethod cloudToDeviceMethod = new CloudToDeviceMethod("DirectMethod").SetPayloadJson("{\"TestKey\" : \"TestValue\"}");

            CloudToDeviceMethodResult result = null;
            // To reduce log size and make troubleshooting easier, log last exception only.
            Exception lastException = null;
            bool isRetrying = true;

            Console.WriteLine("Starting Direct method test.");
            while (isRetrying && DateTime.UtcNow <= endTime)
            {
                try
                {
                    using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                    {
                        result = await serviceClient.InvokeDeviceMethodAsync(
                            this.context.Device.Id,
                            cloudToDeviceMethod,
                            cts.Token);

                        if (result?.Status == 200)
                        {
                            isRetrying = false;
                        }

                        // Don't retry too fast
                        await Task.Delay(1000, cts.Token);
                    }
                }
                catch (OperationCanceledException ex)
                {
                    if (lastException == null)
                    {
                        lastException = ex;
                    }
                }
                catch (Exception ex)
                {
                    lastException = ex;
                }
            }

            if (result?.Status != 200)
            {
                if (lastException != null)
                {
                    Console.WriteLine($"Failed to send direct method from device '{this.context.Device.Id}' with payload '{cloudToDeviceMethod}: {lastException}'");
                }

                throw new Exception($"Could not invoke Direct Method on Device with result status {result?.Status}.");
            }

            if (!result.GetPayloadAsJson().Equals("{\"TestKey\":\"TestValue\"}", StringComparison.Ordinal))
            {
                throw new Exception($"Payload doesn't match with Sent Payload. Received payload: {result.GetPayloadAsJson()}. Expected: {{\"TestKey\":\"TestValue\"}}");
            }

            Console.WriteLine("Direct method test passed.");
        }