in DeviceBridgeTests/Services/ConnectionManagerTests.cs [77:115]
public async Task AssertDeviceConnectionOpenAsyncTemporaryVsPermanent()
{
using (ShimsContext.Create())
{
var connectionManager = CreateConnectionManager();
int closeCount = 0;
// If temporary is set to false (default), creates a permanent connection without creating or renewing a temporary connection.
ShimDps("test-hub.azure.devices.net");
ShimDeviceClientAndCaptureClose(() => closeCount++);
await connectionManager.AssertDeviceConnectionOpenAsync("permanent-device-id");
await connectionManager.AssertDeviceConnectionClosedAsync("permanent-device-id", true);
Assert.AreEqual(0, closeCount, "Closing a temporary connection should not have closed a permanent connection");
await connectionManager.AssertDeviceConnectionClosedAsync("permanent-device-id");
Assert.AreEqual(1, closeCount);
// If temporary is set to true, creates a temporary connection if one doesn't exist, without creating a permanent connection.
closeCount = 0;
await connectionManager.AssertDeviceConnectionOpenAsync("temporary-device-id", true);
TestUtils.ShimUtcNowAhead(20); // Move the clock so the temporary connection will expire.
await connectionManager.AssertDeviceConnectionClosedAsync("temporary-device-id");
Assert.AreEqual(0, closeCount, "Closing a permanent connection should not have closed a temporary connection");
await connectionManager.AssertDeviceConnectionClosedAsync("temporary-device-id", true);
Assert.AreEqual(1, closeCount);
// If temporary is set to true, renews a temporary connection if one already exists, without creating a permanent connection.
closeCount = 0;
TestUtils.UnshimUtcNow();
await connectionManager.AssertDeviceConnectionOpenAsync("renew-device-id", true); // Create initial ~10min connection.
TestUtils.ShimUtcNowAhead(5);
await connectionManager.AssertDeviceConnectionOpenAsync("renew-device-id", true); // Move the clock 5min and renew connection for another ~10min, so total connection duration is ~15min.
TestUtils.ShimUtcNowAhead(12);
await connectionManager.AssertDeviceConnectionClosedAsync("renew-device-id", true);
Assert.AreEqual(0, closeCount, "Temporary connection should not have been closed after 12min, as it was renewed for ~15min");
TestUtils.ShimUtcNowAhead(18);
await connectionManager.AssertDeviceConnectionClosedAsync("renew-device-id", true);
Assert.AreEqual(1, closeCount, "Temporary connection should have been closed after 18min.");
}
}