Beispiel #1
0
        private (bool, ISession) TryConnectInternal(INetworkAddress networkAddress)
        {
            if (_networkConnector.TryConnect(networkAddress,
                                             out INetworkTunnel tunnel))
            {
                return(true, _sessionFactory.Invoke(tunnel));
            }

            return(false, default);
Beispiel #2
0
        public void TryConnect_WithNetworkAddress_ShouldCallSocketConnect()
        {
            var socket = Mock.Create <ISocket>();

            _socketFactory = (a, s, p) => socket;
            _sut           = new NetworkConnector(socket => Mock.Create <INetworkTunnel>(),
                                                  _socketFactory);
            var networkAddress = Mock.Create <INetworkAddress>();

            Mock.Arrange(() => networkAddress.IP).Returns(IPAddress.Parse("192.168.0.1"));
            _sut.TryConnect(networkAddress, out INetworkTunnel tunnel);

            Mock.Assert(() => socket.Connect(Arg.IsAny <IPAddress>(), Arg.AnyInt), Occurs.Once());
        }
Beispiel #3
0
        public void TryConnect_WithNetworkAddress_ShouldReturnTrue()
        {
            var socket = Mock.Create <ISocket>();

            _socketFactory = (a, s, p) => socket;
            _sut           = new NetworkConnector(socket => Mock.Create <INetworkTunnel>(),
                                                  _socketFactory);
            var networkAddress = Mock.Create <INetworkAddress>();

            Mock.Arrange(() => networkAddress.IP).Returns(IPAddress.Parse("192.168.0.1"));

            var result = _sut.TryConnect(networkAddress, out INetworkTunnel tunnel);

            Assert.True(result);
        }
Beispiel #4
0
        public void TryConnect_OnSocketException_ShouldReturnFalse()
        {
            var socket = Mock.Create <ISocket>();

            _socketFactory = (a, s, p) => socket;
            _sut           = new NetworkConnector(socket => Mock.Create <INetworkTunnel>(),
                                                  _socketFactory);
            Mock.Arrange(() => socket.Connect(null, 0)).IgnoreArguments().Throws <Exception>();
            var networkAddress = Mock.Create <INetworkAddress>();

            Mock.Arrange(() => networkAddress.IP).Returns(IPAddress.Parse("192.168.0.1"));

            var result = _sut.TryConnect(networkAddress, out INetworkTunnel tunnel);

            Assert.False(result);
        }
Beispiel #5
0
        public void TryConnect_WithNetworkAddress_ShouldCreateSocket()
        {
            var wasCalledTimes = 0;

            _socketFactory = (a, s, p) =>
            {
                wasCalledTimes++;
                return(Mock.Create <ISocket>());
            };
            _sut = new NetworkConnector(socket => Mock.Create <INetworkTunnel>(),
                                        _socketFactory);
            var networkAddress = Mock.Create <INetworkAddress>();

            Mock.Arrange(() => networkAddress.IP).Returns(IPAddress.Parse("192.168.0.1"));

            _sut.TryConnect(networkAddress, out INetworkTunnel tunnel);

            Assert.Equal(1, wasCalledTimes);
        }