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)); }
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); }
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); }