Exemplo n.º 1
0
        private async Task _Twin_DeviceReportedPropertiesRecovery(Client.TransportType transport, string faultType, string reason, int delayInSec)
        {
            var propName   = Guid.NewGuid().ToString();
            var propValue1 = Guid.NewGuid().ToString();

            Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager);
            var            deviceClient       = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport);
            TwinCollection props = new TwinCollection();

            props[propName] = propValue1;
            await deviceClient.UpdateReportedPropertiesAsync(props);

            var deviceTwin = await deviceClient.GetTwinAsync();

            Assert.AreEqual <String>(deviceTwin.Properties.Reported[propName].ToString(), propValue1);

            // send error command
            await deviceClient.SendEventAsync(TestUtil.ComposeErrorInjectionProperties(faultType, reason, delayInSec));

            deviceTwin = await deviceClient.GetTwinAsync();

            Assert.AreEqual <String>(deviceTwin.Properties.Reported[propName].ToString(), propValue1);

            var propValue2 = Guid.NewGuid().ToString();

            props[propName] = propValue2;
            await deviceClient.UpdateReportedPropertiesAsync(props);

            deviceTwin = await deviceClient.GetTwinAsync();

            Assert.AreEqual <String>(deviceTwin.Properties.Reported[propName].ToString(), propValue2);

            await deviceClient.CloseAsync();

            await TestUtil.RemoveDeviceAsync(deviceInfo.Item1, registryManager);
        }
Exemplo n.º 2
0
        private async Task SendSingleMessage(Client.TransportType transport)
        {
            Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager);

            EventHubClient   eventHubClient;
            EventHubReceiver eventHubReceiver = CreateEventHubReceiver(deviceInfo.Item1, out eventHubClient);

            var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport);
            await deviceClient.OpenAsync();

            string payload;
            string p1Value;

            Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value);
            await deviceClient.SendEventAsync(testMessage);

            bool      isReceived = false;
            Stopwatch sw         = new Stopwatch();

            sw.Start();
            while (!isReceived && sw.Elapsed.Minutes < 1)
            {
                var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5));

                isReceived = VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value);
            }
            sw.Stop();

            Assert.IsTrue(isReceived, "Message is not received.");

            await deviceClient.CloseAsync();

            await eventHubClient.CloseAsync();

            TestUtil.RemoveDevice(deviceInfo.Item1, registryManager);
        }