public async Task GetAll()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);
            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);
            }
        }
        public async Task GetAll()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);
            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);
            }
        }
        public async Task SingleEvent()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);

            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();
            }
        }
        public async Task MissingDeviceId()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            MockReliableStateManager stateManager = new MockReliableStateManager();

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

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

            Assert.True(result is BadRequestResult);
        }
        public async Task MissingDeviceId()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            MockReliableStateManager stateManager = new MockReliableStateManager();

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

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

            Assert.True(result is BadRequestResult);
        }
        public async Task NoEvents()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            MockReliableStateManager stateManager = new MockReliableStateManager();

            string expectedDeviceId = "some-device";

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

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

            Assert.True(result is OkResult);
        }
        public async Task NoEvents()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            MockReliableStateManager stateManager = new MockReliableStateManager();

            string expectedDeviceId = "some-device";

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

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

            Assert.True(result is OkResult);
        }
        public async Task GetAllEmpty()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            MockReliableStateManager stateManager = new MockReliableStateManager();

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

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

            Assert.True(result is OkObjectResult);

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

            Assert.False(actual.Any());
        }
        public async Task GetAllEmpty()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            MockReliableStateManager stateManager = new MockReliableStateManager();

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

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

            Assert.True(result is OkObjectResult);

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

            Assert.False(actual.Any());
        }
        public async Task AddMostRecentEvent()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);

            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()
        {
            ServiceCancellation      cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);
            IActionResult     result = await target.GetQueueLengthAsync();

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

            Assert.Equal(1, actual);
        }
        public async Task GetQueueLength()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);
            IActionResult result = await target.GetQueueLengthAsync();

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

            Assert.Equal(1, actual);
        }
        public async Task AddMostRecentEvent()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);

            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();
            }
        }
Ejemplo n.º 14
0
 public IngestionController(FabricClient fabricClient, ServiceCancellation serviceCancellation)
 {
     this.fabricClient             = fabricClient;
     this.serviceCancellationToken = serviceCancellation.Token;
 }
 public DevicesController(IReliableStateManager stateManager, ServiceCancellation serviceCancellation)
 {
     this.stateManager = stateManager;
     this.serviceCancellationToken = serviceCancellation.Token;
 }
Ejemplo n.º 16
0
 public EventsController(IReliableStateManager stateManager, StatefulServiceContext context, ServiceCancellation serviceCancellation)
 {
     this.stateManager             = stateManager;
     this.context                  = context;
     this.serviceCancellationToken = serviceCancellation.Token;
 }
Ejemplo n.º 17
0
 public DevicesController(FabricClient fabricClient, HttpClient httpClient, ServiceCancellation serviceCancellation)
 {
     this.fabricClient             = fabricClient;
     this.httpClient               = httpClient;
     this.serviceCancellationToken = serviceCancellation.Token;
 }
 public EventsController(IReliableStateManager stateManager, StatefulServiceContext context, ServiceCancellation serviceCancellation)
 {
     this.stateManager = stateManager;
     this.context = context;
     this.serviceCancellationToken = serviceCancellation.Token;
 }
        public async Task SingleEvent()
        {
            ServiceCancellation cancelSource = new ServiceCancellation(CancellationToken.None);
            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, cancelSource);

            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();
            }
        }
 public DevicesController(IReliableStateManager stateManager, ServiceCancellation serviceCancellation)
 {
     this.stateManager             = stateManager;
     this.serviceCancellationToken = serviceCancellation.Token;
 }