public async Task ReceivingHeadersShouldCreateNewStreamsAndAllowToReadTheHeaders()
        {
            const int nrStreams = 10;
            var inPipe = new BufferedPipe(1024);
            var outPipe = new BufferedPipe(1024);
            int nrAcceptedStreams = 0;
            var handlerDone = new SemaphoreSlim(0);
            uint streamId = 1;
            var headersOk = false;
            var streamIdOk = false;
            var streamStateOk = false;

            Func<IStream, bool> listener = (s) =>
            {
                Interlocked.Increment(ref nrAcceptedStreams);
                Task.Run(async () =>
                {
                    var rcvdHeaders = await s.ReadHeadersAsync();
                    headersOk = DefaultGetHeaders.SequenceEqual(rcvdHeaders);
                    streamIdOk = s.Id == streamId;
                    streamStateOk = s.State == StreamState.Open;
                    handlerDone.Release();
                });
                return true;
            };
            var http2Con = await ConnectionUtils.BuildEstablishedConnection(
                true, inPipe, outPipe, loggerProvider, listener);

            var hEncoder = new Encoder();
            for (var i = 0; i < nrStreams; i++)
            {
                headersOk = false;
                streamIdOk = false;
                streamStateOk = false;
                await inPipe.WriteHeaders(hEncoder, streamId, false, DefaultGetHeaders);
                var requestDone = await handlerDone.WaitAsync(ReadableStreamTestExtensions.ReadTimeout);
                Assert.True(requestDone, "Expected handler to complete within timeout");
                Assert.True(headersOk);
                Assert.True(streamIdOk);
                Assert.True(streamStateOk);
                streamId += 2;
            }
            Assert.Equal(nrStreams, nrAcceptedStreams);
        }
Beispiel #2
0
        public async Task ReceivingHeadersWithPaddingAndPriorityShouldBeSupported(
            int?numPadding, bool hasPrio)
        {
            const int nrStreams         = 10;
            var       inPipe            = new BufferedPipe(1024);
            var       outPipe           = new BufferedPipe(1024);
            int       nrAcceptedStreams = 0;
            var       handlerDone       = new SemaphoreSlim(0);
            uint      streamId          = 1;
            var       headersOk         = false;
            var       streamIdOk        = false;
            var       streamStateOk     = false;

            Func <IStream, bool> listener = (s) =>
            {
                Interlocked.Increment(ref nrAcceptedStreams);
                Task.Run(async() =>
                {
                    var rcvdHeaders = await s.ReadHeadersAsync();
                    headersOk       = DefaultGetHeaders.SequenceEqual(rcvdHeaders);
                    streamIdOk      = s.Id == streamId;
                    streamStateOk   = s.State == StreamState.Open;
                    handlerDone.Release();
                });
                return(true);
            };
            var http2Con = await ConnectionUtils.BuildEstablishedConnection(
                true, inPipe, outPipe, loggerProvider, listener);

            var hEncoder = new Encoder();
            var outBuf   = new byte[Settings.Default.MaxFrameSize];

            for (var i = 0; i < nrStreams; i++)
            {
                headersOk     = false;
                streamIdOk    = false;
                streamStateOk = false;

                var headerOffset = 0;
                if (numPadding != null)
                {
                    outBuf[0]     = (byte)numPadding;
                    headerOffset += 1;
                }
                if (hasPrio)
                {
                    // TODO: Initialize the priority data in this test properly
                    // if priority data checking gets inserted later on
                    headerOffset += 5;
                }
                var result = hEncoder.EncodeInto(
                    new ArraySegment <byte>(outBuf, headerOffset, outBuf.Length - headerOffset),
                    DefaultGetHeaders);
                var totalLength = headerOffset + result.UsedBytes;
                if (numPadding != null)
                {
                    totalLength += numPadding.Value;
                }

                var flags = (byte)HeadersFrameFlags.EndOfHeaders;
                if (numPadding != null)
                {
                    flags |= (byte)HeadersFrameFlags.Padded;
                }
                if (hasPrio)
                {
                    flags |= (byte)HeadersFrameFlags.Priority;
                }

                var fh = new FrameHeader
                {
                    Type     = FrameType.Headers,
                    Length   = totalLength,
                    Flags    = (byte)flags,
                    StreamId = streamId,
                };
                await inPipe.WriteFrameHeader(fh);

                await inPipe.WriteAsync(new ArraySegment <byte>(outBuf, 0, totalLength));

                var requestDone = await handlerDone.WaitAsync(ReadableStreamTestExtensions.ReadTimeout);

                Assert.True(requestDone, "Expected handler to complete within timeout");
                Assert.True(headersOk);
                Assert.True(streamIdOk);
                Assert.True(streamStateOk);
                streamId += 2;
            }
            Assert.Equal(nrStreams, nrAcceptedStreams);
        }