Ejemplo n.º 1
0
        public static void RunCancelAfterFirstResponse(TestService.ITestServiceClient client)
        {
            Task.Run(async() =>
            {
                Console.WriteLine("running cancel_after_first_response");

                var cts = new CancellationTokenSource();
                using (var call = client.FullDuplexCall(cancellationToken: cts.Token))
                {
                    await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                                                        .SetResponseType(PayloadType.COMPRESSABLE)
                                                        .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
                                                        .SetPayload(CreateZerosPayload(27182)).Build());

                    Assert.IsTrue(await call.ResponseStream.MoveNext());
                    Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                    Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);

                    cts.Cancel();

                    try
                    {
                        await call.ResponseStream.MoveNext();
                        Assert.Fail();
                    }
                    catch (RpcException e)
                    {
                        Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
                    }
                }
                Console.WriteLine("Passed!");
            }).Wait();
        }
Ejemplo n.º 2
0
        public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running cancel_after_first_response");

            var cts = new CancellationTokenSource();

            using (var call = client.FullDuplexCall(cancellationToken: cts.Token))
            {
                await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
                {
                    ResponseType       = PayloadType.COMPRESSABLE,
                    ResponseParameters = { new ResponseParameters {
                                               Size = 31415
                                           } },
                    Payload = CreateZerosPayload(27182)
                });

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);

                cts.Cancel();

                var ex = Assert.Throws <RpcException>(async() => await call.ResponseStream.MoveNext());
                Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
            }
            Console.WriteLine("Passed!");
        }
Ejemplo n.º 3
0
        public static void RunPingPong(TestService.ITestServiceClient client)
        {
            Task.Run(async() =>
            {
                Console.WriteLine("running ping_pong");

                var call = client.FullDuplexCall();

                StreamingOutputCallResponse response;

                await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
                                               .SetResponseType(PayloadType.COMPRESSABLE)
                                               .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
                                               .SetPayload(CreateZerosPayload(27182)).Build());

                response = await call.ResponseStream.ReadNext();
                Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
                Assert.AreEqual(31415, response.Payload.Body.Length);

                await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
                                               .SetResponseType(PayloadType.COMPRESSABLE)
                                               .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
                                               .SetPayload(CreateZerosPayload(8)).Build());

                response = await call.ResponseStream.ReadNext();
                Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
                Assert.AreEqual(9, response.Payload.Body.Length);

                await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
                                               .SetResponseType(PayloadType.COMPRESSABLE)
                                               .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
                                               .SetPayload(CreateZerosPayload(1828)).Build());

                response = await call.ResponseStream.ReadNext();
                Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
                Assert.AreEqual(2653, response.Payload.Body.Length);

                await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
                                               .SetResponseType(PayloadType.COMPRESSABLE)
                                               .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
                                               .SetPayload(CreateZerosPayload(45904)).Build());

                response = await call.ResponseStream.ReadNext();
                Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
                Assert.AreEqual(58979, response.Payload.Body.Length);

                await call.RequestStream.Close();

                response = await call.ResponseStream.ReadNext();
                Assert.AreEqual(null, response);

                Console.WriteLine("Passed!");
            }).Wait();
        }
Ejemplo n.º 4
0
        public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running empty_stream");
            using (var call = client.FullDuplexCall())
            {
                await call.RequestStream.CompleteAsync();

                var responseList = await call.ResponseStream.ToListAsync();

                Assert.AreEqual(0, responseList.Count);
            }
            Console.WriteLine("Passed!");
        }
Ejemplo n.º 5
0
        public static void RunEmptyStream(TestService.ITestServiceClient client)
        {
            Task.Run(async() =>
            {
                Console.WriteLine("running empty_stream");
                var call = client.FullDuplexCall();
                await call.Close();

                var responseList = await call.ResponseStream.ToList();
                Assert.AreEqual(0, responseList.Count);

                Console.WriteLine("Passed!");
            }).Wait();
        }
Ejemplo n.º 6
0
        public static async Task RunCustomMetadataAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running custom_metadata");
            {
                // step 1: test unary call
                var request = new SimpleRequest
                {
                    ResponseType = PayloadType.COMPRESSABLE,
                    ResponseSize = 314159,
                    Payload      = CreateZerosPayload(271828)
                };

                var call = client.UnaryCallAsync(request, headers: CreateTestMetadata());
                await call.ResponseAsync;

                var responseHeaders  = await call.ResponseHeadersAsync;
                var responseTrailers = call.GetTrailers();

                Assert.AreEqual("test_initial_metadata_value", responseHeaders.First((entry) => entry.Key == "x-grpc-test-echo-initial").Value);
                CollectionAssert.AreEqual(new byte[] { 0xab, 0xab, 0xab }, responseTrailers.First((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ValueBytes);
            }

            {
                // step 2: test full duplex call
                var request = new StreamingOutputCallRequest
                {
                    ResponseType       = PayloadType.COMPRESSABLE,
                    ResponseParameters = { new ResponseParameters {
                                               Size = 31415
                                           } },
                    Payload = CreateZerosPayload(27182)
                };

                var call            = client.FullDuplexCall(headers: CreateTestMetadata());
                var responseHeaders = await call.ResponseHeadersAsync;

                await call.RequestStream.WriteAsync(request);

                await call.RequestStream.CompleteAsync();

                await call.ResponseStream.ToListAsync();

                var responseTrailers = call.GetTrailers();

                Assert.AreEqual("test_initial_metadata_value", responseHeaders.First((entry) => entry.Key == "x-grpc-test-echo-initial").Value);
                CollectionAssert.AreEqual(new byte[] { 0xab, 0xab, 0xab }, responseTrailers.First((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ValueBytes);
            }

            Console.WriteLine("Passed!");
        }
Ejemplo n.º 7
0
        public static async Task RunPingPongAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running ping_pong");

            using (var call = client.FullDuplexCall())
            {
                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                                                    .SetResponseType(PayloadType.COMPRESSABLE)
                                                    .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
                                                    .SetPayload(CreateZerosPayload(27182)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                                                    .SetResponseType(PayloadType.COMPRESSABLE)
                                                    .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
                                                    .SetPayload(CreateZerosPayload(8)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                                                    .SetResponseType(PayloadType.COMPRESSABLE)
                                                    .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
                                                    .SetPayload(CreateZerosPayload(1828)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                                                    .SetResponseType(PayloadType.COMPRESSABLE)
                                                    .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
                                                    .SetPayload(CreateZerosPayload(45904)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.CompleteAsync();

                Assert.IsFalse(await call.ResponseStream.MoveNext());
            }
            Console.WriteLine("Passed!");
        }
Ejemplo n.º 8
0
        public static async Task RunTimeoutOnSleepingServerAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running timeout_on_sleeping_server");

            var deadline = DateTime.UtcNow.AddMilliseconds(1);

            using (var call = client.FullDuplexCall(deadline: deadline))
            {
                try
                {
                    await call.RequestStream.WriteAsync(new StreamingOutputCallRequest { Payload = CreateZerosPayload(27182) });
                }
                catch (InvalidOperationException)
                {
                    // Deadline was reached before write has started. Eat the exception and continue.
                }

                var ex = Assert.Throws <RpcException>(async() => await call.ResponseStream.MoveNext());
                Assert.AreEqual(StatusCode.DeadlineExceeded, ex.Status.StatusCode);
            }
            Console.WriteLine("Passed!");
        }
Ejemplo n.º 9
0
        public static async Task RunStatusCodeAndMessageAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running status_code_and_message");
            var echoStatus = new EchoStatus
            {
                Code    = 2,
                Message = "test status message"
            };

            {
                // step 1: test unary call
                var request = new SimpleRequest {
                    ResponseStatus = echoStatus
                };

                var e = Assert.Throws <RpcException>(() => client.UnaryCall(request));
                Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode);
                Assert.AreEqual(echoStatus.Message, e.Status.Detail);
            }

            {
                // step 2: test full duplex call
                var request = new StreamingOutputCallRequest {
                    ResponseStatus = echoStatus
                };

                var call = client.FullDuplexCall();
                await call.RequestStream.WriteAsync(request);

                await call.RequestStream.CompleteAsync();

                var e = Assert.Throws <RpcException>(async() => await call.ResponseStream.ToListAsync());
                Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode);
                Assert.AreEqual(echoStatus.Message, e.Status.Detail);
            }

            Console.WriteLine("Passed!");
        }
Ejemplo n.º 10
0
        public static async Task RunPingPongAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running ping_pong");

            using (var call = client.FullDuplexCall())
            {
                await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
                {
                    ResponseType       = PayloadType.COMPRESSABLE,
                    ResponseParameters = { new ResponseParameters {
                                               Size = 31415
                                           } },
                    Payload = CreateZerosPayload(27182)
                });

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
                {
                    ResponseType       = PayloadType.COMPRESSABLE,
                    ResponseParameters = { new ResponseParameters {
                                               Size = 9
                                           } },
                    Payload = CreateZerosPayload(8)
                });

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
                {
                    ResponseType       = PayloadType.COMPRESSABLE,
                    ResponseParameters = { new ResponseParameters {
                                               Size = 2653
                                           } },
                    Payload = CreateZerosPayload(1828)
                });

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
                {
                    ResponseType       = PayloadType.COMPRESSABLE,
                    ResponseParameters = { new ResponseParameters {
                                               Size = 58979
                                           } },
                    Payload = CreateZerosPayload(45904)
                });

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.CompleteAsync();

                Assert.IsFalse(await call.ResponseStream.MoveNext());
            }
            Console.WriteLine("Passed!");
        }