Files
swift-nio-http2/Tests/NIOHTTP2Tests/InboundWindowManagerTests.swift
T
George Barnett 1e68e51752 Avoid emitting WINDOW_UPDATE frames when receiving stream closed (#233)
Motivation:

In some cases it's possible for a stream to emit a WINDOW_UPDATE frame
after receiving close from the network. This is unfortunate because in
the eyes of the `NIOHTTP2Handler` the stream no longer exists which
results in a connection-level error.

Modifications:

- Add a `close()` to the `InboundWindowManager`, no new frame sizes will
  be produced if close has been called
- Use the above functionality when the stream is closing and when we
  have seen end stream.

Result:

WINDOW_UPDATE frames are no longer emitted from the stream as a result
of delivering pending reads while closing.
2020-08-24 15:13:19 +01:00

86 lines
3.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
@testable import NIOHTTP2
import XCTest
class InboundWindowManagerTests: XCTestCase {
func testNewWindowSizeWhenNewSizeIsAtOrAboveTarget() {
var windowManager = InboundWindowManager(targetSize: 10)
XCTAssertNil(windowManager.newWindowSize(10))
XCTAssertNil(windowManager.newWindowSize(11))
}
func testNewWindowSizeWhenNewSizeIsAtLeastHalfTarget() {
var windowManager = InboundWindowManager(targetSize: 10)
XCTAssertNil(windowManager.newWindowSize(6))
XCTAssertNil(windowManager.newWindowSize(9))
}
func testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget() {
var windowManager = InboundWindowManager(targetSize: 10)
XCTAssertEqual(windowManager.newWindowSize(0), 10)
XCTAssertEqual(windowManager.newWindowSize(5), 5)
}
func testNewWindowSizeWithBufferedBytes() {
var windowManager = InboundWindowManager(targetSize: 10)
// The adjusted newSize is 3 (new size 2, buffered 1) which is less than half the target so
// we need to increment.
windowManager.bufferedFrameReceived(size: 1)
XCTAssertEqual(windowManager.newWindowSize(2), 7)
// The adjusted newSize is 6 (new size 2, buffered 1+3=4) which is more than half the target
// so no need to increment.
windowManager.bufferedFrameReceived(size: 3)
XCTAssertNil(windowManager.newWindowSize(2))
// The adjusted newSize is 10 (new size 2, buffered 4+4=8) which is equal to the target so
// no need to increment.
windowManager.bufferedFrameReceived(size: 4)
XCTAssertNil(windowManager.newWindowSize(2))
// The last window size was 2; we're emitting 2 of our 8 buffered bytes so now the adjusted
// size is 8 (last size 2, buffered 6); no need to increment.
XCTAssertNil(windowManager.bufferedFrameEmitted(size: 2))
// Emit another 5 bytes: we're down to 3 (last size 2, buffered 1); we need to increment.
XCTAssertEqual(windowManager.bufferedFrameEmitted(size: 5), 7)
}
func testInitialWindowSizeChanged() {
var windowManager = InboundWindowManager(targetSize: 10)
// There's no lastWindow size, so no increment.
XCTAssertNil(windowManager.initialWindowSizeChanged(delta: 10))
windowManager.bufferedFrameReceived(size: 3)
// adjusted size is 8 (new size 5, buffered 3) which is less than half the target (now 20).
XCTAssertEqual(windowManager.newWindowSize(5), 12)
}
func testWindowSizeWhenClosed() {
var windowManager = InboundWindowManager(targetSize: 10)
XCTAssertEqual(windowManager.newWindowSize(5), 5)
windowManager.closed = true
XCTAssertNil(windowManager.newWindowSize(5))
windowManager.bufferedFrameReceived(size: 5)
XCTAssertNil(windowManager.bufferedFrameEmitted(size: 5))
}
}