Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iot-svc): update debug assert #2264

Merged
merged 5 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions e2e/test/iothub/service/RegistryManagerE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,50 +392,32 @@ public async Task RegistryManager_ConfigurationOperations_Work()
public async Task RegistryManager_Query_Works()
{
// arrange
using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString);
string deviceId = $"{_idPrefix}{Guid.NewGuid()}";

try
{
Device device = await registryManager
.AddDeviceAsync(new Device(deviceId))
.ConfigureAwait(false);

// act
using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString);
string deviceId = TestConfiguration.IoTHub.X509ChainDeviceName;

IQuery query = null;
IEnumerable<Twin> twins = null;
for (int i = 0; i < 30; ++i)
{
string queryText = $"select * from devices where deviceId = '{deviceId}'";
query = registryManager.CreateQuery(queryText);
Device device = await registryManager
.GetDeviceAsync(deviceId)
.ConfigureAwait(false);
device.Should().NotBeNull($"Device {deviceId} should already exist in hub setup for E2E tests");

twins = await query.GetNextAsTwinAsync().ConfigureAwait(false);
// act

if (twins.Any())
{
break;
}
string queryText = $"select * from devices where deviceId = '{deviceId}'";
IQuery query = registryManager.CreateQuery(queryText);
IEnumerable<Twin> twins = await query.GetNextAsTwinAsync().ConfigureAwait(false);

// A new device may not return immediately from a query, so give it some time and some retries to appear
await Task.Delay(250).ConfigureAwait(false);
}
// assert

// assert
twins.Count().Should().Be(1, "only asked for 1 device by its Id");
twins.First().DeviceId.Should().Be(deviceId, "The Id of the device returned should match");
query.HasMoreResults.Should().BeFalse("We've processed the single, expected result");
}
finally
{
await registryManager.RemoveDeviceAsync(deviceId).ConfigureAwait(false);
}
twins.Count().Should().Be(1, "only asked for 1 device by its Id");
twins.First().DeviceId.Should().Be(deviceId, "The Id of the device returned should match");
query.HasMoreResults.Should().BeFalse("We've processed the single, expected result");
}

[LoggedTestMethod]
public async Task ModulesClient_GetModulesOnDevice()
{
int moduleCount = 5;
const int moduleCount = 2;
string testDeviceId = $"IdentityLifecycleDevice{Guid.NewGuid()}";
string[] testModuleIds = new string[moduleCount];
for (int i = 0; i < moduleCount; i++)
Expand All @@ -458,6 +440,9 @@ public async Task ModulesClient_GetModulesOnDevice()
new Module(testDeviceId, testModuleIds[i])).ConfigureAwait(false);
}

// Give the hub a moment
await Task.Delay(250).ConfigureAwait(false);

// List the modules on the test device
IEnumerable<Module> modulesOnDevice = await client.GetModulesOnDeviceAsync(testDeviceId).ConfigureAwait(false);

Expand All @@ -468,7 +453,7 @@ public async Task ModulesClient_GetModulesOnDevice()
Assert.AreEqual(moduleCount, moduleIdsOnDevice.Count);
for (int i = 0; i < moduleCount; i++)
{
Assert.IsTrue(moduleIdsOnDevice.Contains(testModuleIds[i]));
moduleIdsOnDevice.Should().Contain(testModuleIds[i]);
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RegistryManagerImportDevicesTests : E2EMsTestBase
[DataRow(StorageAuthenticationType.KeyBased, false)]
[DataRow(StorageAuthenticationType.IdentityBased, false)]
[DataRow(StorageAuthenticationType.IdentityBased, true)]
[Ignore("Waiting on IcM 279379774 to be resolved, estimated early Jan 2022")]
public async Task RegistryManager_ImportDevices(StorageAuthenticationType storageAuthenticationType, bool isUserAssignedMsi)
{
// arrange
Expand Down
7 changes: 5 additions & 2 deletions iothub/device/src/IotHubConnectionString.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ Task<string> IAuthorizationProvider.GetPasswordAsync()
if (Logging.IsEnabled)
Logging.Enter(this, $"{nameof(IotHubConnectionString)}.{nameof(IAuthorizationProvider.GetPasswordAsync)}");

Debug.Assert(TokenRefresher != null);
Debug.Assert(
!string.IsNullOrWhiteSpace(SharedAccessSignature)
|| TokenRefresher != null,
"The token refresher and the shared access signature can't both be null");

return !string.IsNullOrWhiteSpace(SharedAccessSignature)
? Task.FromResult(SharedAccessSignature)
: TokenRefresher.GetTokenAsync(Audience);
: TokenRefresher?.GetTokenAsync(Audience);
}
finally
{
Expand Down