public async Task TestNotifyChangedSceneModel() { var mockHttp = new MockHttpMessageHandler(); mockHttp.AddInitialAndSubscribeMocks(); var mockedEvent = mockHttp.When($"{MockDigitalstromConnection.BaseUri}/json/event/get") .WithExactQueryString($"subscriptionID=42&timeout=60000&token={MockDigitalstromConnection.AppToken}") .Respond("application/json", SceneCommand.Preset0.ToMockedSceneEvent()); using var subscriber = new DssEventSubscriber(mockHttp.AddAuthMock().ToMockProvider(), new List <SystemEventName>() { SystemEvent.CallScene }, 42); await Task.Delay(300); mockHttp.AutoFlush = false; try { mockHttp.Flush(); } catch { } int numChangedEvents = 0; subscriber.ApiEventRaised += (s, e) => { numChangedEvents++; }; await mockHttp.MockDssEventAsync(subscriber, mockedEvent, SceneCommand.Preset1.ToMockedSceneEvent()); Assert.True(1 <= numChangedEvents); await mockHttp.MockDssEventAsync(subscriber, mockedEvent, SceneCommand.Preset2.ToMockedSceneEvent()); Assert.True(2 <= numChangedEvents); await mockHttp.MockDssEventAsync(subscriber, mockedEvent, SceneCommand.Preset1.ToMockedSceneEvent()); Assert.True(3 <= numChangedEvents); }
public async Task TestSceneCallOnModelChangePrefilledModel() { var mockHttp = new MockHttpMessageHandler(); mockHttp.AddInitialAndSubscribeMocks(); var callSceneRequest1 = mockHttp.AddCallSceneMock(zoneKitchen, Color.Yellow, SceneCommand.Preset1); var callSceneRequest2 = mockHttp.AddCallSceneMock(zoneKitchen, Color.Black, SceneCommand.DeepOff); var model = new ApartmentState(); model[zoneKitchen, Color.Yellow].Value = SceneCommand.Preset0; model[zoneKitchen, Color.Black].Value = SceneCommand.Preset0; using (var dssClient = new DigitalstromDssClient(mockHttp.AddAuthMock().ToMockProvider())) using (var subscriber = new DssEventSubscriber(dssClient, new List <SystemEventName>() { SystemEvent.CallScene }, 42)) using (var aggregator = new TwinChangeAggregator(model)) { aggregator.SceneChangedInternal += (s, e) => dssClient.CallScene(e.Zone, e.Group, e.Scene).Wait(); Assert.Equal(0, mockHttp.GetMatchCount(callSceneRequest1)); Assert.Equal(0, mockHttp.GetMatchCount(callSceneRequest2)); await mockHttp.WaitForCallSceneAsync(callSceneRequest1, () => { model[zoneKitchen, Color.Yellow].Value = SceneCommand.Preset1; }); Assert.Equal(1, mockHttp.GetMatchCount(callSceneRequest1)); Assert.Equal(0, mockHttp.GetMatchCount(callSceneRequest2)); // even setting the same value again should result in a scene call await mockHttp.WaitForCallSceneAsync(callSceneRequest1, () => { model[zoneKitchen, Color.Yellow].Value = SceneCommand.Preset1; }); Assert.Equal(2, mockHttp.GetMatchCount(callSceneRequest1)); Assert.Equal(0, mockHttp.GetMatchCount(callSceneRequest2)); await mockHttp.WaitForCallSceneAsync(callSceneRequest2, () => { model[zoneKitchen, Color.Black].Value = SceneCommand.DeepOff; }); Assert.Equal(2, mockHttp.GetMatchCount(callSceneRequest1)); Assert.Equal(1, mockHttp.GetMatchCount(callSceneRequest2)); } }
public async Task TestEventSubscription() { var mockHttp = new MockHttpMessageHandler(); var subscription = mockHttp.When($"{MockDigitalstromConnection.BaseUri}/json/event/subscribe") .WithQueryString($"token={MockDigitalstromConnection.AppToken}") .Respond("application/json", @"{ ""ok"": true }"); using (var eventSubscriber = new DssEventSubscriber(mockHttp.AddAuthMock().ToMockProvider(), new List <SystemEventName>() { SystemEvent.CallScene, SystemEvent.CallSceneBus })) { // Wait for the threads to start and make the subscriptions await Task.Delay(100); } Assert.Equal(2, mockHttp.GetMatchCount(subscription)); }
public async Task TestEventsNoErrors() { var mockHttp = new MockHttpMessageHandler(); mockHttp.AddInitialAndSubscribeMocks(); var mockedEvent = mockHttp.When($"{MockDigitalstromConnection.BaseUri}/json/event/get") .WithExactQueryString($"subscriptionID=42&timeout=60000&token={MockDigitalstromConnection.AppToken}") .Respond("application/json", SceneCommand.Preset0.ToMockedSceneEvent()); using var subscriber = new DssEventSubscriber(mockHttp.AddAuthMock().ToMockProvider(), new List <SystemEventName>() { SystemEvent.CallScene }, 42); await Task.Delay(100); mockHttp.AutoFlush = false; try { mockHttp.Flush(); } catch { } var events = new List <DssEvent>(); var errors = new List <Exception>(); subscriber.ApiEventRaised += (s, e) => { if (e.ApiEvent != null) { events.Add(e.ApiEvent); } }; subscriber.ErrorOccured += (s, e) => { if (e.Error != null) { errors.Add(e.Error); } }; await mockHttp.MockDssEventAsync(subscriber, mockedEvent, SceneCommand.Preset1.ToMockedSceneEvent()); await mockHttp.MockDssEventAsync(subscriber, mockedEvent, SceneCommand.Preset2.ToMockedSceneEvent()); await mockHttp.MockDssEventAsync(subscriber, mockedEvent, SceneCommand.Preset1.ToMockedSceneEvent()); Assert.NotEmpty(events.Where(e => e.SystemEvent == SystemEvent.CallScene)); Assert.Empty(errors); }
public static async Task MockDssEventAsync(this MockHttpMessageHandler mockHttp, DssEventSubscriber eventSubscriber, MockedRequest mockedEventResponse, string responseContent, int timeoutMillis = 1000) { DateTime start = DateTime.UtcNow; bool eventReceived = false; eventSubscriber.ApiEventRaised += (s, e) => eventReceived = true; // set the response and flush it - this will create a response in the long polling request in the event subscriber mockedEventResponse.Respond("application/json", responseContent); await Task.Delay(100); mockHttp.Flush(); // give the event polling thread a chance to receive and handle the event, it will set the eventReceived while (!eventReceived && (DateTime.UtcNow - start).TotalMilliseconds < timeoutMillis) { await Task.Delay(1); } }