Пример #1
0
        public async Task ResponseHeadersAsync_ServerStreamingCall()
        {
            helper.ServerStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) =>
            {
                await context.WriteResponseHeadersAsync(headers);
                await responseStream.WriteAsync("PASS");
            });

            var call            = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "");
            var responseHeaders = await call.ResponseHeadersAsync;

            Assert.AreEqual("ascii-header", responseHeaders[0].Key);
            CollectionAssert.AreEqual(new[] { "PASS" }, await call.ResponseStream.ToListAsync());
        }
Пример #2
0
        public async Task ServerStreamingCall()
        {
            helper.ServerStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) =>
            {
                await responseStream.WriteAllAsync(request.Split(new [] { ' ' }));
                context.ResponseTrailers.Add("xyz", "");
            });

            var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "A B C");

            CollectionAssert.AreEqual(new string[] { "A", "B", "C" }, await call.ResponseStream.ToListAsync());

            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
            Assert.AreEqual("xyz", call.GetTrailers()[0].Key);
        }
Пример #3
0
        public async Task ServerStreamingCall_CancelClientSideRead()
        {
            helper.ServerStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) =>
            {
                await responseStream.WriteAsync("abc");
                while (!context.CancellationToken.IsCancellationRequested)
                {
                    await Task.Delay(10);
                }
            });

            var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "");
            await call.ResponseStream.MoveNext();

            Assert.AreEqual("abc", call.ResponseStream.Current);

            var cts          = new CancellationTokenSource();
            var moveNextTask = call.ResponseStream.MoveNext(cts.Token);

            cts.Cancel();

            try
            {
                // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock.
                await moveNextTask;
                Assert.Fail();
            }
            catch (RpcException ex)
            {
                Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
            }
        }
Пример #4
0
        public void ResponseParsingError_StreamingResponse()
        {
            helper.ServerStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) =>
            {
                await responseStream.WriteAsync("UNPARSEABLE_VALUE");
                await Task.Delay(10000);
            });

            var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "REQUEST");
            var ex   = Assert.ThrowsAsync <RpcException>(async() => await call.ResponseStream.MoveNext());

            Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode);
        }
Пример #5
0
        public async Task ServiceConfigRetryPolicy_ServerStreaming()
        {
            var counter = new AtomicCounter();

            helper.ServerStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) =>
            {
                var attempt = counter.Increment();
                if (attempt <= 2)
                {
                    throw new RpcException(new Status(StatusCode.Unavailable, $"Attempt {attempt} failed on purpose"));
                }
                await responseStream.WriteAllAsync(request.Split(new [] { ' ' }));
            });

            var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "A B C");

            CollectionAssert.AreEqual(new string[] { "A", "B", "C" }, await call.ResponseStream.ToListAsync());
            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
        }
Пример #6
0
        public async Task WriteResponseHeaders_NotAllowedAfterWrite()
        {
            helper.ServerStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) =>
            {
                await responseStream.WriteAsync("A");
                try
                {
                    await context.WriteResponseHeadersAsync(headers);
                    Assert.Fail();
                }
                catch (InvalidOperationException expected)
                {
                }
                await responseStream.WriteAsync("B");
            });

            var call      = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "");
            var responses = await call.ResponseStream.ToListAsync();

            CollectionAssert.AreEqual(new[] { "A", "B" }, responses);
        }