コード例 #1
0
        public async Task SayHelloServerStreamingTest()
        {
            // Arrange
            var service = new TesterService(NullLoggerFactory.Instance);

            var cts            = new CancellationTokenSource();
            var callContext    = TestServerCallContext.Create(cancellationToken: cts.Token);
            var responseStream = new TestServerStreamWriter <HelloReply>(callContext);

            // Act
            var call = service.SayHelloServerStreaming(new HelloRequest {
                Name = "Joe"
            }, responseStream, callContext);

            // Assert
            Assert.IsFalse(call.IsCompletedSuccessfully, "Method should run until cancelled.");

            cts.Cancel();

            await call;

            responseStream.Complete();

            var allMessages = new List <HelloReply>();

            await foreach (var message in responseStream.ReadAllAsync())
            {
                allMessages.Add(message);
            }

            Assert.GreaterOrEqual(allMessages.Count, 1);

            Assert.AreEqual("How are you Joe? 1", allMessages[0].Message);
        }
コード例 #2
0
        public async Task SayHelloServerStreamingTest()
        {
            // Arrange
            var mockGreeter = new Mock <IGreeter>();

            mockGreeter.Setup(m => m.Greet(It.IsAny <string>())).Returns((string s) => $"Hello {s}");
            var service = new TesterService(mockGreeter.Object);

            var cts            = new CancellationTokenSource();
            var callContext    = TestServerCallContext.Create(cancellationToken: cts.Token);
            var responseStream = new TestServerStreamWriter <HelloReply>(callContext);

            // Act
            using var call = service.SayHelloServerStreaming(new HelloRequest { Name = "Joe" }, responseStream, callContext);

            // Assert
            Assert.False(call.IsCompletedSuccessfully, "Method should run until cancelled.");

            cts.Cancel();

            await call;

            responseStream.Complete();

            var allMessages = new List <HelloReply>();

            await foreach (var message in responseStream.ReadAllAsync())
            {
                allMessages.Add(message);
            }

            Assert.True(allMessages.Count >= 1);

            Assert.Equal("Hello Joe 1", allMessages[0].Message);
        }