public void TestClearRegistrations_ShouldClearAllRegistrations() { var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); sessionState.AddOrUpdateSubscription("Sometopic", QualityOfService.AtLeastOnce); sessionState.ClearRegistrations(); IReadOnlyDictionary <string, bool> registrations = sessionState.SubscriptionRegistrations; Assert.NotNull(registrations); Assert.Equal(registrations.Count, 0); }
public void TestSetAsync_SetMultipleSubscriptions_ShouldComplete() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStatePersistenceProvider(edgeHub.Object); ISessionState sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix, QualityOfService.AtLeastOnce); sessionState.AddOrUpdateSubscription(SessionStatePersistenceProvider.TwinResponseTopicFilter, QualityOfService.AtLeastOnce); Task setTask = sessionProvider.SetAsync(identity, sessionState); Assert.True(setTask.IsCompleted); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.Methods), Times.Once); }
public async Task SessionStateSeralizationTest() { var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); sessionState.RemoveSubscription(TwinSubscriptionTopicPrefix); Assert.True(sessionState.SubscriptionRegistrations.ContainsKey(MethodPostTopicPrefix)); Assert.True(sessionState.SubscriptionRegistrations.ContainsKey(TwinSubscriptionTopicPrefix)); Assert.True(sessionState.SubscriptionRegistrations[MethodPostTopicPrefix]); Assert.False(sessionState.SubscriptionRegistrations[TwinSubscriptionTopicPrefix]); IEntityStore <string, SessionState> entityStore = new StoreProvider(new InMemoryDbStoreProvider()).GetEntityStore <string, SessionState>(Constants.SessionStorePartitionKey); string key = Guid.NewGuid().ToString(); await entityStore.Put(key, sessionState); Option <SessionState> retrievedSessionStateOption = await entityStore.Get(key); Assert.True(retrievedSessionStateOption.HasValue); SessionState retrievedSessionState = retrievedSessionStateOption.OrDefault(); Assert.NotNull(retrievedSessionState); Assert.NotNull(retrievedSessionState.Subscriptions); Assert.NotNull(retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == MethodPostTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(MethodPostTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(TwinSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations[MethodPostTopicPrefix]); Assert.False(retrievedSessionState.SubscriptionRegistrations[TwinSubscriptionTopicPrefix]); }
public void TestSetAsync_AddedMethodSubscription_ShouldComplete() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); Task setTask = sessionProvider.SetAsync(identity, sessionState); Assert.True(setTask.IsCompleted); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.Methods), Times.Once); }
public void TestCopy_ShouldReturnSessionStateCopy() { var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); sessionState.AddOrUpdateSubscription("Sometopic", QualityOfService.AtLeastOnce); var copySession = sessionState.Copy() as SessionState; Assert.NotNull(copySession); Assert.Equal(copySession.Subscriptions.Count, sessionState.Subscriptions.Count); for (int i = 0; i < copySession.Subscriptions.Count; i++) { Assert.Equal(copySession.Subscriptions[i].TopicFilter, sessionState.Subscriptions[i].TopicFilter); Assert.Equal(copySession.Subscriptions[i].QualityOfService, sessionState.Subscriptions[i].QualityOfService); Assert.Equal(copySession.Subscriptions[i].CreationTime, sessionState.Subscriptions[i].CreationTime); } Assert.Equal(copySession.SubscriptionRegistrations.Count, sessionState.SubscriptionRegistrations.Count); foreach (KeyValuePair <string, bool> subscriptionRegistration in copySession.SubscriptionRegistrations) { Assert.Equal(subscriptionRegistration.Value, sessionState.SubscriptionRegistrations[subscriptionRegistration.Key]); } }
public async Task TestSetAsync_AddedMethodSubscription_TransientSession_ShouldNotSaveToStore() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); var sessionState = new SessionState(true); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); await sessionProvider.SetAsync(identity, sessionState); ISessionState storedSession = await sessionProvider.GetAsync(identity); Assert.Null(storedSession); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.Methods), Times.Once); }
public void TestAddOrUpdateSubscription_ShouldAddSubscritionRegistration() { var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); IReadOnlyDictionary <string, bool> registrations = sessionState.SubscriptionRegistrations; IReadOnlyList <ISubscription> subs = sessionState.Subscriptions; Assert.NotNull(registrations); Assert.Equal(registrations.Count, 1); Assert.True(registrations.ContainsKey(MethodPostTopicPrefix)); Assert.True(registrations[MethodPostTopicPrefix]); Assert.NotNull(subs); Assert.Equal(subs.Count, 1); Assert.Equal(subs[0].TopicFilter, MethodPostTopicPrefix); }
public void TestSetAsync_AddedMethodSubscription_ShouldComplete() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStatePersistenceProvider(edgeHub.Object); ISessionState sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(SessionStateParser.MethodSubscriptionTopicPrefix, QualityOfService.AtLeastOnce); Task setTask = sessionProvider.SetAsync(identity, sessionState); Assert.True(setTask.IsCompleted); IEnumerable <(DeviceSubscription, bool)> list = new List <(DeviceSubscription, bool)> { (DeviceSubscription.Methods, true) }; edgeHub.Verify(x => x.ProcessSubscriptions("d1", list), Times.Once); }
public async Task TestSetAsync_RemovedSubscription_ShouldUpdateStore() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); await sessionProvider.SetAsync(identity, sessionState); sessionState.RemoveSubscription(MethodPostTopicPrefix); await sessionProvider.SetAsync(identity, sessionState); ISessionState storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); Assert.Null(storedSession.Subscriptions.SingleOrDefault(p => p.TopicFilter == MethodPostTopicPrefix)); edgeHub.Verify(x => x.RemoveSubscription("d1", DeviceSubscription.Methods), Times.Once); // clean up await sessionProvider.DeleteAsync(identity, sessionState); }
public async Task TestSetAsync_AddedMethodSubscription_ShouldSaveToStore() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); await sessionProvider.SetAsync(identity, sessionState); ISessionState storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); IEnumerable <(DeviceSubscription, bool)> list = new List <(DeviceSubscription, bool)> { (DeviceSubscription.Methods, true) }; edgeHub.Verify(x => x.ProcessSubscriptions("d1", list), Times.Once); // clean up await sessionProvider.DeleteAsync(identity, sessionState); }
public async Task TestSetAsync_RemovedSubscription_ShouldUpdateStore() { List <(DeviceSubscription, bool)> receivedSubscriptions = null; var edgeHub = new Mock <IEdgeHub>(); edgeHub.Setup(e => e.ProcessSubscriptions("d1", It.IsAny <IEnumerable <(DeviceSubscription, bool)> >())) .Callback <string, IEnumerable <(DeviceSubscription, bool)> >((d, s) => receivedSubscriptions = s.ToList()) .Returns(Task.CompletedTask); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); var sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(MethodPostTopicPrefix, QualityOfService.AtLeastOnce); await sessionProvider.SetAsync(identity, sessionState); Assert.NotNull(receivedSubscriptions); Assert.Single(receivedSubscriptions); Assert.True(receivedSubscriptions[0].Item2); Assert.Equal(DeviceSubscription.Methods, receivedSubscriptions[0].Item1); sessionState.RemoveSubscription(MethodPostTopicPrefix); await sessionProvider.SetAsync(identity, sessionState); ISessionState storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); Assert.Null(storedSession.Subscriptions.SingleOrDefault(p => p.TopicFilter == MethodPostTopicPrefix)); Assert.NotNull(receivedSubscriptions); Assert.Single(receivedSubscriptions); Assert.False(receivedSubscriptions[0].Item2); Assert.Equal(DeviceSubscription.Methods, receivedSubscriptions[0].Item1); // clean up await sessionProvider.DeleteAsync(identity, sessionState); }
public async Task TestPersistence() { int callbackCount = 0; List <(DeviceSubscription, bool)> receivedSubscriptions = null; var edgeHub = new Mock <IEdgeHub>(); edgeHub.Setup(e => e.ProcessSubscriptions("d1", It.IsAny <IEnumerable <(DeviceSubscription, bool)> >())) .Callback <string, IEnumerable <(DeviceSubscription, bool)> >( (d, s) => { callbackCount++; receivedSubscriptions = s.ToList(); }) .Returns(Task.CompletedTask); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); ISessionState sessionState = await sessionProvider.GetAsync(identity); Assert.Null(sessionState); sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(SessionStateParser.C2DSubscriptionTopicPrefix, QualityOfService.AtLeastOnce); sessionState.AddOrUpdateSubscription(SessionStateParser.MethodSubscriptionTopicPrefix, QualityOfService.AtMostOnce); sessionState.RemoveSubscription(SessionStateParser.TwinSubscriptionTopicPrefix); await sessionProvider.SetAsync(identity, sessionState); await Task.Delay(TimeSpan.FromSeconds(2)); var expectedSubscriptions = new List <(DeviceSubscription, bool)> { (DeviceSubscription.C2D, true), (DeviceSubscription.Methods, true), (DeviceSubscription.DesiredPropertyUpdates, false) }; Assert.NotNull(receivedSubscriptions); Assert.Equal(1, callbackCount); Assert.Equal(receivedSubscriptions, expectedSubscriptions); ISessionState storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); var retrievedSessionState = storedSession as SessionState; Assert.NotNull(retrievedSessionState); Assert.Equal(2, retrievedSessionState.Subscriptions.Count); Assert.Equal(3, retrievedSessionState.SubscriptionRegistrations.Count); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStateParser.C2DSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStateParser.MethodSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStateParser.C2DSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStateParser.MethodSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStateParser.TwinSubscriptionTopicPrefix)); Assert.False(retrievedSessionState.SubscriptionRegistrations[SessionStateParser.TwinSubscriptionTopicPrefix]); ISubscription c2DSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStateParser.C2DSubscriptionTopicPrefix); Assert.NotNull(c2DSubscription); Assert.Equal(QualityOfService.AtLeastOnce, c2DSubscription.QualityOfService); Assert.True(DateTime.UtcNow - c2DSubscription.CreationTime < TimeSpan.FromMinutes(2)); ISubscription methodSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStateParser.MethodSubscriptionTopicPrefix); Assert.NotNull(methodSubscription); Assert.Equal(QualityOfService.AtMostOnce, methodSubscription.QualityOfService); Assert.True(DateTime.UtcNow - methodSubscription.CreationTime < TimeSpan.FromMinutes(2)); await sessionProvider.SetAsync(identity, sessionState); await Task.Delay(TimeSpan.FromSeconds(2)); Assert.NotNull(receivedSubscriptions); Assert.Equal(2, callbackCount); Assert.Equal(receivedSubscriptions, expectedSubscriptions); storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); retrievedSessionState = storedSession as SessionState; Assert.NotNull(retrievedSessionState); Assert.Equal(2, retrievedSessionState.Subscriptions.Count); Assert.Equal(3, retrievedSessionState.SubscriptionRegistrations.Count); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStateParser.C2DSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStateParser.MethodSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStateParser.C2DSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStateParser.MethodSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStateParser.TwinSubscriptionTopicPrefix)); Assert.False(retrievedSessionState.SubscriptionRegistrations[SessionStateParser.TwinSubscriptionTopicPrefix]); c2DSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStateParser.C2DSubscriptionTopicPrefix); Assert.NotNull(c2DSubscription); Assert.Equal(QualityOfService.AtLeastOnce, c2DSubscription.QualityOfService); Assert.True(DateTime.UtcNow - c2DSubscription.CreationTime < TimeSpan.FromMinutes(2)); methodSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStateParser.MethodSubscriptionTopicPrefix); Assert.NotNull(methodSubscription); Assert.Equal(QualityOfService.AtMostOnce, methodSubscription.QualityOfService); Assert.True(DateTime.UtcNow - methodSubscription.CreationTime < TimeSpan.FromMinutes(2)); await sessionProvider.SetAsync(identity, sessionState); await Task.Delay(TimeSpan.FromSeconds(2)); Assert.NotNull(receivedSubscriptions); Assert.Equal(3, callbackCount); Assert.Equal(receivedSubscriptions, expectedSubscriptions); }
public async Task TestPersistence() { var edgeHub = new Mock <IEdgeHub>(); var identity = Mock.Of <IProtocolgatewayDeviceIdentity>(i => i.Id == "d1"); var sessionProvider = new SessionStateStoragePersistenceProvider(edgeHub.Object, this.entityStore); ISessionState sessionState = await sessionProvider.GetAsync(identity); Assert.Null(sessionState); sessionState = new SessionState(false); sessionState.AddOrUpdateSubscription(SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix, QualityOfService.AtLeastOnce); sessionState.AddOrUpdateSubscription(SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix, QualityOfService.AtMostOnce); sessionState.RemoveSubscription(SessionStatePersistenceProvider.TwinSubscriptionTopicPrefix); await sessionProvider.SetAsync(identity, sessionState); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.Methods), Times.Once); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.C2D), Times.Once); edgeHub.Verify(x => x.RemoveSubscription("d1", DeviceSubscription.DesiredPropertyUpdates), Times.Once); ISessionState storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); var retrievedSessionState = storedSession as SessionState; Assert.NotNull(retrievedSessionState); Assert.Equal(2, retrievedSessionState.Subscriptions.Count); Assert.Equal(3, retrievedSessionState.SubscriptionRegistrations.Count); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStatePersistenceProvider.TwinSubscriptionTopicPrefix)); Assert.False(retrievedSessionState.SubscriptionRegistrations[SessionStatePersistenceProvider.TwinSubscriptionTopicPrefix]); ISubscription c2DSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix); Assert.NotNull(c2DSubscription); Assert.Equal(QualityOfService.AtLeastOnce, c2DSubscription.QualityOfService); Assert.True(DateTime.UtcNow - c2DSubscription.CreationTime < TimeSpan.FromMinutes(2)); ISubscription methodSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix); Assert.NotNull(methodSubscription); Assert.Equal(QualityOfService.AtMostOnce, methodSubscription.QualityOfService); Assert.True(DateTime.UtcNow - methodSubscription.CreationTime < TimeSpan.FromMinutes(2)); await sessionProvider.SetAsync(identity, sessionState); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.Methods), Times.Exactly(2)); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.C2D), Times.Exactly(2)); edgeHub.Verify(x => x.RemoveSubscription("d1", DeviceSubscription.DesiredPropertyUpdates), Times.Exactly(2)); storedSession = await sessionProvider.GetAsync(identity); Assert.NotNull(storedSession); retrievedSessionState = storedSession as SessionState; Assert.NotNull(retrievedSessionState); Assert.Equal(2, retrievedSessionState.Subscriptions.Count); Assert.Equal(3, retrievedSessionState.SubscriptionRegistrations.Count); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix)); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations[SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix]); Assert.True(retrievedSessionState.SubscriptionRegistrations.ContainsKey(SessionStatePersistenceProvider.TwinSubscriptionTopicPrefix)); Assert.False(retrievedSessionState.SubscriptionRegistrations[SessionStatePersistenceProvider.TwinSubscriptionTopicPrefix]); c2DSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStatePersistenceProvider.C2DSubscriptionTopicPrefix); Assert.NotNull(c2DSubscription); Assert.Equal(QualityOfService.AtLeastOnce, c2DSubscription.QualityOfService); Assert.True(DateTime.UtcNow - c2DSubscription.CreationTime < TimeSpan.FromMinutes(2)); methodSubscription = retrievedSessionState.Subscriptions.FirstOrDefault(s => s.TopicFilter == SessionStatePersistenceProvider.MethodSubscriptionTopicPrefix); Assert.NotNull(methodSubscription); Assert.Equal(QualityOfService.AtMostOnce, methodSubscription.QualityOfService); Assert.True(DateTime.UtcNow - methodSubscription.CreationTime < TimeSpan.FromMinutes(2)); await sessionProvider.SetAsync(identity, sessionState); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.Methods), Times.Exactly(3)); edgeHub.Verify(x => x.AddSubscription("d1", DeviceSubscription.C2D), Times.Exactly(3)); edgeHub.Verify(x => x.RemoveSubscription("d1", DeviceSubscription.DesiredPropertyUpdates), Times.Exactly(3)); }