Skip to content

Commit

Permalink
Fix Twin NRE (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
varunpuranik authored Nov 5, 2018
1 parent 5681951 commit 29f5b74
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
35 changes: 28 additions & 7 deletions edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/TwinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,19 +335,40 @@ async Task UpdateReportedPropertiesWhenTwinStoreHasTwinAsync(string id, TwinColl
{
IEntityStore<string, TwinInfo> twinStore = this.TwinStore.Expect(() => new InvalidOperationException("Missing twin store"));
await twinStore.Update(
id,
u =>
id,
u =>
{
if (u.Twin == null)
{
if (!cloudVerified)
{
ValidateTwinCollectionSize(reported);
}

var twinProperties = new TwinProperties
{
Desired = new TwinCollection(),
Reported = reported
};
var twin = new Twin(twinProperties);
Events.UpdatedCachedReportedProperties(id, reported.Version, cloudVerified);
return new TwinInfo(twin, reported);
}
else
{
string mergedJson = JsonEx.Merge(u.Twin.Properties.Reported, reported, /*treatNullAsDelete*/ true);
var mergedProperty = new TwinCollection(mergedJson);
var mergedReportedProperties = new TwinCollection(mergedJson);

if (!cloudVerified)
{
ValidateTwinCollectionSize(mergedProperty);
ValidateTwinCollectionSize(mergedReportedProperties);
}
u.Twin.Properties.Reported = mergedProperty;
Events.UpdatedCachedReportedProperties(id, u.Twin.Properties.Reported.Version, cloudVerified);

u.Twin.Properties.Reported = mergedReportedProperties;
Events.UpdatedCachedReportedProperties(id, mergedReportedProperties.Version, cloudVerified);
return u;
});
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,17 @@ public async void UpdateReportedPropertiesWhenCloudOfflineTwinNotStoredSuccess()
connectionManager.Setup(t => t.GetCloudConnection(It.IsAny<string>())).Returns(Task.FromResult(cloudProxy));

string deviceId = "device9";
var collection = new TwinCollection()
var collection1 = new TwinCollection
{
["name"] = "value"
};
IMessage collectionMessage = this.twinCollectionMessageConverter.ToMessage(collection);
IMessage collectionMessage1 = this.twinCollectionMessageConverter.ToMessage(collection1);

var collection2 = new TwinCollection
{
["name2"] = "value2"
};
IMessage collectionMessage2 = this.twinCollectionMessageConverter.ToMessage(collection2);

var twinManager = new TwinManager(connectionManager.Object, this.twinCollectionMessageConverter, this.twinMessageConverter, this.twinStore);

Expand All @@ -407,16 +413,28 @@ public async void UpdateReportedPropertiesWhenCloudOfflineTwinNotStoredSuccess()
Assert.Equal(storeMiss, true);

// Act
await twinManager.UpdateReportedPropertiesAsync(deviceId, collectionMessage);
TwinInfo cached = null;
await twinManager.ExecuteOnTwinStoreResultAsync(deviceId, t => { storeHit = true; cached = t; return Task.FromResult(t); }, () => { storeMiss = true; return Task.FromResult<TwinInfo>(null); });
await twinManager.UpdateReportedPropertiesAsync(deviceId, collectionMessage1);
TwinInfo cached1 = null;
await twinManager.ExecuteOnTwinStoreResultAsync(deviceId, t => { storeHit = true; cached1 = t; return Task.FromResult(t); }, () => { storeMiss = true; return Task.FromResult<TwinInfo>(null); });

// Assert
Assert.Equal(storeHit, true);
Assert.Equal(cached.Twin, null);
Assert.Equal(cached1.Twin, null);
Assert.True(JToken.DeepEquals(
JsonConvert.DeserializeObject<JToken>(cached.ReportedPropertiesPatch.ToJson()),
JsonConvert.DeserializeObject<JToken>(collection.ToJson())));
JsonConvert.DeserializeObject<JToken>(cached1.ReportedPropertiesPatch.ToJson()),
JsonConvert.DeserializeObject<JToken>(collection1.ToJson())));

// Act
await twinManager.UpdateReportedPropertiesAsync(deviceId, collectionMessage2);
TwinInfo cached2 = null;
await twinManager.ExecuteOnTwinStoreResultAsync(deviceId, t => { storeHit = true; cached2 = t; return Task.FromResult(t); }, () => { storeMiss = true; return Task.FromResult<TwinInfo>(null); });

// Assert
Assert.Equal(storeHit, true);
Assert.NotNull(cached2.Twin);
Assert.True(JToken.DeepEquals(
JsonConvert.DeserializeObject<JToken>(cached2.ReportedPropertiesPatch.ToJson()),
JsonConvert.DeserializeObject<JToken>(collection2.ToJson())));
}

[Fact]
Expand Down

0 comments on commit 29f5b74

Please # to comment.