public async Task GetAll()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            IReliableDictionary <string, DeviceEvent> store =
                await stateManager.GetOrAddAsync <IReliableDictionary <string, DeviceEvent> >(DataService.EventDictionaryName);

            Dictionary <string, DeviceEvent> expected = new Dictionary <string, DeviceEvent>();

            expected.Add("device1", new DeviceEvent(DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))));
            expected.Add("device2", new DeviceEvent(DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(2))));


            using (ITransaction tx = stateManager.CreateTransaction())
            {
                foreach (var item in expected)
                {
                    await store.SetAsync(tx, item.Key, item.Value);
                }
            }

            DevicesController target = new DevicesController(stateManager, appLifetime);
            IActionResult     result = await target.GetAsync();

            Assert.True(result is OkObjectResult);

            IEnumerable <dynamic> actual = ((OkObjectResult)result).Value as IEnumerable <dynamic>;

            foreach (dynamic item in actual)
            {
                Assert.Equal <DateTimeOffset>(expected[item.Id].Timestamp, item.Timestamp);
            }
        }
Example #2
0
        public async Task SingleEvent()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            IReliableDictionary <string, DeviceEvent> store =
                await stateManager.GetOrAddAsync <IReliableDictionary <string, DeviceEvent> >(DataService.EventDictionaryName);

            IReliableQueue <DeviceEventSeries> queue =
                await stateManager.GetOrAddAsync <IReliableQueue <DeviceEventSeries> >(DataService.EventQueueName);

            string      expectedDeviceId    = "some-device";
            DeviceEvent expectedDeviceEvent = new DeviceEvent(new DateTimeOffset(1, TimeSpan.Zero));

            EventsController target = new EventsController(stateManager, statefulServiceContext, appLifetime);

            IActionResult result = await target.Post(expectedDeviceId, new[] { expectedDeviceEvent });

            Assert.True(result is OkResult);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                ConditionalValue <DeviceEvent> actualStoredEvent = await store.TryGetValueAsync(tx, expectedDeviceId);

                ConditionalValue <DeviceEventSeries> actualQueuedEvent = await queue.TryDequeueAsync(tx);

                Assert.True(actualStoredEvent.HasValue);
                Assert.Equal(expectedDeviceEvent.Timestamp, actualStoredEvent.Value.Timestamp);

                Assert.True(actualQueuedEvent.HasValue);
                Assert.Equal(expectedDeviceEvent.Timestamp, actualQueuedEvent.Value.Events.First().Timestamp);

                await tx.CommitAsync();
            }
        }
Example #3
0
        public async Task MissingDeviceId()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            EventsController target = new EventsController(stateManager, statefulServiceContext, appLifetime);

            IActionResult result = await target.Post(null, new DeviceEvent[0]);

            Assert.True(result is BadRequestResult);
        }
Example #4
0
        public async Task NoEvents()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            string expectedDeviceId = "some-device";

            EventsController target = new EventsController(stateManager, statefulServiceContext, appLifetime);

            IActionResult result = await target.Post(expectedDeviceId, new DeviceEvent[0]);

            Assert.True(result is OkResult);
        }
        public async Task GetAllEmpty()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            IReliableDictionary <string, DeviceEvent> store =
                await stateManager.GetOrAddAsync <IReliableDictionary <string, DeviceEvent> >(DataService.EventDictionaryName);

            DevicesController target = new DevicesController(stateManager, appLifetime);
            IActionResult     result = await target.GetAsync();

            Assert.True(result is OkObjectResult);

            IEnumerable <dynamic> actual = ((OkObjectResult)result).Value as IEnumerable <dynamic>;

            Assert.False(actual.Any());
        }
Example #6
0
        public async Task AddMostRecentEvent()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            IReliableDictionary <string, DeviceEvent> store =
                await stateManager.GetOrAddAsync <IReliableDictionary <string, DeviceEvent> >(DataService.EventDictionaryName);

            IReliableQueue <DeviceEventSeries> queue =
                await stateManager.GetOrAddAsync <IReliableQueue <DeviceEventSeries> >(DataService.EventQueueName);

            string expectedDeviceId = "some-device";

            List <DeviceEvent> expectedDeviceList  = new List <DeviceEvent>();
            DeviceEvent        expectedDeviceEvent = new DeviceEvent(new DateTimeOffset(100, TimeSpan.Zero));

            for (int i = 0; i < 10; ++i)
            {
                expectedDeviceList.Add(new DeviceEvent(new DateTimeOffset(i, TimeSpan.Zero)));
            }
            expectedDeviceList.Insert(4, expectedDeviceEvent);

            EventsController target = new EventsController(stateManager, statefulServiceContext, appLifetime);

            IActionResult result = await target.Post(expectedDeviceId, expectedDeviceList);

            Assert.True(result is OkResult);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                ConditionalValue <DeviceEvent> actualStoredEvent = await store.TryGetValueAsync(tx, expectedDeviceId);

                ConditionalValue <DeviceEventSeries> actualQueuedEvent = await queue.TryDequeueAsync(tx);

                Assert.True(actualStoredEvent.HasValue);
                Assert.Equal(expectedDeviceEvent.Timestamp, actualStoredEvent.Value.Timestamp);

                Assert.True(actualQueuedEvent.HasValue);
                Assert.True(actualQueuedEvent.Value.Events.Select(x => x.Timestamp).SequenceEqual(expectedDeviceList.Select(x => x.Timestamp)));

                await tx.CommitAsync();
            }
        }
        public async Task GetQueueLength()
        {
            MockApplicationLifetime  appLifetime  = new MockApplicationLifetime();
            MockReliableStateManager stateManager = new MockReliableStateManager();

            IReliableQueue <DeviceEventSeries> queue =
                await stateManager.GetOrAddAsync <IReliableQueue <DeviceEventSeries> >(DataService.EventQueueName);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await queue.EnqueueAsync(tx, new DeviceEventSeries("", new DeviceEvent[0]));
            }

            DevicesController target = new DevicesController(stateManager, appLifetime);
            IActionResult     result = await target.GetQueueLengthAsync();

            Assert.True(result is OkObjectResult);
            long actual = (long)((OkObjectResult)result).Value;

            Assert.Equal(1, actual);
        }