Example #1
0
        public async Task Pipelining_Success()
        {
            const int PipelineLength = 10;

            await RunMultiStreamTest(
                async (client, serverUri) =>
            {
                var tasks = new Task[PipelineLength];

                for (int i = 0; i < tasks.Length; ++i)
                {
                    tasks[i] = MakeRequest(i);
                }

                await tasks.WhenAllOrAnyFailed(10_000);

                async Task MakeRequest(int requestNo)
                {
                    await using ValueHttpRequest request = (await client.CreateNewRequestAsync(Version, HttpVersionPolicy.RequestVersionExact)).Value;

                    request.ConfigureRequest(contentLength: 0, hasTrailingHeaders: false);
                    request.WriteRequest(HttpMethod.Get, serverUri);
                    request.WriteHeader("X-Request-No", requestNo.ToString(CultureInfo.InvariantCulture));
                    await request.CompleteRequestAsync();

                    TestHeadersSink headers = await request.ReadAllHeadersAsync();

                    Assert.Equal(requestNo.ToString(CultureInfo.InvariantCulture), headers.GetSingleValue("X-Response-No"));
                }
            },
                async server =>
            {
                var streams = new (HttpTestStream, HttpTestFullRequest)[PipelineLength];

                for (int i = 0; i < streams.Length; ++i)
                {
                    HttpTestStream stream       = await server.AcceptStreamAsync();
                    HttpTestFullRequest request = await stream.ReceiveFullRequestAsync();
                    Assert.Equal(i.ToString(CultureInfo.InvariantCulture), request.Headers.GetSingleValue("X-Request-No"));
                    streams[i] = (stream, request);
                }

                for (int i = 0; i < streams.Length; ++i)
                {
                    (HttpTestStream stream, HttpTestFullRequest request) = streams[i];

                    var responseHeaders = new TestHeadersSink()
                    {
                        { "X-Response-No", i.ToString(CultureInfo.InvariantCulture) }
                    };

                    await stream.SendResponseAsync(headers: responseHeaders);
                    await stream.DisposeAsync();
                }
            });