public async Task AssertDeviceConnectionOpenAsyncTriesCachedHub()

in DeviceBridgeTests/Services/ConnectionManagerTests.cs [138:178]


        public async Task AssertDeviceConnectionOpenAsyncTriesCachedHub()
        {
            using (ShimsContext.Create())
            {
                var hubCache = new List<HubCacheEntry>()
                {
                    new HubCacheEntry()
                    {
                        DeviceId = "test-device-id",
                        Hub = "known-hub.azure.devices.net",
                    },
                };
                var connectionManager = CreateConnectionManager(hubCache);

                // Check that it Attempts to connect to the cached device hub first, if one exists.
                string connStr = null;
                ShimDeviceClientAndCaptureConnectionString(capturedConnStr => connStr = capturedConnStr);
                await connectionManager.AssertDeviceConnectionOpenAsync("test-device-id");
                StringAssert.Contains("known-hub.azure.devices.net", connStr);

                // Check that the device client is cached and not reopened in subsequent calls.
                bool openAttempted = false;
                ShimDeviceClientAndCaptureOpen(() => openAttempted = true);
                await connectionManager.AssertDeviceConnectionOpenAsync("test-device-id");
                Assert.False(openAttempted);

                // Check that DPS registration is eventually attempted if connection error indicates that the device doesn't exist in the target hub.
                connectionManager = CreateConnectionManager(hubCache);
                ShimDeviceClientToFail(new DeviceNotFoundException());
                var registrationAttempted = false;
                ShimDpsAndCaptureRegistration("test-hub.azure.devices.net", () => registrationAttempted = true);
                await ExpectToThrow(() => connectionManager.AssertDeviceConnectionOpenAsync("test-device-id"));
                Assert.True(registrationAttempted);

                // Check that DPS registration is attempted if connection attempt fails with unknown error.
                registrationAttempted = false;
                ShimDeviceClientToFail(new Exception());
                await ExpectToThrow(() => connectionManager.AssertDeviceConnectionOpenAsync("test-device-id"));
                Assert.True(registrationAttempted);
            }
        }