public async Task ClientStreamingCall_CancelAfterBegin()
        {
            var barrier = new TaskCompletionSource <object>();

            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                barrier.SetResult(null);
                await requestStream.ToListAsync();
                return("");
            });

            var cts  = new CancellationTokenSource();
            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(cancellationToken: cts.Token)));

            await barrier.Task;  // make sure the handler has started.

            cts.Cancel();

            try
            {
                // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock.
                await call.ResponseAsync;
                Assert.Fail();
            }
            catch (RpcException ex)
            {
                Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
            }
        }
Example #2
0
        public async Task ClientStreamingCall()
        {
            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                string result = "";
                await requestStream.ForEachAsync((request) =>
                {
                    result += request;
                    return(TaskUtils.CompletedTask);
                });
                await Task.Delay(100);
                return(result);
            });

            {
                var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
                await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });

                Assert.AreEqual("ABC", await call);
                Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
                Assert.IsNotNull(call.GetTrailers());
            }

            {
                var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
                await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });

                Assert.AreEqual("ABC", await call.ConfigureAwait(false));
                Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
                Assert.IsNotNull(call.GetTrailers());
            }
        }
        public async Task RequestParsingError_StreamingRequest()
        {
            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                Assert.Throws <IOException>(async() => await requestStream.MoveNext());
                return("RESPONSE");
            });

            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
            await call.RequestStream.WriteAsync("UNPARSEABLE_VALUE");

            Assert.AreEqual("RESPONSE", await call);
        }
Example #4
0
        public async Task PropagateCancellation()
        {
            var readyToCancelTcs = new TaskCompletionSource <object>();
            var successTcs       = new TaskCompletionSource <string>();

            helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) =>
            {
                readyToCancelTcs.SetResult(null);  // child call running, ready to parent call

                while (!context.CancellationToken.IsCancellationRequested)
                {
                    await Task.Delay(10);
                }
                successTcs.SetResult("CHILD_CALL_CANCELLED");
                return("");
            });

            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                var propagationToken = context.CreatePropagationToken();
                Assert.IsNotNull(propagationToken.ParentCall);

                var callOptions = new CallOptions(propagationToken: propagationToken);
                try
                {
                    await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz");
                }
                catch (RpcException)
                {
                    // Child call will get cancelled, eat the exception.
                }
                return("");
            });

            var cts        = new CancellationTokenSource();
            var parentCall = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(cancellationToken: cts.Token)));
            await readyToCancelTcs.Task;

            cts.Cancel();
            try
            {
                // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock.
                await parentCall;
                Assert.Fail();
            }
            catch (RpcException)
            {
            }
            Assert.AreEqual("CHILD_CALL_CANCELLED", await successTcs.Task);
        }
Example #5
0
        public async Task ClientStreamingCall()
        {
            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                string result = "";
                await requestStream.ForEachAsync(async(request) =>
                {
                    result += request;
                });
                await Task.Delay(100);
                return(result);
            });

            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
            await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });

            Assert.AreEqual("ABC", await call.ResponseAsync);
        }
        public async Task RequestParsingError_StreamingRequest()
        {
            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                try
                {
                    // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock.
                    await requestStream.MoveNext();
                    Assert.Fail();
                }
                catch (IOException)
                {
                }
                return("RESPONSE");
            });

            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
            await call.RequestStream.WriteAsync("UNPARSEABLE_VALUE");

            Assert.AreEqual("RESPONSE", await call);
        }
Example #7
0
        public async Task PropagateCancellation()
        {
            helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) =>
            {
                // check that we didn't obtain the default cancellation token.
                Assert.IsTrue(context.CancellationToken.CanBeCanceled);
                return("PASS");
            });

            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                var propagationToken = context.CreatePropagationToken();
                Assert.IsNotNull(propagationToken.ParentCall);

                var callOptions = new CallOptions(propagationToken: propagationToken);
                return(await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz"));
            });

            var cts  = new CancellationTokenSource();
            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(cancellationToken: cts.Token)));
            await call.RequestStream.CompleteAsync();

            Assert.AreEqual("PASS", await call);
        }
Example #8
0
        public async Task ResponseHeadersAsync_ClientStreamingCall()
        {
            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                await context.WriteResponseHeadersAsync(headers);
                return("PASS");
            });

            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
            await call.RequestStream.CompleteAsync();

            var responseHeaders = await call.ResponseHeadersAsync;

            Assert.AreEqual("ascii-header", responseHeaders[0].Key);
            Assert.AreEqual("PASS", await call.ResponseAsync);
        }
Example #9
0
        public async Task HalfcloseAfterFullclose_ClientStreamingCall()
        {
            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                return("PASS");
            });

            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());

            // make sure server has fullclosed on us
            Assert.AreEqual("PASS", await call.ResponseAsync);

            // sending close from client should be still fine because server can finish
            // the call anytime and we cannot do anything about it on the client side.
            await call.RequestStream.CompleteAsync();

            // Second attempt to close from client is not allowed.
            Assert.ThrowsAsync(typeof(InvalidOperationException), async() => await call.RequestStream.CompleteAsync());
        }