public async Task When_Device_Api_Throws_Error_Should_Not_Create_Any_Device() { var devAddr = new DevAddr(0x039090); var apiService = new Mock <LoRaDeviceAPIServiceBase>(MockBehavior.Strict); apiService.Setup(x => x.SearchByDevAddrAsync(It.IsAny <DevAddr>())) .Throws(new InvalidOperationException()); var deviceFactory = new Mock <ILoRaDeviceFactory>(MockBehavior.Strict); using var deviceCache = LoRaDeviceCacheDefault.CreateDefault(); var target = new DeviceLoaderSynchronizer( devAddr, apiService.Object, deviceFactory.Object, new NetworkServerConfiguration(), deviceCache, null, NullLogger <DeviceLoaderSynchronizer> .Instance); var ex = await Assert.ThrowsAsync <LoRaProcessingException>(async() => await target.LoadAsync()); Assert.IsType <InvalidOperationException>(ex.InnerException); Assert.Equal(0, deviceCache.CalculateStatistics().Count); // Device was searched by DevAddr apiService.VerifyAll(); }
public async Task When_Not_In_Cache_It_Is_Loaded() { using var cache = LoRaDeviceCacheDefault.CreateDefault(); var factory = new Mock <ILoRaDeviceFactory>(); using var joinDeviceLoader = new JoinDeviceLoader(DefaultDeviceInfo, factory.Object, cache, NullLogger <JoinDeviceLoader> .Instance); await joinDeviceLoader.LoadAsync(); factory.Verify(x => x.CreateAndRegisterAsync(DefaultDeviceInfo, It.IsAny <CancellationToken>()), Times.Once); Assert.True(joinDeviceLoader.CanCache); }
public async Task When_The_Load_Fails_CanCache_Is_False() { using var cache = LoRaDeviceCacheDefault.CreateDefault(); var factory = new Mock <ILoRaDeviceFactory>(); factory.Setup(x => x.CreateAndRegisterAsync(DefaultDeviceInfo, It.IsAny <CancellationToken>())) .ThrowsAsync(new LoRaProcessingException()); using var joinDeviceLoader = new JoinDeviceLoader(DefaultDeviceInfo, factory.Object, cache, NullLogger <JoinDeviceLoader> .Instance); Assert.Null(await joinDeviceLoader.LoadAsync()); Assert.False(joinDeviceLoader.CanCache); }
public async Task When_Device_Does_Not_Exist_Should_Complete_Requests_As_Failed(int loadDevicesDurationInMs) { var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1)); var devAddr = simulatedDevice.DevAddr.Value; var payload1 = simulatedDevice.CreateUnconfirmedDataUpMessage("1"); var apiService = new Mock <LoRaDeviceAPIServiceBase>(); var searchMock = apiService.Setup(x => x.SearchByDevAddrAsync(devAddr)); if (loadDevicesDurationInMs > 0) { searchMock.ReturnsAsync(new SearchDevicesResult(), TimeSpan.FromMilliseconds(loadDevicesDurationInMs)); } else { searchMock.ReturnsAsync(new SearchDevicesResult()); } var deviceFactory = new Mock <ILoRaDeviceFactory>(MockBehavior.Strict); using var deviceCache = LoRaDeviceCacheDefault.CreateDefault(); var target = new DeviceLoaderSynchronizer( devAddr, apiService.Object, deviceFactory.Object, new NetworkServerConfiguration(), deviceCache, null, NullLogger <DeviceLoaderSynchronizer> .Instance); _ = target.LoadAsync(); using var req1 = WaitableLoRaRequest.Create(payload1); target.Queue(req1); Assert.True(await req1.WaitCompleteAsync()); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByDevAddr, req1.ProcessingFailedReason); var payload2 = simulatedDevice.CreateUnconfirmedDataUpMessage("2"); using var req2 = WaitableLoRaRequest.Create(payload2); target.Queue(req2); Assert.True(await req2.WaitCompleteAsync()); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByDevAddr, req2.ProcessingFailedReason); // Device was searched by DevAddr apiService.VerifyAll(); }
public async Task When_Device_Does_Not_Match_Gateway_Should_Fail_Request() { var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1, gatewayID: "a_different_one")); var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234"); var apiService = new Mock <LoRaDeviceAPIServiceBase>(); var iotHubDeviceInfo = new IoTHubDeviceInfo(simulatedDevice.LoRaDevice.DevAddr, simulatedDevice.LoRaDevice.DevEui, "pk") { GatewayId = "a_different_one", NwkSKey = simulatedDevice.NwkSKey.Value }; apiService.Setup(x => x.SearchByDevAddrAsync(It.IsAny <DevAddr>())) .ReturnsAsync(new SearchDevicesResult(iotHubDeviceInfo.AsList())); var loRaDeviceClient = new Mock <ILoRaDeviceClient>(MockBehavior.Loose); using var deviceCache = LoRaDeviceCacheDefault.CreateDefault(); var deviceFactory = new TestLoRaDeviceFactory(loRaDeviceClient.Object, deviceCache, this.connectionManager); var target = new DeviceLoaderSynchronizer( simulatedDevice.DevAddr.Value, apiService.Object, deviceFactory, new NetworkServerConfiguration(), deviceCache, null, NullLogger <DeviceLoaderSynchronizer> .Instance); _ = target.LoadAsync(); using var request = WaitableLoRaRequest.Create(payload); target.Queue(request); Assert.True(await request.WaitCompleteAsync()); Assert.True(request.ProcessingFailed); Assert.Equal(LoRaDeviceRequestFailedReason.BelongsToAnotherGateway, request.ProcessingFailedReason); // device should not be initialized, since it belongs to another gateway loRaDeviceClient.Verify(x => x.GetTwinAsync(CancellationToken.None), Times.Never()); // device should not be disconnected (was never connected) loRaDeviceClient.Verify(x => x.Disconnect(), Times.Never()); // Device was searched by DevAddr apiService.VerifyAll(); // Device was created by factory loRaDeviceClient.VerifyAll(); }
public async Task When_Device_Is_Not_In_Cache_And_Found_In_Api_Does_Not_Match_Mic_Should_Fail_Request(string deviceGatewayID) { var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1, gatewayID: deviceGatewayID)); var apiService = new Mock <LoRaDeviceAPIServiceBase>(); var iotHubDeviceInfo = new IoTHubDeviceInfo(simulatedDevice.LoRaDevice.DevAddr, simulatedDevice.LoRaDevice.DevEui, "pk"); apiService.Setup(x => x.SearchByDevAddrAsync(It.IsAny <DevAddr>())) .ReturnsAsync(new SearchDevicesResult(iotHubDeviceInfo.AsList())); // Will get device twin var loRaDeviceClient = new Mock <ILoRaDeviceClient>(MockBehavior.Loose); loRaDeviceClient.Setup(x => x.GetTwinAsync(CancellationToken.None)) .ReturnsAsync(TestUtils.CreateABPTwin(simulatedDevice)); using var deviceCache = LoRaDeviceCacheDefault.CreateDefault(); var deviceFactory = new TestLoRaDeviceFactory(loRaDeviceClient.Object, deviceCache, this.connectionManager); var target = new DeviceLoaderSynchronizer( simulatedDevice.DevAddr.Value, apiService.Object, deviceFactory, new NetworkServerConfiguration(), deviceCache, null, NullLogger <DeviceLoaderSynchronizer> .Instance); _ = target.LoadAsync(); var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", appSKey: simulatedDevice.AppSKey, nwkSKey: TestKeys.CreateNetworkSessionKey(0xEEAAFF)); using var request = WaitableLoRaRequest.Create(payload); target.Queue(request); Assert.True(await request.WaitCompleteAsync()); Assert.True(request.ProcessingFailed); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByMicCheck, request.ProcessingFailedReason); // Device was searched by DevAddr apiService.VerifyAll(); loRaDeviceClient.VerifyAll(); }
public async Task When_One_Load_Is_Pending_Other_Is_Waiting() { using var cache = LoRaDeviceCacheDefault.CreateDefault(); var factory = new Mock <ILoRaDeviceFactory>(); using var device = new LoRaDevice(DefaultDeviceInfo.DevAddr, DefaultDeviceInfo.DevEUI, null); factory.Setup(x => x.CreateAndRegisterAsync(DefaultDeviceInfo, It.IsAny <CancellationToken>())) .ReturnsAsync(() => { cache.Register(device); return(device); }); using var joinDeviceLoader = new JoinDeviceLoader(DefaultDeviceInfo, factory.Object, cache, NullLogger <JoinDeviceLoader> .Instance); var t1 = joinDeviceLoader.LoadAsync(); var t2 = joinDeviceLoader.LoadAsync(); Assert.All(await Task.WhenAll(t1, t2), x => Assert.Equal(device, x)); factory.Verify(x => x.CreateAndRegisterAsync(DefaultDeviceInfo, It.IsAny <CancellationToken>()), Times.Once); }
private static LoRaDeviceCache CreateDefaultCache() => LoRaDeviceCacheDefault.CreateDefault();