Files
swift-nio-http2/Tests/NIOHTTP2Tests/HTTP2ConcurrentStreamsHandlerTest.swift
T
Cory Benfield 3c90345c36 Pure-Swift HTTP/2 implementation (#40)
* Updated to use SwiftNIO 1.10+ to gain access to HTTPHeaders internals.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Initial basic parser code.

* Adding unit tests.

* Add NIOHTTP2FlowControlHandler

Motivation:

One of the things that nghttp2 used to do for us was to handle buffering
DATA frames until flow control allowed them to go out. For users that
want that behaviour, we need to replace the nghttp2 code with a new
ChannelHandler that can do this work.

Modifications:

- Added NIOHTTP2FlowControlHandler.

Result:

Users will have the same feature set as they did with nghttp2.

* Generate Linux test information.

* Documentation comment.

* Updated new tests from master to use new payload types.

* Updated following review.

Notable changes:

- Implemented unit tests for the ALTSVC (0xa) and ORIGIN (0xc) frame types.
- Additional DATA frames are synthesized to wrap individual payload chunks as they arrive. If the payload of a DATA frame with 4MB data arrives in 4 separate 1MB chunks, the decoder will emit 4 DATA frames, with only the last containing the END_STREAM flag (if that was set on the incoming frame).
- HEADERS / PUSH_PROMISE frames and any following CONTINUATION frames are now handled properly: header fragments from all frames are gathered up into a single buffer. Once a frame with END_HEADERS is encountered, the resultant accumulated buffer is sent for decoding along with the flags and stream ID of the original HEADERS / PUSH_PROMISE frame.
- HPACK encoders/decoders now live in the frame encoder/decoder types, rather than being attached to a stream.

* Updated Linux tests.

* Headers placed in relevant files.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Revamped frame decoder state machine and API. Now uses separate input and output methods to manage multiple frames in a single buffer.

* Updated Linux tests.

* Bumped swift-nio version to 1.12.0 and updated `HTTPHeaders` -> `HPACKHeaders` conversion initializer to directly copy the buffer and map the header name/value indices.

* Set up HTTP2Parser in handlerAdded if the channel had been previously activated. (#36)

By setting up the handler in the handlerAdded hook, HTTP2Parser supports
being added dynamically in a channelRead event, after the channel has sent
the channelActive event.

(cherry picked from commit 88bd6d369a)

* Minor tweaks to get the tests passing

* Initial integration here

* Minor clearing up of the handler

* Get the tests compiling

* Fix header issues.

We now pass the frame parser to the various setting operations to let it
update the state. We also fixed some issues with specific settings.

* Remove ngthttp2-based impl, clean up stream ID

* Tests with new HTTP2StreamID

* Simple test cleanup

* Get remaining non-core tests passing

* Allow GOAWAY even when fully quiesced

* We don't buffer outbound frames any more

* Emit StreamClosed events on GoAway frame

* Notify stream creation and closure

* This test started failing

* Goodbye, C code! 🎉

* Send PING ACK automatically

* Updated based on PR feedback.

* Correctly handle 1XX responses

* Add SETTINGS_MAX_FRAME_SIZE support to flow controller

* Correctly account for flow control

* Define ConcurrentStreamsHandler

* Prepare for signalling connection state changes

* Better state change message propagation

* Refactor to use result with effect everywhere

* Don't lose flow control info on stream error

* Add basic flow control management to stream multiplexer

* Better pipeline in sample server

* Clean up some reentrancy concerns

* Move to Swift 5 and NIO 2

* Move to new Swift 5 for CI

* Allow wider range of choices of years

* Catch up to NIO 2 again
2019-02-21 11:52:49 +00:00

434 lines
22 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
import NIOHPACK
import NIOHTTP2
/// A ChannelHandler that has a stripped down version of the HTTP/2 state machine.
final class OutboundFrameRecorder: ChannelOutboundHandler {
typealias OutboundIn = HTTP2Frame
typealias OutboundOut = Never
private var _pendingWrites: [(HTTP2Frame, EventLoopPromise<Void>?)] = []
var pendingWrites: [HTTP2Frame] {
return self._pendingWrites.map { $0.0 }
}
var flushedWrites: [HTTP2Frame] = []
private var openStreams: Set<HTTP2StreamID> = []
func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let frame = self.unwrapOutboundIn(data)
self._pendingWrites.append((frame, promise))
if !self.openStreams.contains(frame.streamID) {
self.openStreams.insert(frame.streamID)
ctx.fireUserInboundEventTriggered(NIOHTTP2StreamCreatedEvent(streamID: frame.streamID, localInitialWindowSize: 65535, remoteInitialWindowSize: 65535))
}
if frame.flags.contains(.endStream) {
self.openStreams.remove(frame.streamID)
ctx.fireUserInboundEventTriggered(StreamClosedEvent(streamID: frame.streamID, reason: nil))
}
}
func flush(ctx: ChannelHandlerContext) {
let flushedWrites = self._pendingWrites
self._pendingWrites = []
for (frame, promise) in flushedWrites {
self.flushedWrites.append(frame)
promise?.succeed(())
}
}
}
final class CloseOnWriteForStreamHandler: ChannelOutboundHandler {
typealias OutboundIn = HTTP2Frame
typealias OutboundOut = Never
private let reason: HTTP2ErrorCode?
private let streamToFail: HTTP2StreamID
init(streamToFail: HTTP2StreamID, reason: HTTP2ErrorCode?) {
self.streamToFail = streamToFail
self.reason = reason
}
func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let frame = self.unwrapOutboundIn(data)
if frame.streamID == self.streamToFail {
ctx.fireUserInboundEventTriggered(StreamClosedEvent(streamID: self.streamToFail, reason: self.reason))
}
ctx.write(data, promise: promise)
}
}
final class HTTP2ConcurrentStreamsHandlerTests: XCTestCase {
var channel: EmbeddedChannel!
override func setUp() {
self.channel = EmbeddedChannel()
}
override func tearDown() {
self.channel = nil
}
func testBasicFunctionality() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 2)).wait())
// Write frames for three streams.
let frames = stride(from: 1, through: 5, by: 2).map { HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(HPACKHeaders([]), nil)) }
for frame in frames {
self.channel.write(frame, promise: nil)
}
// Only two of these should have been passed through, and neither should have been flushed.
frameRecorder.pendingWrites.assertFramesMatch(frames.prefix(2))
XCTAssertEqual(frameRecorder.flushedWrites.count, 0)
// Flushing these should not produce another frame.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2))
// Sending a frame with end stream on it should not produce a write yet, though that frame should be passed through.
var endStreamFrame = HTTP2Frame(streamID: 1, payload: .data(.byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
endStreamFrame.flags.insert(.endStream)
self.channel.write(endStreamFrame, promise: nil)
frameRecorder.pendingWrites.assertFramesMatch([endStreamFrame])
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2))
// Now we flush. This should cause the remaining HEADERS frame to be written.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(Array(frames.prefix(2)) + [endStreamFrame, frames.last!])
}
func testToleratesNegativeNumbersOfStreams() throws {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 2)).wait())
// Write frames for three streams and flush them.
let frames = stride(from: 1, through: 5, by: 2).map { HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(HPACKHeaders([]), nil)) }
for frame in frames {
self.channel.write(frame, promise: nil)
}
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2))
// Now we're going to drop the number of allowed concurrent streams to 1.
let settings = HTTP2Frame(streamID: .rootStream, payload: .settings([HTTP2Setting(parameter: .maxConcurrentStreams, value: 3), HTTP2Setting(parameter: .maxConcurrentStreams, value: 1)]))
try self.channel.writeInbound(settings)
// No change.
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2))
// Write a new stream. This should be buffered.
var newStreamFrame = HTTP2Frame(streamID: 7, payload: .headers(HPACKHeaders([]), nil))
newStreamFrame.flags.insert(.endStream)
self.channel.writeAndFlush(newStreamFrame, promise: nil)
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2))
// Now send an END_STREAM frame. Nothing should happen, though the frame itself should pass through.
var endStreamFrame = HTTP2Frame(streamID: 1, payload: .data(.byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
endStreamFrame.flags.insert(.endStream)
self.channel.write(endStreamFrame, promise: nil)
frameRecorder.pendingWrites.assertFramesMatch([endStreamFrame])
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2))
// And flush it. Nothing should happen, as only one stream is allowed through.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2) + [endStreamFrame])
// Now send endStream for the other one. Initially nothing happens.
var newEndStreamFrame = endStreamFrame
newEndStreamFrame.streamID = 3
self.channel.write(newEndStreamFrame, promise: nil)
frameRecorder.pendingWrites.assertFramesMatch([newEndStreamFrame])
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2) + [endStreamFrame])
// Flush it. This *does* unblock the next stream, but only one.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch(frames.prefix(2) + [endStreamFrame, newEndStreamFrame, frames.last!])
}
func testCascadingFrames() throws {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 1)).wait())
// We're going to test a cascade of frames as a result of stream closure. This is not likely to happen in real code,
// but it's a good test to confirm that we're safe in re-entrant calls. To do this, we set up, let's say, 100
// streams where all but the first has an END_STREAM frame in it.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(HPACKHeaders([]), nil))
let subsequentFrames: [HTTP2Frame] = stride(from: 3, to: 201, by: 2).map { streamID in
var frame = HTTP2Frame(streamID: HTTP2StreamID(streamID), payload: .headers(HPACKHeaders([]), nil))
frame.flags.insert(.endStream)
return frame
}
// Write the first frame, and all the subsequent frames, and flush them. This will lead to one flushed frame.
for frame in [firstFrame] + subsequentFrames {
self.channel.write(frame, promise: nil)
}
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
// Ok, now we're going to tell the buffer that the first stream is closed. Initially, nothing will happen.
self.channel.pipeline.fireUserInboundEventTriggered(StreamClosedEvent(streamID: 1, reason: nil))
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
// Now send channelReadComplete, which will trigger the cascade. This should flush *everything*.
self.channel.pipeline.fireChannelReadComplete()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame] + subsequentFrames)
}
func testBufferedFramesPassedThroughOnStreamClosed() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: CloseOnWriteForStreamHandler(streamToFail: 3, reason: nil)).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 1)).wait())
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3. We'll flush some, but not all of them
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(HPACKHeaders([]), nil))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.byteBuffer(self.channel.allocator.buffer(capacity: 0))))
self.channel.write(firstFrame, promise: nil)
(0..<15).forEach { _ in self.channel.write(subsequentFrame, promise: nil) }
self.channel.flush()
(0..<15).forEach { _ in self.channel.write(subsequentFrame, promise: nil) }
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
// Ok, now we're going to unblock stream 3 by completing stream 1.
// This will cause an stream closed to fire, but that will not affect these writes.
self.channel.pipeline.fireUserInboundEventTriggered(StreamClosedEvent(streamID: 1, reason: nil))
self.channel.pipeline.fireChannelReadComplete()
// The flushed writes should all be passed through.
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame] + Array(repeating: subsequentFrame, count: 15))
// A subsequent flush will drive the rest of the frames through.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame] + Array(repeating: subsequentFrame, count: 30))
}
func testResetStreamOnUnbufferingStream() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 1)).wait())
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3. We'll flush some, but not all of them
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(HPACKHeaders([]), nil))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.byteBuffer(self.channel.allocator.buffer(capacity: 0))))
self.channel.write(firstFrame, promise: nil)
var writeStatus: [Bool?] = Array(repeating: nil, count: 30)
var writePromises = (0..<15).map { _ in self.channel.write(subsequentFrame) }
self.channel.flush()
writePromises.append(contentsOf: (0..<15).map { _ in self.channel.write(subsequentFrame) })
for (idx, promise) in writePromises.enumerated() {
promise.map {
writeStatus[idx] = true
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.StreamClosed(streamID: 3, errorCode: .cancel))
writeStatus[idx] = false
}
}
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
// Now we're going to unblock stream 3 by completing stream 1. This will leave our buffer still in place, as we can't
// pass on the unflushed writes.
self.channel.pipeline.fireUserInboundEventTriggered(StreamClosedEvent(streamID: 1, reason: nil))
self.channel.pipeline.fireChannelReadComplete()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame] + Array(repeating: subsequentFrame, count: 15))
// Only the first 15 are done.
XCTAssertEqual(writeStatus, Array(repeating: true as Bool?, count: 15) + Array(repeating: nil as Bool?, count: 15))
// Now we're going to send RST_STREAM. The RST_STREAM frame should immediately pass through, and all buffered writes should be failed.
let rstStreamFrame = HTTP2Frame(streamID: 3, payload: .rstStream(.cancel))
self.channel.write(rstStreamFrame, promise: nil)
frameRecorder.pendingWrites.assertFramesMatch([rstStreamFrame])
frameRecorder.flushedWrites.assertFramesMatch([firstFrame] + Array(repeating: subsequentFrame, count: 15))
XCTAssertEqual(writeStatus, Array(repeating: true as Bool?, count: 15) + Array(repeating: false as Bool?, count: 15))
// Now we'll flush it. The RST_STREAM frame will be emitted in line with the others.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame] + Array(repeating: subsequentFrame, count: 15) + [rstStreamFrame])
}
func testResetStreamOnBufferingStream() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 1)).wait())
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3. We won't flush stream 3.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(HPACKHeaders([]), nil))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.byteBuffer(self.channel.allocator.buffer(capacity: 0))))
self.channel.writeAndFlush(firstFrame, promise: nil)
var writeStatus: [Bool?] = Array(repeating: nil, count: 15)
let writePromises = (0..<15).map { _ in self.channel.write(subsequentFrame) }
for (idx, promise) in writePromises.enumerated() {
promise.map {
writeStatus[idx] = true
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.StreamClosed(streamID: 3, errorCode: .cancel))
writeStatus[idx] = false
}
}
// No new writes other than the first frame.
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
XCTAssertEqual(writeStatus, Array(repeating: nil as Bool?, count: 15))
// Now we're going to send RST_STREAM on 3. All writes should fail, *excluding* the RST_STREAM one, which succeeds immediately.
// No frame is sent through.
let rstStreamFrame = HTTP2Frame(streamID: 3, payload: .rstStream(.cancel))
let rstStreamFuture = self.channel.write(rstStreamFrame)
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
XCTAssertEqual(writeStatus, Array(repeating: false as Bool?, count: 15))
XCTAssertNoThrow(try rstStreamFuture.wait())
// Flushing changes nothing.
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([firstFrame])
}
func testGoingBackwardsInStreamIDIsNotAllowed() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 1)).wait())
// We're going to create stream 1 and stream 11. Stream 1 will be passed through, stream 11 will have to wait.
let oneFrame = HTTP2Frame(streamID: 1, payload: .headers(HPACKHeaders([]), nil))
let elevenFrame = HTTP2Frame(streamID: 11, payload: .headers(HPACKHeaders([]), nil))
self.channel.write(oneFrame, promise: nil)
self.channel.write(elevenFrame, promise: nil)
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([oneFrame])
// Now we're going to try to write a frame for stream 5. This will fail.
let fiveFrame = HTTP2Frame(streamID: 5, payload: .headers(HPACKHeaders([]), nil))
do {
try self.channel.write(fiveFrame).wait()
XCTFail("Did not throw")
} catch is NIOHTTP2Errors.StreamIDTooSmall {
// Ok
} catch {
XCTFail("Unexpected error: \(error)")
}
}
func testFramesForNonLocalStreamIDsAreIgnoredClient() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .client, initialMaxOutboundStreams: 1)).wait())
// We're going to create stream 1 and stream 3, which will be buffered.
let oneFrame = HTTP2Frame(streamID: 1, payload: .headers(HPACKHeaders([]), nil))
let threeFrame = HTTP2Frame(streamID: 3, payload: .headers(HPACKHeaders([]), nil))
self.channel.write(oneFrame, promise: nil)
self.channel.write(threeFrame, promise: nil)
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([oneFrame])
// Now we're going to try to write a frame for stream 4, a server-initiated stream. This will be passed through safely.
let fourFrame = HTTP2Frame(streamID: 4, payload: .headers(HPACKHeaders([]), nil))
self.channel.writeAndFlush(fourFrame, promise: nil)
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([oneFrame, fourFrame])
}
func testFramesForNonLocalStreamIDsAreIgnoredServer() {
let frameRecorder = OutboundFrameRecorder()
XCTAssertNoThrow(try self.channel.pipeline.add(handler: frameRecorder).wait())
XCTAssertNoThrow(try self.channel.pipeline.add(handler: NIOHTTP2ConcurrentStreamsHandler(mode: .server, initialMaxOutboundStreams: 1)).wait())
// We're going to create stream 2 and stream 4. First, however, we reserve them. These are not buffered, per RFC 7540 § 5.1.2:
//
// > Streams in either of the "reserved" states do not count toward the stream limit.
let twoFramePromise = HTTP2Frame(streamID: 1, payload: .pushPromise(2, HPACKHeaders([])))
let fourFramePromise = HTTP2Frame(streamID: 1, payload: .pushPromise(4, HPACKHeaders([])))
self.channel.write(twoFramePromise, promise: nil)
self.channel.write(fourFramePromise, promise: nil)
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([twoFramePromise, fourFramePromise])
// Now we're going to try to initiate these by sending HEADERS frames. This will buffer the second one.
let twoFrame = HTTP2Frame(streamID: 2, payload: .headers(HPACKHeaders([]), nil))
let fourFrame = HTTP2Frame(streamID: 4, payload: .headers(HPACKHeaders([]), nil))
self.channel.write(twoFrame, promise: nil)
self.channel.write(fourFrame, promise: nil)
self.channel.flush()
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([twoFramePromise, fourFramePromise, twoFrame])
// Now we're going to try to write a frame for stream 3, a client-initiated stream. This will be passed through safely.
let threeFrame = HTTP2Frame(streamID: 3, payload: .headers(HPACKHeaders([]), nil))
self.channel.writeAndFlush(threeFrame, promise: nil)
XCTAssertEqual(frameRecorder.pendingWrites.count, 0)
frameRecorder.flushedWrites.assertFramesMatch([twoFramePromise, fourFramePromise, twoFrame, threeFrame])
}
}