Exemple #1
0
        public async Task SendValidRequest_StreamedContentReturned()
        {
            // Arrage
            var httpClient = CreateGrpcWebClient();
            var channel    = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
            {
                HttpClient    = httpClient,
                LoggerFactory = LoggerFactory
            });

            var client = new EchoService.EchoServiceClient(channel);

            // Act
            var call = client.ServerStreamingEcho(new ServerStreamingEchoRequest
            {
                Message         = "test",
                MessageCount    = 3,
                MessageInterval = TimeSpan.FromMilliseconds(10).ToDuration()
            });

            // Assert
            Assert.IsTrue(await call.ResponseStream.MoveNext(CancellationToken.None).DefaultTimeout());
            Assert.AreEqual("test", call.ResponseStream.Current.Message);

            Assert.IsTrue(await call.ResponseStream.MoveNext(CancellationToken.None).DefaultTimeout());
            Assert.AreEqual("test", call.ResponseStream.Current.Message);

            Assert.IsTrue(await call.ResponseStream.MoveNext(CancellationToken.None).DefaultTimeout());
            Assert.AreEqual("test", call.ResponseStream.Current.Message);

            Assert.IsFalse(await call.ResponseStream.MoveNext(CancellationToken.None).DefaultTimeout());
            Assert.AreEqual(null, call.ResponseStream.Current);

            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
        }
Exemple #2
0
        public async Task SendValidRequest_ClientAbort_ClientThrowsCancelledException()
        {
            // Arrage
            SetExpectedErrorsFilter(r =>
            {
                if (r.EventId.Name == "GrpcStatusError" &&
                    r.Message == "Call failed with gRPC error status. Status code: 'Cancelled', Message: 'Call canceled by the client.'.")
                {
                    return(true);
                }

                return(false);
            });

            var httpClient = CreateGrpcWebClient();
            var channel    = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
            {
                HttpClient    = httpClient,
                LoggerFactory = LoggerFactory
            });

            var cts    = new CancellationTokenSource();
            var client = new EchoService.EchoServiceClient(channel);

            // Act
            var call = client.ServerStreamingEcho(new ServerStreamingEchoRequest
            {
                Message         = "test",
                MessageCount    = 2,
                MessageInterval = TimeSpan.FromMilliseconds(100).ToDuration()
            }, cancellationToken: cts.Token);

            // Assert
            Assert.IsTrue(await call.ResponseStream.MoveNext(CancellationToken.None).DefaultTimeout());
            Assert.AreEqual("test", call.ResponseStream.Current.Message);

            cts.Cancel();

            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => call.ResponseStream.MoveNext(CancellationToken.None));

            Assert.AreEqual(StatusCode.Cancelled, ex.StatusCode);
            Assert.AreEqual("Call canceled by the client.", ex.Status.Detail);

            Assert.AreEqual(StatusCode.Cancelled, call.GetStatus().StatusCode);
        }
Exemple #3
0
        private async Task GrpcWebServerStreamingAsync(EchoService.EchoServiceClient client)
        {
            var call = client.ServerStreamingEcho(new ServerStreamingEchoRequest
            {
                Message         = "test",
                MessageCount    = 5,
                MessageInterval = Google.Protobuf.WellKnownTypes.TimeExtensions.ToDuration(TimeSpan.FromMilliseconds(100))
            });

            var messages = new List <ServerStreamingEchoResponse>();

            await foreach (var message in call.ResponseStream.ReadAllAsync())
            {
                messages.Add(message);
            }

            Assert.AreEqual(5, messages.Count);
        }
        public async Task SendValidRequest_ClientAbort_ClientThrowsCancelledException()
        {
            // Arrage
            var httpClient = CreateGrpcWebClient();
            var channel    = GrpcChannel.ForAddress(httpClient.BaseAddress !, new GrpcChannelOptions
            {
                HttpClient    = httpClient,
                LoggerFactory = LoggerFactory
            });

            var cts    = new CancellationTokenSource();
            var client = new EchoService.EchoServiceClient(channel);

            // Act
            var call = client.ServerStreamingEcho(new ServerStreamingEchoRequest
            {
                Message         = "test",
                MessageCount    = 2,
                MessageInterval = TimeSpan.FromMilliseconds(100).ToDuration()
            }, cancellationToken: cts.Token);

            // Assert
            Assert.IsTrue(await call.ResponseStream.MoveNext(CancellationToken.None).DefaultTimeout());
            Assert.AreEqual("test", call.ResponseStream.Current.Message);

            cts.Cancel();

            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => call.ResponseStream.MoveNext(CancellationToken.None)).DefaultTimeout();

            Assert.AreEqual(StatusCode.Cancelled, ex.StatusCode);
            Assert.AreEqual("Call canceled by the client.", ex.Status.Detail);

            Assert.AreEqual(StatusCode.Cancelled, call.GetStatus().StatusCode);

            // It is possible get into a situation where the response stream finishes slightly before the call.
            // Small delay to ensure call logging is complete.
            await Task.Delay(50);

            AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Cancelled', Message: 'Call canceled by the client.'.");
        }