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

            return(false, default);
コード例 #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());
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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);
        }