Beispiel #1
0
        public async Task DisableHandlingEventsOnCloseTest()
        {
            // Arrange
            int connectionStatusChangedHandlerCount = 0;

            void ConnectionStatusChangedHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
            {
                Interlocked.Increment(ref connectionStatusChangedHandlerCount);
            }

            var deviceConnectivityManager = new DeviceConnectivityManager();
            var client = Mock.Of <IClient>();
            var connectivityAwareClient = new ConnectivityAwareClient(client, deviceConnectivityManager);

            connectivityAwareClient.SetConnectionStatusChangedHandler(ConnectionStatusChangedHandler);

            // Act
            deviceConnectivityManager.InvokeDeviceConnected();
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(2, connectionStatusChangedHandlerCount);

            // Act
            await connectivityAwareClient.CloseAsync();

            deviceConnectivityManager.InvokeDeviceConnected();
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(2, connectionStatusChangedHandlerCount);
        }
        public async Task TestNoSubscriptionWhenOpenFails()
        {
            // Arrange
            int connectionStatusChangedHandlerCount = 0;

            void ConnectionStatusChangedHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
            {
                Interlocked.Increment(ref connectionStatusChangedHandlerCount);
            }

            var deviceConnectivityManager = new DeviceConnectivityManager();
            var client = new Mock <IClient>();

            client.Setup(c => c.OpenAsync())
            .ThrowsAsync(new ArgumentException());

            var connectivityAwareClient = new ConnectivityAwareClient(client.Object, deviceConnectivityManager, Mock.Of <IIdentity>(i => i.Id == "d1"));

            connectivityAwareClient.SetConnectionStatusChangedHandler(ConnectionStatusChangedHandler);

            // Act
            deviceConnectivityManager.InvokeDeviceConnected();
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(0, connectionStatusChangedHandlerCount);

            // Act
            await Assert.ThrowsAsync <ArgumentException>(() => connectivityAwareClient.OpenAsync());

            deviceConnectivityManager.InvokeDeviceConnected();
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(0, connectionStatusChangedHandlerCount);
        }
        public async Task ConnectivityChangeTest()
        {
            // Arrange
            var receivedConnectionStatuses = new List <ConnectionStatus>();
            var receivedChangeReasons      = new List <ConnectionStatusChangeReason>();

            void ConnectionStatusChangedHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
            {
                receivedConnectionStatuses.Add(status);
                receivedChangeReasons.Add(reason);
            }

            var deviceConnectivityManager = new DeviceConnectivityManager();
            var client = Mock.Of <IClient>();

            Mock.Get(client).SetupSequence(c => c.SendEventAsync(It.IsAny <Client.Message>()))
            .Returns(Task.CompletedTask)
            .Throws(new TimeoutException());
            var connectivityAwareClient = new ConnectivityAwareClient(client, deviceConnectivityManager, Mock.Of <IIdentity>(i => i.Id == "d1"));

            connectivityAwareClient.SetConnectionStatusChangedHandler(ConnectionStatusChangedHandler);

            // Act
            await connectivityAwareClient.SendEventAsync(new Client.Message());

            // Assert
            Assert.Equal(1, receivedConnectionStatuses.Count);
            Assert.Equal(ConnectionStatus.Connected, receivedConnectionStatuses[0]);
            Assert.Equal(ConnectionStatusChangeReason.Connection_Ok, receivedChangeReasons[0]);

            // Act
            await Assert.ThrowsAsync <TimeoutException>(async() => await connectivityAwareClient.SendEventAsync(new Client.Message()));

            // Assert
            Assert.Equal(1, receivedConnectionStatuses.Count);
            Assert.Equal(ConnectionStatus.Connected, receivedConnectionStatuses[0]);
            Assert.Equal(ConnectionStatusChangeReason.Connection_Ok, receivedChangeReasons[0]);

            // Act
            deviceConnectivityManager.InvokeDeviceConnected();

            // Assert
            Assert.Equal(1, receivedConnectionStatuses.Count);
            Assert.Equal(ConnectionStatus.Connected, receivedConnectionStatuses[0]);
            Assert.Equal(ConnectionStatusChangeReason.Connection_Ok, receivedChangeReasons[0]);

            // Act
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(2, receivedConnectionStatuses.Count);
            Assert.Equal(ConnectionStatus.Disconnected, receivedConnectionStatuses[1]);
            Assert.Equal(ConnectionStatusChangeReason.No_Network, receivedChangeReasons[1]);

            // Act
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(2, receivedConnectionStatuses.Count);
            Assert.Equal(ConnectionStatus.Disconnected, receivedConnectionStatuses[1]);
            Assert.Equal(ConnectionStatusChangeReason.No_Network, receivedChangeReasons[1]);

            // Act
            await connectivityAwareClient.CloseAsync();

            deviceConnectivityManager.InvokeDeviceConnected();
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(2, receivedConnectionStatuses.Count);
            Assert.Equal(ConnectionStatus.Disconnected, receivedConnectionStatuses[1]);
            Assert.Equal(ConnectionStatusChangeReason.No_Network, receivedChangeReasons[1]);
        }
        public async Task ConnectivityChangeEventTest2()
        {
            // Arrange
            int connectedStatusChangedHandlerCount    = 0;
            int disconnectedStatusChangedHandlerCount = 0;

            void ConnectionStatusChangedHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
            {
                if (status == ConnectionStatus.Connected)
                {
                    Interlocked.Increment(ref connectedStatusChangedHandlerCount);
                }
                else
                {
                    Interlocked.Increment(ref disconnectedStatusChangedHandlerCount);
                }
            }

            var deviceConnectivityManager = new DeviceConnectivityManager();
            ConnectionStatusChangesHandler innerClientHandler = null;
            var client = new Mock <IClient>();

            client.Setup(c => c.SetConnectionStatusChangedHandler(It.IsAny <ConnectionStatusChangesHandler>()))
            .Callback <ConnectionStatusChangesHandler>(c => innerClientHandler = c);

            var connectivityAwareClient = new ConnectivityAwareClient(client.Object, deviceConnectivityManager, Mock.Of <IIdentity>(i => i.Id == "d1"));

            connectivityAwareClient.SetConnectionStatusChangedHandler(ConnectionStatusChangedHandler);

            // Act
            await connectivityAwareClient.OpenAsync();

            // Assert
            Assert.NotNull(innerClientHandler);

            // Act
            deviceConnectivityManager.InvokeDeviceConnected();

            // Assert
            Assert.Equal(1, connectedStatusChangedHandlerCount);
            Assert.Equal(0, disconnectedStatusChangedHandlerCount);

            // Act
            innerClientHandler.Invoke(ConnectionStatus.Connected, ConnectionStatusChangeReason.Connection_Ok);

            // Assert
            Assert.Equal(1, connectedStatusChangedHandlerCount);
            Assert.Equal(0, disconnectedStatusChangedHandlerCount);

            // Act
            deviceConnectivityManager.InvokeDeviceDisconnected();

            // Assert
            Assert.Equal(1, connectedStatusChangedHandlerCount);
            Assert.Equal(1, disconnectedStatusChangedHandlerCount);

            // Act
            innerClientHandler.Invoke(ConnectionStatus.Disconnected, ConnectionStatusChangeReason.Communication_Error);

            // Assert
            Assert.Equal(1, connectedStatusChangedHandlerCount);
            Assert.Equal(1, disconnectedStatusChangedHandlerCount);
        }