public void When_Registering_Existing_Connection_Throws() { // arrange using var cache = new MemoryCache(new MemoryCacheOptions()); using var connectionManager = new LoRaDeviceClientConnectionManager(cache, NullLogger <LoRaDeviceClientConnectionManager> .Instance); using var loraDevice = new LoRaDevice(null, new DevEui(0), null); connectionManager.Register(loraDevice, new Mock <ILoRaDeviceClient>().Object); Assert.Throws <InvalidOperationException>(() => connectionManager.Register(loraDevice, new Mock <ILoRaDeviceClient>().Object)); }
public void When_Needed_Should_Reconnect_Client() { var deviceClient = new Mock <ILoRaDeviceClient>(MockBehavior.Strict); var cache = new MemoryCache(new MemoryCacheOptions()); var manager = new LoRaDeviceClientConnectionManager(cache); var device = new LoRaDevice("00000000", "0123456789", manager); device.KeepAliveTimeout = 60; manager.Register(device, deviceClient.Object); deviceClient.Setup(x => x.EnsureConnected()) .Returns(true); deviceClient.Setup(x => x.Disconnect()) .Returns(true); using (var activity1 = device.BeginDeviceClientConnectionActivity()) { Assert.NotNull(activity1); } Assert.True(device.TryDisconnect()); using (var activity2 = device.BeginDeviceClientConnectionActivity()) { Assert.NotNull(activity2); Assert.False(device.TryDisconnect()); } Assert.True(device.TryDisconnect()); deviceClient.Verify(x => x.EnsureConnected(), Times.Exactly(2)); deviceClient.Verify(x => x.Disconnect(), Times.Exactly(2)); }
public void When_Disposing_Should_Dispose_All_Managed_Connections(int numberOfDevices) { // arrange using var cache = new MemoryCache(new MemoryCacheOptions()); using var connectionManager = new LoRaDeviceClientConnectionManager(cache, NullLogger <LoRaDeviceClientConnectionManager> .Instance); var deviceRegistrations = Enumerable.Range(0, numberOfDevices) .Select(i => TestUtils.CreateFromSimulatedDevice(new SimulatedDevice(TestDeviceInfo.CreateABPDevice((uint)i)), connectionManager)) .Select(d => (d, new Mock <ILoRaDeviceClient>())) .ToList(); foreach (var(d, c) in deviceRegistrations) { connectionManager.Register(d, c.Object); } // act connectionManager.Dispose(); // assert foreach (var(_, c) in deviceRegistrations) { c.Verify(client => client.Dispose(), Times.Exactly(2)); } }
public void When_Device_Has_No_Connection_Timeout_Should_Disconnect() { var deviceClient = new Mock <ILoRaDeviceClient>(MockBehavior.Strict); var cache = new MemoryCache(new MemoryCacheOptions()); var manager = new LoRaDeviceClientConnectionManager(cache); var device = new LoRaDevice("00000000", "0123456789", manager); manager.Register(device, deviceClient.Object); var activity = device.BeginDeviceClientConnectionActivity(); Assert.NotNull(activity); deviceClient.Setup(x => x.Disconnect()) .Returns(true); Assert.True(device.TryDisconnect()); deviceClient.Verify(x => x.Disconnect(), Times.Once()); }
public void When_Device_Has_No_Connection_Timeout_Should_Disconnect() { var deviceClient = new Mock <ILoRaDeviceClient>(MockBehavior.Strict); deviceClient.Setup(dc => dc.Dispose()); using var cache = new MemoryCache(new MemoryCacheOptions()); using var manager = new LoRaDeviceClientConnectionManager(cache, NullLogger <LoRaDeviceClientConnectionManager> .Instance); using var device = new LoRaDevice(DevAddr.Private0(0), new DevEui(0x0123456789), manager); manager.Register(device, deviceClient.Object); var activity = device.BeginDeviceClientConnectionActivity(); Assert.NotNull(activity); deviceClient.Setup(x => x.Disconnect()) .Returns(true); Assert.True(device.TryDisconnect()); deviceClient.Verify(x => x.Disconnect(), Times.Once()); }
public void When_Needed_Should_Reconnect_Client() { var deviceClient = new Mock <ILoRaDeviceClient>(MockBehavior.Strict); deviceClient.Setup(dc => dc.Dispose()); using var cache = new MemoryCache(new MemoryCacheOptions()); using var manager = new LoRaDeviceClientConnectionManager(cache, NullLogger <LoRaDeviceClientConnectionManager> .Instance); using var device = new LoRaDevice(DevAddr.Private0(0), new DevEui(0x0123456789), manager); device.KeepAliveTimeout = 60; manager.Register(device, deviceClient.Object); deviceClient.Setup(x => x.EnsureConnected()) .Returns(true); deviceClient.Setup(x => x.Disconnect()) .Returns(true); using (var activity1 = device.BeginDeviceClientConnectionActivity()) { Assert.NotNull(activity1); } Assert.True(device.TryDisconnect()); using (var activity2 = device.BeginDeviceClientConnectionActivity()) { Assert.NotNull(activity2); Assert.False(device.TryDisconnect()); } Assert.True(device.TryDisconnect()); deviceClient.Verify(x => x.EnsureConnected(), Times.Exactly(2)); deviceClient.Verify(x => x.Disconnect(), Times.Exactly(2)); }