Esempio n. 1
0
 public async Task ServerStreamingManyMessagesPerformance(ChannelContextFactory factory)
 {
     using var ctx = factory.Create();
     var stopwatch = Stopwatch.StartNew();
     var call      = ctx.Client.ServerStreaming(new RequestMessage {
         Value = 10_000
     });
        public async Task DuplexStreaming(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call = ctx.Client.DuplexStreaming();

            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(10, call.ResponseStream.Current.Value);
            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(11, call.ResponseStream.Current.Value);

            await call.RequestStream.WriteAsync(new RequestMessage { Value = 3 });

            await call.RequestStream.WriteAsync(new RequestMessage { Value = 2 });

            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(3, call.ResponseStream.Current.Value);
            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(2, call.ResponseStream.Current.Value);

            await call.RequestStream.WriteAsync(new RequestMessage { Value = 1 });

            await call.RequestStream.CompleteAsync();

            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(1, call.ResponseStream.Current.Value);
            Assert.False(await call.ResponseStream.MoveNext());
        }
        public async Task SimpleUnaryAsync(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var response = await ctx.Client.SimpleUnaryAsync(new RequestMessage { Value = 10 });

            Assert.Equal(10, response.Value);
            Assert.True(ctx.Impl.SimplyUnaryCalled);
        }
        public async Task SetStatus(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call      = ctx.Client.SetStatusAsync(new RequestMessage());
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await call);

            Assert.Equal(StatusCode.InvalidArgument, exception.Status.StatusCode);
            Assert.Equal("invalid argument", exception.Status.Detail);
        }
        public async Task Deadline(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var deadline  = DateTime.UtcNow + TimeSpan.FromSeconds(0.1);
            var call      = ctx.Client.DelayedUnaryAsync(new RequestMessage(), deadline: deadline);
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await call);

            Assert.Equal(StatusCode.DeadlineExceeded, exception.StatusCode);
        }
        public async Task ThrowingUnary(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var responseTask = ctx.Client.ThrowingUnaryAsync(new RequestMessage {
                Value = 10
            });
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await responseTask);

            Assert.Equal(StatusCode.Unknown, exception.StatusCode);
            Assert.Equal("Exception was thrown by handler.", exception.Status.Detail);
        }
        public async Task LargePayload(ChannelContextFactory factory)
        {
            var bytes = new byte[1024 * 1024];

            new Random(1234).NextBytes(bytes);
            var byteString = ByteString.CopyFrom(bytes);

            using var ctx = factory.Create();
            var response = await ctx.Client.SimpleUnaryAsync(new RequestMessage { Binary = byteString });

            Assert.Equal(byteString, response.Binary);
        }
        public async Task ThrowRpcExceptionUnary(ChannelContextFactory factory)
        {
            using var ctx             = factory.Create();
            ctx.Impl.ExceptionToThrow = new RpcException(new Status(StatusCode.InvalidArgument, "Bad arg"));
            var responseTask = ctx.Client.ThrowingUnaryAsync(new RequestMessage {
                Value = 10
            });
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await responseTask);

            Assert.Equal(StatusCode.InvalidArgument, exception.StatusCode);
            Assert.Equal("Bad arg", exception.Status.Detail);
        }
Esempio n. 9
0
        public async Task ThrowingServerStreaming(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call = ctx.Client.ThrowingServerStreaming(new RequestMessage {
                Value = 1
            });

            Assert.True(await call.ResponseStream.MoveNext());
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await call.ResponseStream.MoveNext());

            Assert.Equal(StatusCode.Unknown, exception.StatusCode);
            Assert.Equal("Exception was thrown by handler.", exception.Status.Detail);
        }
        public async Task CancelUnary(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var cts          = new CancellationTokenSource();
            var responseTask =
                ctx.Client.DelayedUnaryAsync(new RequestMessage {
                Value = 10
            }, cancellationToken: cts.Token);

            cts.CancelAfter(100);
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await responseTask);

            Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
        }
        public async Task CancelServerStreamingBeforeCall(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var cts = new CancellationTokenSource();

            cts.Cancel();
            var call = ctx.Client.DelayedServerStreaming(new RequestMessage {
                Value = 3
            },
                                                         cancellationToken: cts.Token);
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await call.ResponseStream.MoveNext());

            Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
        }
        public async Task CancelDuplexStreaming(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var cts  = new CancellationTokenSource();
            var call = ctx.Client.DelayedDuplexStreaming(cancellationToken: cts.Token);

            cts.CancelAfter(100);
            await call.RequestStream.WriteAsync(new RequestMessage { Value = 1 });

            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(1, call.ResponseStream.Current.Value);
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await call.ResponseStream.MoveNext());

            Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
        }
        public async Task ServerStreaming(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call = ctx.Client.ServerStreaming(new RequestMessage {
                Value = 3
            });

            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(3, call.ResponseStream.Current.Value);
            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(2, call.ResponseStream.Current.Value);
            Assert.True(await call.ResponseStream.MoveNext());
            Assert.Equal(1, call.ResponseStream.Current.Value);
            Assert.False(await call.ResponseStream.MoveNext());
        }
        public async Task CancelUnaryBeforeCall(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var cts = new CancellationTokenSource();

            cts.Cancel();
            var responseTask =
                ctx.Client.SimpleUnaryAsync(new RequestMessage {
                Value = 10
            }, cancellationToken: cts.Token);
            var exception = await Assert.ThrowsAsync <RpcException>(async() => await responseTask);

            Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
            Assert.False(ctx.Impl.SimplyUnaryCalled);
        }
Esempio n. 15
0
        public async Task ClientStreamWriteAfterCompletion(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call = ctx.Client.ClientStreaming();
            await call.RequestStream.WriteAsync(new RequestMessage { Value = 1 });

            await call.RequestStream.CompleteAsync();

            void WriteAction() => call.RequestStream.WriteAsync(new RequestMessage {
                Value = 1
            });

            var exception = Assert.Throws <InvalidOperationException>(WriteAction);

            Assert.Equal("Request stream has already been completed.", exception.Message);
        }
        public async Task CancelClientStreamingBeforeCall(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var cts = new CancellationTokenSource();

            cts.Cancel();
            var call = ctx.Client.ClientStreaming(cancellationToken: cts.Token);
            await Assert.ThrowsAsync <TaskCanceledException>(async() =>
                                                             await call.RequestStream.WriteAsync(new RequestMessage {
                Value = 1
            }));

            var exception = await Assert.ThrowsAsync <RpcException>(async() => await call.ResponseAsync);

            Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
        }
        public async Task ClientStreaming(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call = ctx.Client.ClientStreaming();
            await call.RequestStream.WriteAsync(new RequestMessage { Value = 3 });

            await call.RequestStream.WriteAsync(new RequestMessage { Value = 2 });

            await call.RequestStream.WriteAsync(new RequestMessage { Value = 1 });

            await call.RequestStream.CompleteAsync();

            var response = await call.ResponseAsync;

            Assert.Equal(6, response.Value);
        }
Esempio n. 18
0
        public async Task ServerStreamWriteAfterCompletion(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var call = ctx.Client.ServerStreaming(new RequestMessage {
                Value = 1
            });

            Assert.True(await call.ResponseStream.MoveNext());
            Assert.False(await call.ResponseStream.MoveNext());
            void WriteAction() => ctx.Impl.ServerStream.WriteAsync(new ResponseMessage {
                Value = 1
            });

            var exception = Assert.Throws <InvalidOperationException>(WriteAction);

            Assert.Equal("Response stream has already been completed.", exception.Message);
        }
        public async Task HeadersAndTrailers(ChannelContextFactory factory)
        {
            using var ctx = factory.Create();
            var requestHeaders = new Metadata
            {
                { "A1", "1" },
                { "A2-bin", new[] { (byte)2 } },
            };
            var responseHeaders = new Metadata
            {
                { "B1", "1" },
                { "B2-bin", new[] { (byte)2 } },
            };
            var responseTrailers = new Metadata
            {
                { "C1", "1" },
                { "C2-bin", new[] { (byte)2 } },
            };

            ctx.Impl.ResponseHeaders  = responseHeaders;
            ctx.Impl.ResponseTrailers = responseTrailers;
            var call = ctx.Client.HeadersTrailersAsync(new RequestMessage {
                Value = 1
            }, requestHeaders);

            var actualResponseHeaders = await call.ResponseHeadersAsync;
            await call.ResponseAsync;
            var actualResponseTrailers = call.GetTrailers();
            var actualStatus           = call.GetStatus();
            var actualRequestHeaders   = ctx.Impl.RequestHeaders;

            AssertHasMetadata(requestHeaders, actualRequestHeaders);
            AssertHasMetadata(responseHeaders, actualResponseHeaders);
            AssertHasMetadata(responseTrailers, actualResponseTrailers);
            Assert.Equal(StatusCode.OK, actualStatus.StatusCode);
        }