Beispiel #1
0
 public static void ClassCleanup()
 {
     TestUtil.UnInitializeEnvironment(registryManager);
 }
Beispiel #2
0
        private async Task SendMessageRecovery(Client.TransportType transport,
                                               string faultType, string reason, int delayInSec, int durationInSec = 0, int retryDurationInMilliSec = 240000)
        {
            await sequentialTestSemaphore.WaitAsync();

            Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager);
            var              deviceClient     = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport);
            EventHubClient   eventHubClient;
            EventHubReceiver eventHubReceiver = CreateEventHubReceiver(deviceInfo.Item1, out eventHubClient);

            try
            {
                deviceClient.OperationTimeoutInMilliseconds = (uint)retryDurationInMilliSec;

                ConnectionStatus?            lastConnectionStatus             = null;
                ConnectionStatusChangeReason?lastConnectionStatusChangeReason = null;
                int setConnectionStatusChangesHandlerCount = 0;

                deviceClient.SetConnectionStatusChangesHandler((status, statusChangeReason) =>
                {
                    lastConnectionStatus             = status;
                    lastConnectionStatusChangeReason = statusChangeReason;
                    setConnectionStatusChangesHandlerCount++;
                });

                await deviceClient.OpenAsync();

                if (transport != Client.TransportType.Http1)
                {
                    Assert.AreEqual(1, setConnectionStatusChangesHandlerCount);
                    Assert.AreEqual(ConnectionStatus.Connected, lastConnectionStatus);
                    Assert.AreEqual(ConnectionStatusChangeReason.Connection_Ok, lastConnectionStatusChangeReason);
                }

                string         payload, 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();

                // send error command and clear eventHubReceiver of the fault injection message
                await deviceClient.SendEventAsync(TestUtil.ComposeErrorInjectionProperties(faultType, reason,
                                                                                           delayInSec,
                                                                                           durationInSec));

                await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5));

                Thread.Sleep(1000);

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

                sw.Reset();
                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();

                await deviceClient.CloseAsync();

                if (transport != Client.TransportType.Http1)
                {
                    Assert.AreEqual(2, setConnectionStatusChangesHandlerCount);
                    Assert.AreEqual(ConnectionStatus.Disabled, lastConnectionStatus);
                    Assert.AreEqual(ConnectionStatusChangeReason.Client_Close, lastConnectionStatusChangeReason);
                }
            }
            finally
            {
                await deviceClient.CloseAsync();

                await eventHubReceiver.CloseAsync();

                await eventHubClient.CloseAsync();

                await TestUtil.RemoveDeviceAsync(deviceInfo.Item1, registryManager);

                sequentialTestSemaphore.Release(1);
            }
        }
        private async Task DeviceClient_TokenIsRefreshed_Internal(Client.TransportType transport)
        {
            var builder = IotHubConnectionStringBuilder.Create(Configuration.IoTHub.ConnectionString);

            RegistryManager rm = await TestUtil.GetRegistryManagerAsync(DevicePrefix).ConfigureAwait(false);

            int ttl    = 6;
            int buffer = 50;

            try
            {
                Device device = await CreateDeviceClientAsync(rm).ConfigureAwait(false);

                var refresher = new TestTokenRefresher(
                    device.Id,
                    device.Authentication.SymmetricKey.PrimaryKey,
                    ttl,
                    buffer);

                DeviceClient deviceClient =
                    DeviceClient.Create(builder.HostName, refresher, transport);

                var message = new Client.Message(Encoding.UTF8.GetBytes("Hello"));

                // Create the first Token.
                Console.WriteLine($"[{DateTime.UtcNow}] OpenAsync");
                await deviceClient.OpenAsync().ConfigureAwait(false);

                Console.WriteLine($"[{DateTime.UtcNow}] SendEventAsync (1)");
                await deviceClient.SendEventAsync(message).ConfigureAwait(false);

                int countAfterOpenAndFirstSend = refresher.SafeCreateNewTokenCallCount;
                Assert.IsTrue(countAfterOpenAndFirstSend >= 1, $"[{DateTime.UtcNow}] Token should have been refreshed at least once.");

                Console.WriteLine($"[{DateTime.UtcNow}] Waiting {ttl} seconds.");

                // Wait for the Token to expire.
                await Task.Delay(ttl * 1000).ConfigureAwait(false);

                Console.WriteLine($"[{DateTime.UtcNow}] SendEventAsync (2)");
                await deviceClient.SendEventAsync(message).ConfigureAwait(false);

                // Ensure that the token was refreshed.
                if (transport == Client.TransportType.Mqtt)
                {
                    // This is not currently supported for MQTT unless the connection is dropped and re-established.
                    Assert.IsTrue(
                        refresher.SafeCreateNewTokenCallCount >= countAfterOpenAndFirstSend,
                        $"[{DateTime.UtcNow}] Token should have been refreshed after TTL expired.");
                }
                else
                {
                    Assert.IsTrue(
                        refresher.SafeCreateNewTokenCallCount >= countAfterOpenAndFirstSend + 1,
                        $"[{DateTime.UtcNow}] Token should have been refreshed after TTL expired.");
                }

                Console.WriteLine($"[{DateTime.UtcNow}] CloseAsync");
                await deviceClient.CloseAsync().ConfigureAwait(false);
            }
            finally
            {
                await TestUtil.UnInitializeEnvironment(rm).ConfigureAwait(false);
            }
        }