public void TestConnect_FailToConnect() { Exception globalError = null; foreach ( var target in CreateTestTargets( () => new RpcClientOptions(), () => new EventHandler<RpcTransportErrorEventArgs>( ( sender, e ) => { globalError = e.RpcError.Value.ToException(); return; } ), () => null ) ) { using ( var socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ) ) using ( var server = ServerMock.CreateTcp( 57129, 8192 ) ) using ( var connectCompleted = new ManualResetEventSlim() ) { var connectClientMock = new AsyncConnectClientMock( socket, target.CreateSocketContext( new IPEndPoint( IPAddress.Loopback, 57128 ) ) ); object expectedAsyncState = new object(); var connectContext = new ConnectingContext( connectClientMock, expectedAsyncState ); int connected = 0; connectClientMock.Connected += ( context, completedSynchronously, asyncState ) => { Assert.IsFalse( completedSynchronously ); Assert.AreSame( expectedAsyncState, asyncState ); connected++; connectCompleted.Set(); }; Exception errorOnTest = null; connectClientMock.ConnectError += ( context, error, completedSynchronously, asyncState ) => { Assert.AreSame( expectedAsyncState, asyncState ); errorOnTest = error; Console.Error.WriteLine( "Error::CompletedSynchronously:{0}", completedSynchronously ); connectCompleted.Set(); }; target.Connect( connectContext ); Assert.IsTrue( connectCompleted.Wait( 5000 ) ); if ( globalError != null ) { Assert.Fail( globalError.ToString() ); } Assert.AreEqual( 0, connected ); Assert.NotNull( errorOnTest ); Assert.IsInstanceOf<SocketException>( errorOnTest, errorOnTest.ToString() ); } } }
private static void TestSendAndReceiveCore( int bufferSegmentSize, int bufferSegmentCount, String messageToSend ) { Exception globalError = null; foreach ( var target in CreateTestTargets( () => new RpcClientOptions() { BufferSegmentSize = bufferSegmentSize, BufferSegmentCount = bufferSegmentCount }, () => new EventHandler<RpcTransportErrorEventArgs>( ( sender, e ) => { globalError = e.RpcError.Value.ToException(); return; } ), () => null ) ) { using ( var socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ) ) using ( var server = ServerMock.CreateTcp( 57129, 8192 ) ) using ( var connectCompleted = new ManualResetEventSlim() ) using ( var receiveCompleted = new ManualResetEventSlim() ) { server.Received += ( sender, e ) => { Console.WriteLine( "Received. Type:{0}, ID:{1}, Method:'{2}', Arguments:'{3}'", e.GetRequest().MessageType, e.GetRequest().MessageId, e.GetRequest().Method, e.GetRequest().Arguments.Aggregate( new StringBuilder( "[" ), ( buffer, item ) => ( buffer.Length == 1 ? buffer : buffer.Append( ", " ) ).Append( item ) ).Append( ']' ) ); Assert.AreEqual( "Test", e.GetRequest().Method ); e.Reply( 1, messageToSend ); }; var socketContext = target.CreateSocketContext( new IPEndPoint( IPAddress.Loopback, 57129 ) ); var connectClientMock = new AsyncConnectClientMock( socket, socketContext ); connectClientMock.Connected += ( context, completedSynchronously, asyncState ) => { connectCompleted.Set(); }; Exception errorOnTest = null; connectClientMock.ConnectError += ( context, error, completedSynchronously, asyncState ) => { errorOnTest = error; connectCompleted.Set(); }; target.Connect( new ConnectingContext( connectClientMock, null ) ); Assert.IsTrue( connectCompleted.Wait( 3000 ) ); if ( errorOnTest != null ) { Assert.Fail( errorOnTest.ToString() ); } if ( globalError != null ) { Assert.Fail( globalError.ToString() ); } Assert.IsNotNull( socketContext.ConnectSocket ); var receiverMock = new TransportReceiveHandlerMock(); MessagePackObject? returned = null; receiverMock.Received += ( receiveContext ) => { var message = SerializationUtility.DeserializeResponse( receiveContext.ReceivingBuffer ); if ( message.Error != null ) { errorOnTest = message.Error; } else { returned = message.ReturnValue; } receiveCompleted.Set(); }; var sendingContext = new SendingContext( new ClientSessionContext( receiverMock, new RpcClientOptions(), socketContext ), new RpcOutputBuffer( ChunkBuffer.CreateDefault() ), 1, ( _sendingContext, error, completedSynchronously ) => { errorOnTest = error; } ); var requestBytes = CreateDefaultRequest(); sendingContext.SendingBuffer.OpenWriteStream().Write( requestBytes, 0, requestBytes.Length ); sendingContext.SocketContext.BufferList = sendingContext.SendingBuffer.DebugGetChunk(); target.Send( sendingContext ); Assert.IsTrue( receiveCompleted.Wait( MillisecondsTimeout ), "Timeout" ); Assert.IsTrue( returned.HasValue ); if ( errorOnTest != null ) { Assert.Fail( errorOnTest.ToString() ); } if ( globalError != null ) { Assert.Fail( globalError.ToString() ); } Assert.AreEqual( messageToSend, returned.Value.AsString() ); socket.Shutdown( SocketShutdown.Both ); } } }