public void TestConstructor_IOCompletion_CancellationTokenIsNull()
 {
     using ( var target = new IOCompletionPortClientEventLoop( new RpcClientOptions(), ( sender, e ) => { return; }, null ) )
     {
         // Solely CancellationTokenSource will return valid token.
         Assert.IsTrue( target.CancellationToken.CanBeCanceled );
     }
 }
Exemple #2
0
        public void TestTcpSend()
        {
            using ( var server = ServerMock.CreateTcp( 57129, 8192 ) )
            using ( var serverDone = new ManualResetEventSlim() )
            {
                Exception exceptionOnServer = null;
                int? requestId = null;
                server.Received +=
                    ( sender, e ) =>
                    {
                        try
                        {
                            var request = e.GetRequest();
                            requestId = request.MessageId;
                            Assert.AreEqual( MessageType.Request, request.MessageType );
                            Assert.AreEqual( "Test", request.Method );
                            Assert.AreEqual( 3, request.Arguments.Count, "Invalid argument count." );
                            Assert.AreEqual( true, request.Arguments[ 0 ].AsBoolean() );
                            Assert.AreEqual( "Test", request.Arguments[ 1 ].AsString() );
                            Assert.IsTrue( request.Arguments[ 2 ].IsArray );
                            var argments = request.Arguments[ 2 ].AsList();
                            Assert.AreEqual( 1, argments[ 0 ].AsInt32() );
                            Assert.AreEqual( 2, argments[ 1 ].AsInt32() );
                            Assert.AreEqual( 3, argments[ 2 ].AsInt32() );
                            e.Reply( requestId, new MessagePackObject( "Hello, world!" ) );
                        }
                        catch ( SocketException ex )
                        {
                            if ( ex.SocketErrorCode != SocketError.OperationAborted )
                            {
                                Console.Error.WriteLine( "{0}:{1}", ex.SocketErrorCode, ex );
                                exceptionOnServer = ex;
                            }
                        }
                        catch ( Exception ex )
                        {
                            Console.Error.WriteLine( ex );
                            exceptionOnServer = ex;
                        }
                        finally
                        {
                            serverDone.Set();
                        }
                    };
                var options = new RpcClientOptions();
                options.ForceIPv4 = true;
                RpcErrorMessage? error = null;
                using ( var eventLoop = new IOCompletionPortClientEventLoop( options, ( sender, e ) => error = e.RpcError, null ) )
                {
                    RpcClient client = RpcClient.CreateTcp( new IPEndPoint( IPAddress.Loopback, 57129 ), eventLoop, options );
                    var ar = client.BeginCall( "Test", new object[] { true, "Test", new int[] { 1, 2, 3 } }, null, null );

                    serverDone.Wait();

                    if ( exceptionOnServer != null )
                    {
                        throw new AssertionException( "Server error.", exceptionOnServer );
                    }

                    var result = client.EndCall( ar );

                    if ( error != null )
                    {
                        Assert.Fail( error.Value.ToString() );
                    }

                    Assert.AreEqual( "Hello, world!", result.AsString() );
                }
            }
        }