Skip to content

Commit

Permalink
Fix NRE in TwinManager (#300)
Browse files Browse the repository at this point in the history
* Fix NRE in TwinManager

* Move null check
  • Loading branch information
myagley authored Sep 17, 2018
1 parent d65101d commit 0b4ef50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 14 additions & 8 deletions edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/TwinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public async Task<IMessage> GetTwinAsync(string id)
async (store) =>
{
TwinInfo twinInfo = await this.GetTwinInfoWithStoreSupportAsync(id);
return this.twinConverter.ToMessage(twinInfo.Twin);
return twinInfo.Twin != null
? this.twinConverter.ToMessage(twinInfo.Twin)
: throw new InvalidOperationException($"Error getting twin for device {id}. Twin is null.");
},
async () =>
{
Expand Down Expand Up @@ -317,10 +319,7 @@ await this.ExecuteOnTwinStoreResultAsync(
t =>
{
twinInfo = t;
Events.GetTwinFromStoreWhenOffline(id,
twinInfo.Twin.Properties.Desired.Version,
twinInfo.Twin.Properties.Reported.Version,
e);
Events.GetTwinFromStoreWhenOffline(id, twinInfo, e);
return Task.CompletedTask;
},
() => throw new InvalidOperationException($"Error getting twin for device {id}", e));
Expand Down Expand Up @@ -653,10 +652,17 @@ public static void UpdatedCachedReportedProperties(string id, long reportedVersi
$"at reported property version {reportedVersion} cloudVerified {cloudVerified}");
}

public static void GetTwinFromStoreWhenOffline(string id, long desiredVersion, long reportedVersion, Exception e)
public static void GetTwinFromStoreWhenOffline(string id, TwinInfo twinInfo, Exception e)
{
Log.LogDebug((int)EventIds.GetTwinFromStoreWhenOffline, $"Getting twin for {id} at desired version " +
$"{desiredVersion} reported version {reportedVersion} from local store. Get from cloud threw {e.GetType()} {e.Message}");
if (twinInfo.Twin != null)
{
Log.LogDebug((int)EventIds.GetTwinFromStoreWhenOffline, $"Getting twin for {id} at desired version " +
$"{twinInfo.Twin.Properties.Desired.Version} reported version {twinInfo.Twin.Properties.Reported.Version} from local store. Get from cloud threw {e.GetType()} {e.Message}");
}
else
{
Log.LogDebug((int)EventIds.GetTwinFromStoreWhenOffline, $"Getting twin info for {id}, but twin is null. Get from cloud threw {e.GetType()} {e.Message}");
}
}

public static void GotTwinFromCloudSuccess(string id, long desiredVersion, long reportedVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public async Task TestEdgeHubConnection()
var identityFactory = new ClientCredentialsFactory(iothubHostName);
string edgeHubConnectionString = $"{deviceConnStr};ModuleId={EdgeHubModuleId}";
IClientCredentials edgeHubCredentials = identityFactory.GetWithConnectionString(edgeHubConnectionString);
Mock.Get(credentialsCache)
.Setup(c => c.Get(edgeHubCredentials.Identity))
.ReturnsAsync(Option.Some(edgeHubCredentials));
Assert.NotNull(edgeHubCredentials);
Assert.NotNull(edgeHubCredentials.Identity);

Expand All @@ -78,6 +81,7 @@ public async Task TestEdgeHubConnection()
Router router = await Router.CreateAsync(Guid.NewGuid().ToString(), iothubHostName, routerConfig, endpointExecutorFactory);
IInvokeMethodHandler invokeMethodHandler = new InvokeMethodHandler(connectionManager);
IEdgeHub edgeHub = new RoutingEdgeHub(router, new RoutingMessageConverter(), connectionManager, twinManager, edgeDeviceId, invokeMethodHandler);
cloudConnectionProvider.BindEdgeHub(edgeHub);

var versionInfo = new VersionInfo("v1", "b1", "c1");

Expand Down

0 comments on commit 0b4ef50

Please sign in to comment.