Exemple #1
0
        private RemoteOperatorTestWrapperFakeSockets ArrangeLocalOperatorTestWrapper(
            IEnumerable <SocketTester> otherSockets = default,
            string address = "111.111.111.111:11111")
        {
            var remoteTraceMonitorСonsistent = new RemoteTraceMonitorСonsistentTester(null);
            var addressAndPort = address.Split(":");
            var recorder       = new ApplicationCacheRecorder();
            var sockets        = new Dictionary <string, SocketTester>();
            var socketTester1  = new SocketTester("connecter");

            socketTester1.SetLocalEndPoint(IPAddress.Parse(addressAndPort[0]), Convert.ToInt32(addressAndPort[1]));
            var socketTester2 = new SocketTester("listener");
            var socketTester3 = new SocketTester("accepted");

            if (otherSockets != default)
            {
                foreach (var socketTester in otherSockets)
                {
                    socketTester.SetLocalEndPoint(IPAddress.Parse(addressAndPort[0]), Convert.ToInt32(addressAndPort[1]));
                }
            }

            var socketFactory = ArrangeSocketFactoryLocal(
                socketTester1,
                socketTester2,
                socketTester3,
                otherSockets);
            var apiOperator = CreateLocalOperator(socketFactory, recorder, remoteTraceMonitorСonsistent, address);

            sockets.Add("connector", socketTester1);
            sockets.Add("listener", socketTester2);
            sockets.Add("accepted", socketTester3);

            return(new RemoteOperatorTestWrapperFakeSockets(sockets, apiOperator, recorder, remoteTraceMonitorСonsistent));
        }
Exemple #2
0
        public async void NetworkImitatorReceiveTestLoop()
        {
            var bytesToSend     = new byte[] { 0b00110000, 0b00110011 };
            var bytesReceived   = new byte[1024];
            var size            = 0;
            var socket1         = new SocketTester();
            var socket2         = new SocketTester();
            var networkImitator = new NetworkImitator(socket1, socket2);

            for (int i = 0; i < 3; i++)
            {
                var receivedTask = new TaskCompletionSource <bool>();
                bytesReceived = new byte[1024];
                Task.Run(async() =>
                {
                    size = socket2.Receive(bytesReceived);
                    receivedTask.SetResult(true);
                });

                await Task.Delay(500);

                socket1.Send(bytesToSend);
                await receivedTask.Task;

                var pureReceived = bytesReceived.Take(size).ToArray();
                Assert.Equal(bytesToSend, pureReceived);
            }
        }
Exemple #3
0
        public async void Operator_WhenSendWrongCommandToRemote_ShouldCallWrongCommand()
        {
            bool wrongCommandWasCalled = false;
            var  socket = new SocketTester("remote");

            socket.SetLocalEndPoint(IPAddress.Parse("111.111.111.111"), 11111);
            var local = ArrangeLocalOperatorTestWrapper(
                new [] { socket },
                "111.111.111.111:11111");

            var remote = ArrangeRemoteApiMapWithSocketsAndRecorders(socket, "222.222.222.222:2222");

            remote.ApiMap.RegisterWrongCommandHandler(() => wrongCommandWasCalled = true);
            await local.Initialized;
            await local.RaiseCommandReceived("connect 222.222.222.222:2222");

            local.Recorder.ClearCache();
            remote.Recorder.ClearCache();

            await local.RaiseCommandReceived("hello");

            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, local.Recorder);
            _output.WriteLine("-----------------------------");
            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, remote.Recorder);
            Assert.True(wrongCommandWasCalled);
        }
Exemple #4
0
        private (ITraceableRemoteApiMap, Dictionary <string, SocketTester>) ArrangeRemoteApiMapWithSockets(
            IApplicationRecorder applicationRecorder,
            IRecorder recorder,
            SocketTester socketConnecter,
            string address)
        {
            var sockets        = new Dictionary <string, SocketTester>();
            var socketListener = new SocketTester("listener");
            var socketAccepted = new SocketTester("accepted");
            var socketFactory  = ArrangeSocketFactoryTraceableRemoteApiMap(socketConnecter, socketListener, socketAccepted, isRemote: true);

            // RemoteApiMap
            var instructionReceiverFactory   = new TestInstructionReceiverFactory(socketFactory, recorder);
            var traceableRemoteApiMapFactory = new BaseTraceableRemoteApiMapFactory(
                instructionReceiverFactory,
                applicationRecorder);

            sockets.Add("connecter", socketConnecter);
            sockets.Add("listener", socketListener);
            sockets.Add("accepted", socketAccepted);

            var apiMap = traceableRemoteApiMapFactory.Create(address);

            return(apiMap, sockets);
        }
Exemple #5
0
        private TraceableRemoteApiMapWrapper ArrangeRemoteApiMapWithSocketsAndRecorders(
            SocketTester socketConnecter,
            string address)
        {
            var recorder = new ApplicationCacheRecorder();
            var result   = ArrangeRemoteApiMapWithSockets(recorder, recorder, socketConnecter, address);

            return(new TraceableRemoteApiMapWrapper(result.Item2, result.Item1, recorder));
        }
Exemple #6
0
        public async void Operator_WhenCalledConnectWithNotExistingAddress_ShouldCatchError()
        {
            var socket = new SocketTester();

            socket.ConnectAction = (address, i) => throw new Exception("Connect exception");
            var local = ArrangeLocalOperatorTestWrapper(new [] { socket });
            await local.Initialized;

            await local.RaiseCommandReceived("connect 123.0.0.2:2334");

            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, local.Recorder);
            Assert.Equal(1, socket.ConnectCalledTimes);
        }
Exemple #7
0
        private Func <AddressFamily, SocketType, ProtocolType, ISocket> ArrangeSocketFactoryTraceableRemoteApiMap(
            SocketTester socketConnecter,
            SocketTester socketListener,
            SocketTester socketAccepted,
            IEnumerable <SocketTester> otherSockets = default,
            bool isRemote = false)
        {
            socketAccepted.Connected = true;

            new NetworkImitator(socketConnecter, socketAccepted);

            var socketTesterFactory = Mock.Create <ISocketTesterFactory>();

            Mock.Arrange(() => socketTesterFactory.Create()).Returns(socketListener)
            .InSequence();

            if (otherSockets != default)
            {
                foreach (var socket in otherSockets)
                {
                    Mock.Arrange(() => socketTesterFactory.Create()).Returns(socket)
                    .InSequence();
                }
            }

            socketConnecter.ConnectCalled += (ip, address) =>
            {
                if (isRemote)
                {
                    socketAccepted.SetRemoteEndPoint(socketConnecter.LocalEndPoint.Address, address);
                    socketAccepted.SetLocalEndPoint(ip, 6666);
                }
                else
                {
                    socketAccepted.SetRemoteEndPoint(ip, address);
                    socketAccepted.SetLocalEndPoint(socketConnecter.LocalEndPoint.Address, 6666);
                }
                socketListener.RaiseSocketAccepted(socketAccepted);
            };

            ISocket SocketFactory(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            {
                return(socketTesterFactory.Create());
            }

            return(SocketFactory);
        }
Exemple #8
0
        public ConfigurationPanelViewModel(
            PersistenceManager persistenceManager,
            IVersionProvider versionProvider,
            ITrackRepository trackRepository)
        {
            _versionProvider = versionProvider;
            _trackRepository = trackRepository;
            _userSettings    = persistenceManager?.UserSettingsModel ??
                               throw new ArgumentNullException(nameof(persistenceManager));

            _socketTester = new SocketTester(persistenceManager);
            _socketTester.ConnectionChangeListener += status =>
            {
                ServiceStatus = status;
                OnPropertyChanged(nameof(ServiceStatus));
            };

            _socketTester.VerifyConnection();
        }
Exemple #9
0
        public async void Operator_WhenCalledConnectWithExistingAddress_ShouldConnect()
        {
            var socket      = new SocketTester("remote");
            var operatorApi = ArrangeLocalOperatorTestWrapper(
                new [] { socket },
                "111.111.111.111:11111");

            var remote = ArrangeRemoteApiMapWithSocketsAndRecorders(socket, "222.222.222.222:2222");
            await operatorApi.Initialized;

            operatorApi.Recorder.ClearCache();

            await operatorApi.RaiseCommandReceived("connect 222.222.222.222:2222");

            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, operatorApi.Recorder);
            _output.WriteLine("-----------------------------");
            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, remote.Recorder);
            Assert.Equal(1, socket.ConnectCalledTimes);
        }
Exemple #10
0
        public void OperatorConstructor_WhenCalled_ShouldListenFirstSocket()
        {
            var socketTesterFactory = Mock.Create <ISocketTesterFactory>();
            var socketTester1       = new SocketTester();
            var socketTester2       = new SocketTester();

            Mock.Arrange(() => socketTesterFactory.Create()).Returns(socketTester1)
            .InSequence();
            Mock.Arrange(() => socketTesterFactory.Create()).Returns(socketTester2)
            .InSequence();
            ISocket SocketFactory(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            {
                return(socketTesterFactory.Create());
            }

            CreateLocalOperator(SocketFactory, _recorder);

            Assert.Equal(1, socketTester1.ListenCalledTimes);
            Assert.Equal(0, socketTester2.ListenCalledTimes);
            Assert.Equal(1, socketTester2.ConnectCalledTimes);
        }
Exemple #11
0
        public async void NetworkImitatorDisconnectedTest()
        {
            var receivedTask  = new TaskCompletionSource <bool>();
            var bytesReceived = new byte[1024];
            var size          = 0;
            var socket2       = new SocketTester();

            bytesReceived = new byte[1024];
            Task.Run(async() =>
            {
                socket2.Connect(new IPAddress(0), 0);
                size = socket2.Receive(bytesReceived);
                receivedTask.SetResult(true);
            });

            await Task.Delay(500);

            socket2.RaiseDisconnected();
            await receivedTask.Task;

            Assert.Equal(0, size);
            Assert.False(socket2.Connected);
        }
Exemple #12
0
        public async void OperatorConstructor_WhenCalled_ShouldCallReceiveInAcceptedSocket()
        {
            var socketTester1 = new SocketTester("connector");
            var socketTester2 = new SocketTester("listener");
            var socketTester3 = new SocketTester("accepted");
            var socketFactory = ArrangeSocketFactoryLocal(socketTester1, socketTester2, socketTester3);

            var remoteTraceMonitorСonsistent = new RemoteTraceMonitorСonsistentTester(null);

            CreateLocalOperator(socketFactory, _cacheRecorder, remoteTraceMonitorСonsistent);

            await remoteTraceMonitorСonsistent.Initialized;

            _output.WriteLine("Errors:");
            _output.WriteLine(_cacheRecorder.ErrorCache);
            _output.WriteLine("Info:");
            _output.WriteLine(_cacheRecorder.InfoCache);
            Assert.Equal(3, socketTester3.ReceiveCalledTimes);
            Mock.Assert(() => _recorder.DefaultException(Arg.IsAny <Object>(), Arg.IsAny <Exception>()),
                        Occurs.Never());
            Mock.Assert(() => _recorder.RecordError(Arg.IsAny <string>(), Arg.IsAny <string>()),
                        Occurs.Never());
        }
Exemple #13
0
        public async void Operator_WhenRemoteSocketDisconnected_ShouldDisposeSocket()
        {
            var socket = new SocketTester("remote");
            var local  = ArrangeLocalOperatorTestWrapper(
                new [] { socket },
                "111.111.111.111:11111");

            var remote = ArrangeRemoteApiMapWithSocketsAndRecorders(socket, "222.222.222.222:2222");
            await local.Initialized;
            await local.RaiseCommandReceived("connect 222.222.222.222:2222");

            await remote.Connected;

            local.Recorder.ClearCache();
            remote.Recorder.ClearCache();

            socket.RaiseDisconnected();

            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, local.Recorder);
            _output.WriteLine("-----------------------------");
            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, remote.Recorder);
            Assert.Equal(1, remote.Sockets["connecter"].DisposeCalledTimes);
        }
Exemple #14
0
        public async void Operator_WhenSendCommandToRemote_ShouldReceiveCommand()
        {
            int command1ReceivedTimes = 0;
            int command2ReceivedTimes = 0;
            int command3ReceivedTimes = 0;
            var socket = new SocketTester("remote");
            var local  = ArrangeLocalOperatorTestWrapper(
                new [] { socket },
                "111.111.111.111:11111");

            var remote = ArrangeRemoteApiMapWithSocketsAndRecorders(socket, "222.222.222.222:2222");

            remote.ApiMap.RegisterCommand("command1", () => command1ReceivedTimes++);
            remote.ApiMap.RegisterCommand("command2", () => command2ReceivedTimes++);
            remote.ApiMap.RegisterCommand("command3", () => command3ReceivedTimes++);
            await local.Initialized;
            await local.RaiseCommandReceived("connect 222.222.222.222:2222");

            local.Recorder.ClearCache();
            remote.Recorder.ClearCache();

            await local.RaiseCommandReceived("command1");

            await local.RaiseCommandReceived("command2");

            await local.RaiseCommandReceived("command2");

            await local.RaiseCommandReceived("command3");

            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, local.Recorder);
            _output.WriteLine("-----------------------------");
            IntegrationTestsHelpers.LogCacheRecorderTestInfo(_output, remote.Recorder);
            Assert.Equal(1, command1ReceivedTimes);
            Assert.Equal(2, command2ReceivedTimes);
            Assert.Equal(1, command3ReceivedTimes);
        }
Exemple #15
0
 void Start()
 {
     SocketTester.Test();
 }