Ejemplo n.º 1
0
        public async Task AccumulateCount_MultipleMessagesThenClose_SuccessResponse()
        {
            // Arrange
            var ms = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(ms, new CounterRequest
            {
                Count = 1
            }).DefaultTimeout();

            var requestStream = new AwaitableMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Count.Counter/AccumulateCount");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming");

            Fixture.Signal.Reset();
            requestStream.SendData(ms.ToArray());
            await Fixture.Signal.Task.DefaultTimeout();

            Fixture.Signal.Reset();
            requestStream.SendData(ms.ToArray());
            await Fixture.Signal.Task.DefaultTimeout();

            Fixture.Signal.Reset();
            requestStream.SendData(Array.Empty <byte>());
            await Fixture.Signal.Task.DefaultTimeout();

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout();

            var replyTask = MessageHelpers.AssertReadMessageAsync <CounterReply>(responseStream);

            Assert.IsTrue(replyTask.IsCompleted);
            var reply = await replyTask.DefaultTimeout();

            Assert.AreEqual(2, reply.Count);
        }
        public async Task Chat_MultipleMessagesFromOneClient_SuccessResponses()
        {
            // Arrange
            var ms = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(ms, new ChatMessage
            {
                Name    = "John",
                Message = "Hello Jill"
            }).DefaultTimeout();

            var requestStream = new AwaitableMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Chat.Chatter/Chat");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for first message from client");
            requestStream.SendData(ms.ToArray());

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout();

            var message1Task = MessageHelpers.AssertReadMessageAsync <ChatMessage>(responseStream);

            Assert.IsTrue(message1Task.IsCompleted);
            var message1 = await message1Task.DefaultTimeout();

            Assert.AreEqual("John", message1.Name);
            Assert.AreEqual("Hello Jill", message1.Message);

            var message2Task = MessageHelpers.AssertReadMessageAsync <ChatMessage>(responseStream);

            Assert.IsFalse(message2Task.IsCompleted, "Server is waiting for messages from client");

            ms = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(ms, new ChatMessage
            {
                Name    = "Jill",
                Message = "Hello John"
            }).DefaultTimeout();

            requestStream.SendData(ms.ToArray());
            var message2 = await message2Task.DefaultTimeout();

            Assert.AreEqual("Jill", message2.Name);
            Assert.AreEqual("Hello John", message2.Message);

            var finishedTask = MessageHelpers.AssertReadMessageAsync <ChatMessage>(responseStream);

            Assert.IsFalse(finishedTask.IsCompleted, "Server is waiting for client to end streaming");

            requestStream.SendData(Array.Empty <byte>());
            await finishedTask.DefaultTimeout();
        }