Files
swift-nio-http2/Tests/NIOHTTP2Tests/NIOHTTP2ServerConnectionManagementHandler+StateMachineTests.swift
T
aryan-25 b6571f3db4 Add a server connection manager (#532)
Motivation:

`NIOHTTP2Handler` currently does not support graceful shutdown because
it does not action upon a `ChannelShouldQuiesceEvent` event. See issue
https://github.com/apple/swift-nio-http2/issues/336.

Per the
[comment](https://github.com/apple/swift-nio-http2/issues/336#issuecomment-2252862397)
on that issue,
[`gRPC-swift-nio-transport`](https://github.com/grpc/grpc-swift-nio-transport)
implements this functionality in a separate channel handler, named
[`ServerConnectionManagementHandler`](https://github.com/grpc/grpc-swift-nio-transport/blob/main/Sources/GRPCNIOTransportCore/Server/Connection/ServerConnectionManagementHandler.swift).
We should introduce this to this repo too.

Modifications:

- Added `ServerConnectionManagementHandler` from
`gRPC-swift-nio-transport` with some slight modifications such as
removing gRPC-specific functionality like policing client pings.
- Added a new `configureAsyncHTTP2Pipeline` helper, which takes a
connection manager configuration as an argument and sets the
connection manager up alongside `NIOHTTP2Handler`.

Result:

Addresses issue https://github.com/apple/swift-nio-http2/issues/336.
2026-02-18 08:08:55 +00:00

178 lines
6.9 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//
// swift-format-ignore: NoBlockComments
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import NIOCore
import Testing
@testable import NIOHTTP2
struct NIOHTTP2ServerConnectionManagementHandlerStateMachineTests {
private func makeStateMachine(
goAwayPingData: HTTP2PingData = HTTP2PingData(withInteger: 42)
) -> NIOHTTP2ServerConnectionManagementHandler.StateMachine {
.init(goAwayPingData: goAwayPingData)
}
@Test("Closing last stream starts idle timer")
func testCloseAllStreamsWhenActive() {
var state = self.makeStateMachine()
state.streamOpened(1)
#expect(state.streamClosed(1) == .startIdleTimer)
}
@Test("Closing stream with others open returns no action")
func testCloseSomeStreamsWhenActive() {
var state = self.makeStateMachine()
state.streamOpened(1)
state.streamOpened(2)
#expect(state.streamClosed(2) == .none)
}
@Test("Stream operations after close are no-ops")
func testOpenAndCloseStreamWhenClosed() {
var state = self.makeStateMachine()
state.markClosed()
state.streamOpened(1)
#expect(state.streamClosed(1) == .none)
}
@Test("Graceful shutdown when no open streams")
func testGracefulShutdownWhenNoOpenStreams() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
#expect(state.startGracefulShutdown() == .sendGoAwayAndPing(pingData))
}
@Test("Repeated graceful shutdown is idempotent")
func testGracefulShutdownWhenClosing() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
#expect(state.startGracefulShutdown() == .sendGoAwayAndPing(pingData))
#expect(state.startGracefulShutdown() == .none)
}
@Test("Graceful shutdown after close is a no-op")
func testGracefulShutdownWhenClosed() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
state.markClosed()
#expect(state.startGracefulShutdown() == .none)
}
@Test("Stream opened just before sending first ping")
func testReceiveAckForGoAwayPingWhenStreamsOpenedBeforeShutdownOnly() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
state.streamOpened(1)
#expect(state.startGracefulShutdown() == .sendGoAwayAndPing(pingData))
#expect(state.receivedPingAck(data: pingData) == .sendGoAway(lastStreamID: 1, close: false))
}
@Test("Stream opened after sending first ping but before receiving ack")
func testReceiveAckForGoAwayPingWhenStreamsOpenedBeforeAck() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
#expect(state.startGracefulShutdown() == .sendGoAwayAndPing(pingData))
state.streamOpened(1)
#expect(state.receivedPingAck(data: pingData) == .sendGoAway(lastStreamID: 1, close: false))
}
@Test("Receiving ping ack with no streams sends final GOAWAY and closes")
func testReceiveAckForGoAwayPingWhenNoOpenStreams() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
#expect(state.startGracefulShutdown() == .sendGoAwayAndPing(pingData))
#expect(state.receivedPingAck(data: pingData) == .sendGoAway(lastStreamID: .rootStream, close: true))
}
@Test("Non-matching ping ack is ignored")
func testReceiveAckNotForGoAwayPing() {
let pingData = HTTP2PingData(withInteger: 42)
var state = self.makeStateMachine(goAwayPingData: pingData)
#expect(state.startGracefulShutdown() == .sendGoAwayAndPing(pingData))
let otherPingData = HTTP2PingData(withInteger: 0)
#expect(state.receivedPingAck(data: otherPingData) == .none)
}
@Test("Ping ack when not shutting down is ignored")
func testReceivePingAckWhenActive() {
var state = self.makeStateMachine()
#expect(state.receivedPingAck(data: HTTP2PingData()) == .none)
}
@Test("Ping ack after close is ignored")
func testReceivePingAckWhenClosed() {
var state = self.makeStateMachine()
state.markClosed()
#expect(state.receivedPingAck(data: HTTP2PingData()) == .none)
}
@Test("Full graceful shutdown waits for all streams to close")
func testGracefulShutdownFlow() {
var state = self.makeStateMachine()
// Open a few streams.
state.streamOpened(1)
state.streamOpened(2)
switch state.startGracefulShutdown() {
case .sendGoAwayAndPing(let pingData):
// Open another stream and then receive the ping ack.
state.streamOpened(3)
#expect(state.receivedPingAck(data: pingData) == .sendGoAway(lastStreamID: 3, close: false))
case .none:
Issue.record("Expected '.sendGoAwayAndPing'")
}
// Both GOAWAY frames have been sent. Start closing streams.
#expect(state.streamClosed(1) == .none)
#expect(state.streamClosed(2) == .none)
#expect(state.streamClosed(3) == .close)
}
@Test("Only close after receiving ping ack even when no streams are open")
func testGracefulShutdownWhenNoOpenStreamsBeforeSecondGoAway() {
var state = self.makeStateMachine()
// Open a stream.
state.streamOpened(1)
switch state.startGracefulShutdown() {
case .sendGoAwayAndPing(let pingData):
// Close the stream. This shouldn't lead to a close.
#expect(state.streamClosed(1) == .none)
// Only on receiving the ack do we send a GOAWAY and close.
#expect(state.receivedPingAck(data: pingData) == .sendGoAway(lastStreamID: 1, close: true))
case .none:
Issue.record("Expected '.sendGoAwayAndPing'")
}
}
}