Files
swift-nio-http2/Tests/NIOHTTP2Tests/StreamChannelFlowControllerTests.swift
T
Cory Benfield 492a77b186 Support child channel writability management. (#142)
Motivation:

It's important to ensure that child channel users can observe the
writability state of a child channel in order to avoid ballooning data
in memory. Up until now we haven't supported writability notifications,
but we really should.

Modifications:

- Added a flow controller to manage writability.
- Managed data flow through promises.
- Lots of tests.

Result:

Child channels will provide a much better signal as to whether they are
writable.
2019-07-19 16:49:22 +01:00

89 lines
3.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 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
@testable import NIOHTTP2
final class StreamChannelFlowControllerTests: XCTestCase {
func testChannelWritabilityChangesFromFlowControl() {
var controller = StreamChannelFlowController(highWatermark: 10, lowWatermark: 5, parentIsWritable: true)
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.bufferedBytes(10), .noChange)
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.bufferedBytes(1), .changed(newValue: false))
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.bufferedBytes(10), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.wroteBytes(16), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.wroteBytes(1), .changed(newValue: true))
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.wroteBytes(4), .noChange)
XCTAssertTrue(controller.isWritable)
}
func testChannelWritabilityChangesFromParent() {
var controller = StreamChannelFlowController(highWatermark: 10, lowWatermark: 5, parentIsWritable: true)
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(true), .noChange)
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(false), .changed(newValue: false))
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(false), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(true), .changed(newValue: true))
XCTAssertTrue(controller.isWritable)
}
func testChannelWritabilityConsidersBothFlowControlAndParent() {
var controller = StreamChannelFlowController(highWatermark: 10, lowWatermark: 5, parentIsWritable: true)
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.bufferedBytes(15), .changed(newValue: false))
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(false), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.wroteBytes(15), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(true), .changed(newValue: true))
XCTAssertTrue(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(false), .changed(newValue: false))
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.bufferedBytes(15), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.parentWritabilityChanged(true), .noChange)
XCTAssertFalse(controller.isWritable)
XCTAssertEqual(controller.wroteBytes(15), .changed(newValue: true))
XCTAssertTrue(controller.isWritable)
}
}