Exemplo n.º 1
0
        public async Task ByLineMessagesOverSocketsTest()
        {
            var serverListener = new TcpListener(IPAddress.Loopback, TcpLocalTestPort);

            serverListener.Start();
            try
            {
                using (var tcpClient = new TcpClient())
                {
                    var serverAsync = serverListener.AcceptTcpClientAsync();
                    await tcpClient.ConnectAsync(IPAddress.Loopback, TcpLocalTestPort);

                    using (var tcpServer = await serverAsync)
                        using (var ss = tcpServer.GetStream())
                            using (var cs = tcpClient.GetStream())
                            {
                                using (var clientReader = new ByLineTextMessageReader(cs))
                                    using (var clientWriter = new ByLineTextMessageWriter(cs))
                                        using (var serverReader = new ByLineTextMessageReader(ss))
                                            using (var serverWriter = new ByLineTextMessageWriter(ss))
                                                using (var server = new ServerTestHelper(this, serverReader, serverWriter,
                                                                                         StreamRpcServerHandlerOptions.None))
                                                    using (var client = new ClientTestHelper(clientReader, clientWriter))
                                                        using (var server1 = new ServerTestHelper(this, clientReader, clientWriter,
                                                                                                  StreamRpcServerHandlerOptions.None))
                                                            using (var client1 = new ClientTestHelper(serverReader, serverWriter))
                                                            {
                                                                async Task ClientTestAsync()
                                                                {
                                                                    await Task.Yield();

                                                                    await TestRoutines.TestStubAsync(client.ClientStub);

                                                                    await TestRoutines.TestStubAsync(client.ClientExceptionStub);
                                                                }
                                                                async Task Client1TestAsync()
                                                                {
                                                                    await Task.Yield();

                                                                    await TestRoutines.TestStubAsync(client1.ClientStub);

                                                                    await TestRoutines.TestStubAsync(client1.ClientExceptionStub);
                                                                }

                                                                await Task.WhenAll(ClientTestAsync(), Client1TestAsync());
                                                            }
                            }
                }
            }
            finally
            {
                serverListener.Stop();
            }
        }
 public async Task PartwiseStreamCancellationTest()
 {
     (var ss, var cs) = FullDuplexStream.CreatePair();
     using (var clientReader = new ByLineTextMessageReader(cs))
         using (var clientWriter = new ByLineTextMessageWriter(cs))
             using (var serverReader = new ByLineTextMessageReader(ss))
                 using (var serverWriter = new ByLineTextMessageWriter(ss))
                     using (var server = new ServerTestHelper(this, serverReader, serverWriter,
                                                              StreamRpcServerHandlerOptions.SupportsRequestCancellation))
                         using (var client = new ClientTestHelper(clientReader, clientWriter))
                         {
                             await TestRoutines.TestCancellationAsync(client.ClientCancellationStub);
                         }
 }
        public async Task ConsistentResponseSequenceTest()
        {
            (var ss, var cs) = FullDuplexStream.CreatePair();
            using (var clientReader = new ByLineTextMessageReader(cs))
                using (var clientWriter = new ByLineTextMessageWriter(cs))
                    using (var serverReader = new ByLineTextMessageReader(ss))
                        using (var serverWriter = new ByLineTextMessageWriter(ss))
                            using (var client = new ClientTestHelper(clientReader, clientWriter))
                            {
                                using (var server = new ServerTestHelper(this, serverReader, serverWriter,
                                                                         StreamRpcServerHandlerOptions.None))
                                {
                                    // The responses are ordered by the time of completion.
                                    var delayTask =
                                        client.ClientStub.DelayAsync(TimeSpan.FromMilliseconds(200), CancellationToken.None);
                                    var addTask = client.ClientStub.AddAsync(3, -4);
                                    var result  = await addTask;
                                    Assert.Equal(-1, result);
                                    // addTask completes first.
                                    Assert.False(delayTask.IsCompleted);
                                    await delayTask;
                                }
                                using (var server = new ServerTestHelper(this, serverReader, serverWriter,
                                                                         StreamRpcServerHandlerOptions.ConsistentResponseSequence))
                                {
                                    // The responses are in the same order as the requests.
                                    var delayTask =
                                        client.ClientStub.DelayAsync(TimeSpan.FromMilliseconds(200), CancellationToken.None);
                                    var addTask = client.ClientStub.AddAsync(10, 20);
                                    await Task.Delay(100);

                                    // addTask is held up.
                                    Assert.False(addTask.IsCompleted);
                                    await delayTask;
                                    var   result = await addTask;
                                    Assert.Equal(30, result);
                                }
                            }
        }
        public async Task PartwiseStreamInteropTest()
        {
            (var ss, var cs) = FullDuplexStream.CreatePair();
            using (var clientReader = new ByLineTextMessageReader(cs))
                using (var clientWriter = new ByLineTextMessageWriter(cs))
                    using (var serverReader = new ByLineTextMessageReader(ss))
                        using (var serverWriter = new ByLineTextMessageWriter(ss))
                            using (var server = new ServerTestHelper(this, serverReader, serverWriter,
                                                                     StreamRpcServerHandlerOptions.None))
                                using (var client = new ClientTestHelper(clientReader, clientWriter))
                                {
                                    var e = Assert.Raises <MessageEventArgs>(h => client.ClientHandler.MessageSending += h,
                                                                             h => client.ClientHandler.MessageSending -= h,
                                                                             () => client.ClientStub.One());
                                    Assert.Equal("one", ((RequestMessage)e.Arguments.Message).Method);
                                    e = Assert.Raises <MessageEventArgs>(h => client.ClientHandler.MessageReceiving += h,
                                                                         h => client.ClientHandler.MessageReceiving -= h,
                                                                         () => client.ClientStub.One());
                                    Assert.Equal(new JValue(1), ((ResponseMessage)e.Arguments.Message).Result);
                                    await TestRoutines.TestStubAsync(client.ClientStub);

                                    await TestRoutines.TestStubAsync(client.ClientExceptionStub);
                                }
        }