diff --git a/Package.swift b/Package.swift index aeec7c8..574c420 100644 --- a/Package.swift +++ b/Package.swift @@ -83,7 +83,8 @@ let package = Package( .product(name: "NIOCore", package: "swift-nio"), .product(name: "NIOConcurrencyHelpers", package: "swift-nio"), .product(name: "NIOHTTP1", package: "swift-nio"), - ] + ], + swiftSettings: strictConcurrencySettings ), .testTarget( name: "NIOHTTP2Tests", diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineAsyncMultiplexerTests.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineAsyncMultiplexerTests.swift index 684e418..a211b3c 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineAsyncMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineAsyncMultiplexerTests.swift @@ -117,9 +117,13 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { ) let serverMultiplexer = try await assertNoThrowWithValue( - try await self.serverChannel.configureAsyncHTTP2Pipeline(mode: .server) { - channel -> EventLoopFuture in - channel.pipeline.addHandlers([OKResponder(), serverRecorder]).map { _ in channel } + try await self.serverChannel.configureAsyncHTTP2Pipeline(mode: .server) { channel in + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(OKResponder()) + try sync.addHandler(serverRecorder) + return channel + } }.get() ) @@ -142,9 +146,12 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { // client for i in 0.. EventLoopFuture in - channel.pipeline.addHandlers([SimpleRequest(), InboundFramePayloadRecorder()]).map { - channel + let streamChannel = try await clientMultiplexer.openStream { channel in + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(SimpleRequest()) + try sync.addHandler(InboundFramePayloadRecorder()) + return channel } } @@ -189,9 +196,13 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { ) let serverMultiplexer = try await assertNoThrowWithValue( - try await self.serverChannel.configureAsyncHTTP2Pipeline(mode: .server) { - channel -> EventLoopFuture in - channel.pipeline.addHandlers([OKResponder(), serverRecorder]).map { _ in channel } + try await self.serverChannel.configureAsyncHTTP2Pipeline(mode: .server) { channel in + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(OKResponder()) + try sync.addHandler(serverRecorder) + return channel + } }.get() ) @@ -213,8 +224,11 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { for _ in 0.. EventLoopFuture in - channel.pipeline.addHandlers([SimpleRequest(), InboundFramePayloadRecorder()]).map { - channel + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(SimpleRequest()) + try sync.addHandler(InboundFramePayloadRecorder()) + return channel } } @@ -368,7 +382,12 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { } http2ConnectionInitializer: { channel in channel.eventLoop.makeSucceededVoidFuture() } http2StreamInitializer: { channel -> EventLoopFuture in - channel.pipeline.addHandlers([OKResponder(), serverRecorder]).map { _ in channel } + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(OKResponder()) + try sync.addHandler(serverRecorder) + return channel + } }.get() // Let's pretend the TLS handler did protocol negotiation for us @@ -403,8 +422,11 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { for _ in 0.. EventLoopFuture in - channel.pipeline.addHandlers([SimpleRequest(), InboundFramePayloadRecorder()]).map { - channel + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(SimpleRequest()) + try sync.addHandler(InboundFramePayloadRecorder()) + return channel } } @@ -443,13 +465,25 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { let _ = try await self.clientChannel.pipeline.addHTTPClientHandlers().map { [clientChannel = self.clientChannel!] _ in - clientChannel.pipeline.addHandlers([ - InboundRecorderHandler(), HTTP1ClientSendability(), - ]) + clientChannel.eventLoop.makeCompletedFuture { + try clientChannel.pipeline.syncOperations.addHandlers( + [ + InboundRecorderHandler(), + HTTP1ClientSendability(), + ] + ) + } }.get() let negotiationResultFuture = try await self.serverChannel.configureAsyncHTTPServerPipeline { channel in - channel.pipeline.addHandlers([HTTP1OKResponder(), InboundRecorderHandler()]) + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandlers( + [ + HTTP1OKResponder(), + InboundRecorderHandler(), + ] + ) + } } http2ConnectionInitializer: { channel in channel.eventLoop.makeSucceededVoidFuture() } http2StreamInitializer: { channel -> EventLoopFuture in @@ -571,7 +605,10 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { } http2ConnectionInitializer: { channel in channel.eventLoop.makeSucceededVoidFuture() } http2StreamInitializer: { channel -> EventLoopFuture in - channel.pipeline.addHandlers([OKResponder()]).map { _ in channel } + channel.pipeline.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(OKResponder()) + return channel + } }.get() // Let's pretend the TLS handler did protocol negotiation for us @@ -606,8 +643,11 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { for _ in 0.. EventLoopFuture in - channel.pipeline.addHandlers([SimpleRequest(), InboundFramePayloadRecorder()]).map { - channel + channel.eventLoop.makeCompletedFuture { + let sync = channel.pipeline.syncOperations + try sync.addHandler(SimpleRequest()) + try sync.addHandler(InboundFramePayloadRecorder()) + return channel } } @@ -676,14 +716,18 @@ final class ConfiguringPipelineAsyncMultiplexerTests: XCTestCase { mode: .server, streamDelegate: serverRecorder ) { channel in - channel.pipeline.addHandler(OKResponder()) + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(OKResponder()) + } }.get() try await self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel) for _ in 0..<3 { try await clientMultiplexer.openStream { stream in - stream.pipeline.addHandlers(SimpleRequest()) + stream.eventLoop.makeCompletedFuture { + try stream.pipeline.syncOperations.addHandler(SimpleRequest()) + } } try await Self.deliverAllBytes(from: self.clientChannel, to: self.serverChannel) diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineInlineMultiplexerTests.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineInlineMultiplexerTests.swift index e7772ad..89d6b11 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineInlineMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineInlineMultiplexerTests.swift @@ -306,7 +306,7 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { func testClosingParentChannelClosesStreamChannel() throws { /// A channel handler that succeeds a promise when the channel becomes inactive. - final class InactiveHandler: ChannelInboundHandler { + final class InactiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let inactivePromise: EventLoopPromise @@ -361,7 +361,7 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { func testClosingParentChannelClosesStreamChannelWithTargetWindowSize() throws { /// A channel handler that succeeds a promise when the channel becomes inactive. - final class InactiveHandler: ChannelInboundHandler { + final class InactiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let inactivePromise: EventLoopPromise @@ -428,13 +428,6 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { } func testNegotiatedHTTP2BasicPipelineCommunicates() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } let serverRecorder = HTTP1ServerRequestRecorderHandler() let clientMultiplexer = try self.clientChannel.configureHTTP2Pipeline( mode: .client, @@ -450,7 +443,7 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { connectionConfiguration: .init(), streamConfiguration: .init() ) { channel in - channel.pipeline.addHandler(ErrorHandler()) + channel.pipeline.addHandler(NIOCloseOnErrorHandler()) } configurator: { channel in channel.pipeline.addHandler(serverRecorder) }.wait() @@ -524,14 +517,6 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { } func testNegotiatedHTTP2BasicPipelineCommunicatesWithTargetWindowSize() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } - let serverRecorder = HTTP1ServerRequestRecorderHandler() let clientMultiplexer = try self.clientChannel.configureHTTP2Pipeline( mode: .client, @@ -547,7 +532,7 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { connectionConfiguration: .init(), streamConfiguration: .init() ) { channel in - channel.pipeline.addHandler(ErrorHandler()) + channel.pipeline.addHandler(NIOCloseOnErrorHandler()) } configurator: { channel in channel.pipeline.addHandler(serverRecorder) }.wait() @@ -621,13 +606,6 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { } func testNegotiatedHTTP1BasicPipelineCommunicates() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } let serverRecorder = HTTP1ServerRequestRecorderHandler() XCTAssertNoThrow(try self.clientChannel.pipeline.addHTTPClientHandlers().wait()) @@ -636,7 +614,7 @@ class ConfiguringPipelineInlineMultiplexerTests: XCTestCase { connectionConfiguration: .init(), streamConfiguration: .init() ) { channel in - channel.pipeline.addHandler(ErrorHandler()) + channel.pipeline.addHandler(NIOCloseOnErrorHandler()) } configurator: { channel in channel.pipeline.addHandler(serverRecorder) }.wait() diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift index 531d17b..b7d0cd1 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift @@ -38,11 +38,13 @@ class ConfiguringPipelineTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testBasicPipelineCommunicates() throws { let serverRecorder = FrameRecorderHandler() - let clientHandler = try assertNoThrowWithValue(self.clientChannel.configureHTTP2Pipeline(mode: .client).wait()) + let clientHandler = try assertNoThrowWithValue( + self.clientChannel.configureHTTP2Pipeline(mode: .client).map { $0.sendableView }.wait() + ) XCTAssertNoThrow( try self.serverChannel.configureHTTP2Pipeline(mode: .server) { channel, streamID in channel.pipeline.addHandler(serverRecorder) - }.wait() + }.map { _ in }.wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -98,12 +100,13 @@ class ConfiguringPipelineTests: XCTestCase { func testBasicPipelineCommunicatesWithTargetWindowSize() throws { let serverRecorder = FrameRecorderHandler() let clientHandler = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client, targetWindowSize: 65535).wait() + self.clientChannel.configureHTTP2Pipeline(mode: .client, targetWindowSize: 65535).map { $0.sendableView } + .wait() ) XCTAssertNoThrow( try self.serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535) { channel, streamID in channel.pipeline.addHandler(serverRecorder) - }.wait() + }.map { _ in }.wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -158,8 +161,10 @@ class ConfiguringPipelineTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testPipelineRespectsPositionRequest() throws { XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(FailOnWriteHandler()).wait()) - XCTAssertNoThrow(try self.clientChannel.configureHTTP2Pipeline(mode: .client, position: .first).wait()) - XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server).wait()) + XCTAssertNoThrow( + try self.clientChannel.configureHTTP2Pipeline(mode: .client, position: .first).map { _ in }.wait() + ) + XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server).map { _ in }.wait()) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -178,9 +183,12 @@ class ConfiguringPipelineTests: XCTestCase { XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(FailOnWriteHandler()).wait()) XCTAssertNoThrow( try self.clientChannel.configureHTTP2Pipeline(mode: .client, position: .first, targetWindowSize: 65535) + .map { _ in } .wait() ) - XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535).wait()) + XCTAssertNoThrow( + try self.serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535).map { _ in }.wait() + ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -206,7 +214,7 @@ class ConfiguringPipelineTests: XCTestCase { // Register the callback on the promise so we run it as it succeeds (i.e. before fireChannelActive is called). let handlerAdded = connectionPromise.futureResult.flatMap { // Use .server to avoid sending client magic. - serverChannel.configureHTTP2Pipeline(mode: .server) + serverChannel.configureHTTP2Pipeline(mode: .server).map { _ in } } self.serverChannel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake"), promise: connectionPromise) @@ -242,7 +250,7 @@ class ConfiguringPipelineTests: XCTestCase { // Register the callback on the promise so we run it as it succeeds (i.e. before fireChannelActive is called). let handlerAdded = connectionPromise.futureResult.flatMap { // Use .server to avoid sending client magic. - serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535) + serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535).map { _ in } } self.serverChannel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake"), promise: connectionPromise) @@ -269,7 +277,7 @@ class ConfiguringPipelineTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testClosingParentChannelClosesStreamChannel() throws { /// A channel handler that succeeds a promise when the channel becomes inactive. - final class InactiveHandler: ChannelInboundHandler { + final class InactiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let inactivePromise: EventLoopPromise @@ -284,9 +292,9 @@ class ConfiguringPipelineTests: XCTestCase { } let clientMultiplexer = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client).wait() + self.clientChannel.configureHTTP2Pipeline(mode: .client).map { $0.sendableView }.wait() ) - XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server).wait()) + XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server).map { _ in }.wait()) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -315,7 +323,7 @@ class ConfiguringPipelineTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testClosingParentChannelClosesStreamChannelWithTargetWindowSize() throws { /// A channel handler that succeeds a promise when the channel becomes inactive. - final class InactiveHandler: ChannelInboundHandler { + final class InactiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let inactivePromise: EventLoopPromise @@ -330,9 +338,12 @@ class ConfiguringPipelineTests: XCTestCase { } let clientMultiplexer = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client, targetWindowSize: 65535).wait() + self.clientChannel.configureHTTP2Pipeline(mode: .client, targetWindowSize: 65535).map { $0.sendableView } + .wait() + ) + XCTAssertNoThrow( + try self.serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535).map { _ in }.wait() ) - XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535).wait()) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -373,20 +384,14 @@ class ConfiguringPipelineTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testNegotiatedHTTP2BasicPipelineCommunicates() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } - - let clientHandler = try assertNoThrowWithValue(self.clientChannel.configureHTTP2Pipeline(mode: .client).wait()) + let clientHandler = try assertNoThrowWithValue( + self.clientChannel.configureHTTP2Pipeline(mode: .client).map { $0.sendableView }.wait() + ) let serverChildChannelPromise = self.serverChannel.eventLoop.makePromise(of: Channel.self) XCTAssertNoThrow( try self.serverChannel.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: { - $0.pipeline.addHandler(ErrorHandler()) + $0.pipeline.addHandler(NIOCloseOnErrorHandler()) }) { channel in serverChildChannelPromise.succeed(channel) return channel.pipeline.addHandler(HTTP1ServerRequestRecorderHandler()) @@ -467,22 +472,15 @@ class ConfiguringPipelineTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testNegotiatedHTTP2BasicPipelineCommunicatesWithTargetWindowSize() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } - let clientHandler = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client, targetWindowSize: 65535).wait() + self.clientChannel.configureHTTP2Pipeline(mode: .client, targetWindowSize: 65535).map { $0.sendableView } + .wait() ) let serverChildChannelPromise = self.serverChannel.eventLoop.makePromise(of: Channel.self) XCTAssertNoThrow( try self.serverChannel.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: { - $0.pipeline.addHandler(ErrorHandler()) + $0.pipeline.addHandler(NIOCloseOnErrorHandler()) }) { channel in serverChildChannelPromise.succeed(channel) return channel.pipeline.addHandler(HTTP1ServerRequestRecorderHandler()) diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineWithFramePayloadStreamsTests.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineWithFramePayloadStreamsTests.swift index 5537a36..6abcbe2 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineWithFramePayloadStreamsTests.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineWithFramePayloadStreamsTests.swift @@ -24,7 +24,7 @@ import XCTest /// A simple channel handler that can be inserted in a pipeline to ensure that it never sees a write. /// /// Fails if it receives a write call. -class FailOnWriteHandler: ChannelOutboundHandler { +final class FailOnWriteHandler: ChannelOutboundHandler, Sendable { typealias OutboundIn = Never typealias OutboundOut = Never @@ -50,13 +50,13 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { func testBasicPipelineCommunicates() throws { let serverRecorder = InboundFramePayloadRecorder() - let clientHandler = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait() - ) + let multiplexer = try self.clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil) + .map { $0.sendableView }.wait() + XCTAssertNoThrow( try self.serverChannel.configureHTTP2Pipeline(mode: .server) { channel in channel.pipeline.addHandler(serverRecorder) - }.wait() + }.map { _ in }.wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -77,7 +77,7 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { let errorHandler = ErrorEncounteredHandler() let streamChannelPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) - clientHandler.createStreamChannel(promise: streamChannelPromise) { channel in + multiplexer.createStreamChannel(promise: streamChannelPromise) { channel in try? channel.pipeline.syncOperations.addHandler(errorHandler) channel.writeAndFlush(reqFrame.payload).whenComplete { _ in channel.close(promise: requestPromise) } return channel.eventLoop.makeSucceededFuture(()) @@ -109,17 +109,17 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { func testBasicPipelineCommunicatesWithTargetWindowSize() throws { let serverRecorder = InboundFramePayloadRecorder() - let clientHandler = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline( - mode: .client, - targetWindowSize: 65535, - inboundStreamInitializer: nil - ).wait() - ) + let multiplexer = try self.clientChannel.configureHTTP2Pipeline( + mode: .client, + targetWindowSize: 65535, + inboundStreamInitializer: nil + ).map { + $0.sendableView + }.wait() XCTAssertNoThrow( try self.serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535) { channel in channel.pipeline.addHandler(serverRecorder) - }.wait() + }.map { _ in }.wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -140,7 +140,7 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { let errorHandler = ErrorEncounteredHandler() let streamChannelPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) - clientHandler.createStreamChannel(promise: streamChannelPromise) { channel in + multiplexer.createStreamChannel(promise: streamChannelPromise) { channel in try? channel.pipeline.syncOperations.addHandler(errorHandler) channel.writeAndFlush(reqFrame.payload).whenComplete { _ in channel.close(promise: requestPromise) } return channel.eventLoop.makeSucceededFuture(()) @@ -177,10 +177,11 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { mode: .client, position: .first, inboundStreamInitializer: nil - ).wait() + ).map { _ in }.wait() ) XCTAssertNoThrow( - try self.serverChannel.configureHTTP2Pipeline(mode: .server, inboundStreamInitializer: nil).wait() + try self.serverChannel.configureHTTP2Pipeline(mode: .server, inboundStreamInitializer: nil).map { _ in } + .wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -203,14 +204,14 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { position: .first, targetWindowSize: 65535, inboundStreamInitializer: nil - ).wait() + ).map { _ in }.wait() ) XCTAssertNoThrow( try self.serverChannel.configureHTTP2Pipeline( mode: .server, targetWindowSize: 65535, inboundStreamInitializer: nil - ).wait() + ).map { _ in }.wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -236,7 +237,7 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { // Register the callback on the promise so we run it as it succeeds (i.e. before fireChannelActive is called). let handlerAdded = connectionPromise.futureResult.flatMap { // Use .server to avoid sending client magic. - serverChannel.configureHTTP2Pipeline(mode: .server, inboundStreamInitializer: nil) + serverChannel.configureHTTP2Pipeline(mode: .server, inboundStreamInitializer: nil).map { _ in } } self.serverChannel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake"), promise: connectionPromise) @@ -272,6 +273,7 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { let handlerAdded = connectionPromise.futureResult.flatMap { // Use .server to avoid sending client magic. serverChannel.configureHTTP2Pipeline(mode: .server, targetWindowSize: 65535, inboundStreamInitializer: nil) + .map { _ in } } self.serverChannel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake"), promise: connectionPromise) @@ -297,7 +299,7 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { func testClosingParentChannelClosesStreamChannel() throws { /// A channel handler that succeeds a promise when the channel becomes inactive. - final class InactiveHandler: ChannelInboundHandler { + final class InactiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let inactivePromise: EventLoopPromise @@ -312,10 +314,13 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { } let clientMultiplexer = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait() + self.clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).map { + $0.sendableView + }.wait() ) XCTAssertNoThrow( - try self.serverChannel.configureHTTP2Pipeline(mode: .server, inboundStreamInitializer: nil).wait() + try self.serverChannel.configureHTTP2Pipeline(mode: .server, inboundStreamInitializer: nil).map { _ in } + .wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -344,7 +349,7 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { func testClosingParentChannelClosesStreamChannelWithTargetWindowSize() throws { /// A channel handler that succeeds a promise when the channel becomes inactive. - final class InactiveHandler: ChannelInboundHandler { + final class InactiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let inactivePromise: EventLoopPromise @@ -363,14 +368,14 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { mode: .client, targetWindowSize: 65535, inboundStreamInitializer: nil - ).wait() + ).map { $0.sendableView }.wait() ) XCTAssertNoThrow( try self.serverChannel.configureHTTP2Pipeline( mode: .server, targetWindowSize: 65535, inboundStreamInitializer: nil - ).wait() + ).map { _ in }.wait() ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -411,22 +416,16 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { } func testNegotiatedHTTP2BasicPipelineCommunicates() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } - let serverRecorder = HTTP1ServerRequestRecorderHandler() let clientHandler = try assertNoThrowWithValue( - self.clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait() + self.clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).map { + $0.sendableView + }.wait() ) XCTAssertNoThrow( try self.serverChannel.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: { - $0.pipeline.addHandler(ErrorHandler()) + $0.pipeline.addHandler(NIOCloseOnErrorHandler()) }) { channel in channel.pipeline.addHandler(serverRecorder) }.wait() @@ -500,26 +499,18 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { } func testNegotiatedHTTP2BasicPipelineCommunicatesWithTargetWindowSize() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } - let serverRecorder = HTTP1ServerRequestRecorderHandler() let clientHandler = try assertNoThrowWithValue( self.clientChannel.configureHTTP2Pipeline( mode: .client, targetWindowSize: 65535, inboundStreamInitializer: nil - ).wait() + ).map { $0.sendableView }.wait() ) XCTAssertNoThrow( try self.serverChannel.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: { - $0.pipeline.addHandler(ErrorHandler()) + $0.pipeline.addHandler(NIOCloseOnErrorHandler()) }) { channel in channel.pipeline.addHandler(serverRecorder) }.wait() @@ -593,23 +584,14 @@ class ConfiguringPipelineWithFramePayloadStreamsTests: XCTestCase { } func testNegotiatedHTTP1BasicPipelineCommunicates() throws { - final class ErrorHandler: ChannelInboundHandler { - typealias InboundIn = Never - - func errorCaught(context: ChannelHandlerContext, error: Error) { - context.close(promise: nil) - } - } let serverRecorder = HTTP1ServerRequestRecorderHandler() XCTAssertNoThrow(try self.clientChannel.pipeline.addHTTPClientHandlers().wait()) - XCTAssertNoThrow( - try self.serverChannel.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: { - $0.pipeline.addHandler(ErrorHandler()) - }) { channel in - channel.pipeline.addHandler(serverRecorder) - }.wait() - ) + try self.serverChannel.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: { + $0.pipeline.addHandler(NIOCloseOnErrorHandler()) + }) { channel in + channel.pipeline.addHandler(serverRecorder) + }.wait() // Let's pretend the TLS handler did protocol negotiation for us self.serverChannel.pipeline.fireUserInboundEventTriggered( diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift index 5370cbf..56374ee 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift @@ -26,11 +26,12 @@ extension Channel { /// Adds a simple no-op `HTTP2StreamMultiplexer` to the pipeline. fileprivate func addNoOpMultiplexer(mode: NIOHTTP2Handler.ParserMode) { XCTAssertNoThrow( - try self.pipeline.addHandler( - HTTP2StreamMultiplexer(mode: mode, channel: self) { channel in + try self.eventLoop.makeCompletedFuture { + let mux = HTTP2StreamMultiplexer(mode: mode, channel: self) { channel in self.eventLoop.makeSucceededFuture(()) } - ).wait() + try self.pipeline.syncOperations.addHandler(mux) + }.wait() ) } } @@ -38,7 +39,7 @@ extension Channel { private struct MyError: Error {} /// A handler that asserts the frames received match the expected set. -internal final class FramePayloadExpecter: ChannelInboundHandler { +internal final class FramePayloadExpecter: ChannelInboundHandler, @unchecked Sendable { typealias InboundIn = HTTP2Frame.FramePayload typealias OutboundOut = HTTP2Frame.FramePayload @@ -108,7 +109,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channelCount.wrappingIncrement(ordering: .sequentiallyConsistent) return channel.close() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a bunch of headers frames. for streamID in stride(from: 1, to: 100, by: 2) { @@ -128,7 +129,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { } return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a bunch of headers frames with endStream on them. This should open some streams. let streamIDs = stride(from: 1, to: 100, by: 2).map { HTTP2StreamID($0) } @@ -174,7 +175,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { FramePayloadExpecter(expectedPayload: [frame.payload, rstStreamFrame.payload]) ) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open the stream up. XCTAssertNoThrow(try self.channel.writeInbound(frame)) @@ -222,7 +223,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { // The channel won't see the goaway frame. return channel.pipeline.addHandler(FramePayloadExpecter(expectedPayload: [frame.payload])) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open the stream up. XCTAssertNoThrow(try self.channel.writeInbound(frame)) @@ -295,7 +296,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channel.close() } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a bunch of headers frames. These will all be answered by RST_STREAM frames. let streamIDs = stride(from: 1, to: 100, by: 2).map { HTTP2StreamID($0) } @@ -322,7 +323,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -352,7 +353,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -391,7 +392,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -449,7 +450,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -501,7 +502,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { setupCompletePromise.futureResult } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -557,7 +558,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { } } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a headers frame to open the stream, along with some DATA frames. let streamID = HTTP2StreamID(1) @@ -620,7 +621,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeTracker).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -665,7 +666,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { childChannelPromise.succeed(channel) return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -703,7 +704,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { childChannelPromise.succeed(channel) return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -745,7 +746,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { childChannelPromise.succeed(channel) return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -794,7 +795,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(frameRecorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -850,7 +851,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(InboundFramePayloadRecorder()) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -903,7 +904,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(frameRecorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -959,7 +960,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { } } XCTAssertNoThrow(try self.channel.pipeline.addHandler(readCounter).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -1042,7 +1043,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { channel in channel.pipeline.addHandler(HandlerRemovedHandler(removedPromise: handlerRemovedPromise)) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -1074,7 +1075,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { channel in channel.pipeline.addHandler(HandlerRemovedHandler(removedPromise: handlerRemovedPromise)) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -1105,7 +1106,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) for _ in 0..<3 { @@ -1155,7 +1156,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) for _ in 0..<3 { @@ -1208,7 +1209,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { channel in childChannelPromise.succeed(channel) return configurePromise.futureResult @@ -1235,7 +1236,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { channel in childChannelPromise.succeed(channel) return configurePromise.futureResult @@ -1274,7 +1275,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { channel in configurePromise.futureResult } @@ -1300,7 +1301,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { channel in let activeRecorder = ActiveHandler(activatedPromise: activePromise) return channel.pipeline.addHandler(activeRecorder) @@ -1329,7 +1330,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 8765)).wait()) XCTAssertFalse(activated.load(ordering: .sequentiallyConsistent)) @@ -1360,7 +1361,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let activeRecorder = ActiveHandler(activatedPromise: activePromise) return channel.pipeline.addHandler(activeRecorder) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) self.channel.pipeline.fireChannelActive() // Open a new stream. @@ -1409,7 +1410,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertFalse(closed.load(ordering: .sequentiallyConsistent)) multiplexer.createStreamChannel(promise: nil) { channel in @@ -1430,7 +1431,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) let channelPromise = self.channel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: channelPromise) { channel in @@ -1460,7 +1461,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertFalse(closed.load(ordering: .sequentiallyConsistent)) multiplexer.createStreamChannel(promise: nil) { channel in @@ -1484,7 +1485,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) let channelPromise = self.channel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: channelPromise) { channel in @@ -1516,7 +1517,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { channel in channel.pipeline.addHandler(QuickFramePayloadResponseHandler()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We're going to send in 10 request frames. let requestHeaders = HPACKHeaders([ @@ -1527,7 +1528,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(.init(headers: requestHeaders, endStream: true))) } for frame in framesToSend { - self.channel.pipeline.fireChannelRead(NIOAny(frame)) + self.channel.pipeline.fireChannelRead(frame) } self.channel.embeddedEventLoop.run() @@ -1553,7 +1554,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { childChannel.pipeline.addHandler(readCompleteCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertEqual(frameRecorder.receivedFrames.count, 0) readCompleteCounter.readCompleteCount.withLockedValue { readCompleteCount in @@ -1565,7 +1566,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { (":path", "/"), (":method", "GET"), (":authority", "localhost"), (":scheme", "https"), ]) let requestFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: requestHeaders, endStream: false))) - self.channel.pipeline.fireChannelRead(NIOAny(requestFrame)) + self.channel.pipeline.fireChannelRead(requestFrame) self.activateStream(1) self.channel.embeddedEventLoop.run() @@ -1583,7 +1584,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { ) for frame in dataFrames { - self.channel.pipeline.fireChannelRead(NIOAny(frame)) + self.channel.pipeline.fireChannelRead(frame) } // We should have 1 read (the HEADERS), and one read complete. @@ -1623,7 +1624,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { childChannel.pipeline.addHandler(readCompleteCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertEqual(frameRecorder.receivedFrames.count, 0) readCompleteCounter.readCompleteCount.withLockedValue { readCompleteCount in @@ -1640,7 +1641,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { streamID: streamID, payload: .headers(.init(headers: requestHeaders, endStream: false)) ) - self.channel.pipeline.fireChannelRead(NIOAny(requestFrame)) + self.channel.pipeline.fireChannelRead(requestFrame) self.activateStream(streamID) } self.channel.embeddedEventLoop.run() @@ -1661,7 +1662,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { var requestData = self.channel.allocator.buffer(capacity: 1024) requestData.writeBytes("Hello world!".utf8) let frame = HTTP2Frame(streamID: 1, payload: .data(.init(data: .byteBuffer(requestData), endStream: false))) - self.channel.pipeline.fireChannelRead(NIOAny(frame)) + self.channel.pipeline.fireChannelRead(frame) // We should have 3 reads, and 3 read completes. The frame is not delivered as we have no frame fast-path. XCTAssertEqual(frameRecorder.receivedFrames.count, 3) @@ -1697,7 +1698,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -1751,7 +1752,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -1808,7 +1809,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -1881,7 +1882,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5)).wait()) @@ -1907,7 +1908,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5)).wait()) // Ok, create an inbound channel. @@ -1943,10 +1944,10 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer() throws { let frameRecorder = FrameWriteRecorder() - XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameRecorder).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(frameRecorder)) let multiplexer = HTTP2StreamMultiplexer(mode: .client, channel: self.channel, inboundStreamInitializer: nil) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) // Create a payload based stream. @@ -2003,7 +2004,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(readCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -2051,7 +2052,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { ) { channel in channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2061,7 +2062,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { (":path", "/"), (":method", "GET"), (":authority", "localhost"), (":scheme", "https"), ]) let headersFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: headers))) - self.channel.pipeline.fireChannelRead(NIOAny(headersFrame)) + self.channel.pipeline.fireChannelRead(headersFrame) // Activate the stream. self.activateStream(1) @@ -2081,7 +2082,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { .init(data: .byteBuffer(.init(repeating: 0, count: targetWindowSize + 1))) ) let dataFrame = HTTP2Frame(streamID: 1, payload: data) - self.channel.pipeline.fireChannelRead(NIOAny(dataFrame)) + self.channel.pipeline.fireChannelRead(dataFrame) self.channel.pipeline.fireUserInboundEventTriggered(StreamClosedEvent(streamID: 1, reason: nil)) self.channel.pipeline.fireChannelReadComplete() @@ -2100,7 +2101,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { ) { channel in channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2110,7 +2111,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { (":path", "/"), (":method", "GET"), (":authority", "localhost"), (":scheme", "https"), ]) let headersFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: headers))) - self.channel.pipeline.fireChannelRead(NIOAny(headersFrame)) + self.channel.pipeline.fireChannelRead(headersFrame) // Activate the stream. self.activateStream(1) @@ -2132,11 +2133,11 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let firstDataFrame = HTTP2Frame(streamID: 1, payload: firstData) let secondDataFrame = HTTP2Frame(streamID: 1, payload: secondData) - self.channel.pipeline.fireChannelRead(NIOAny(firstDataFrame)) + self.channel.pipeline.fireChannelRead(firstDataFrame) windowUpdated = NIOHTTP2WindowUpdatedEvent(streamID: 1, inboundWindowSize: 1, outboundWindowSize: nil) self.channel.pipeline.fireUserInboundEventTriggered(windowUpdated) - self.channel.pipeline.fireChannelRead(NIOAny(secondDataFrame)) + self.channel.pipeline.fireChannelRead(secondDataFrame) // This is nil here for a reason: it reflects what would actually be sent in the real code. Relevantly, the nil currently // does not actually propagate into the handler, which matters a lot. windowUpdated = NIOHTTP2WindowUpdatedEvent(streamID: 1, inboundWindowSize: nil, outboundWindowSize: nil) @@ -2169,7 +2170,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { return channel.close() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) let frame = HTTP2Frame(streamID: HTTP2StreamID(1), payload: .headers(.init(headers: HPACKHeaders()))) XCTAssertNoThrow(try self.channel.writeInbound(frame)) @@ -2188,7 +2189,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { ) { channel in channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2277,7 +2278,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { XCTFail("Server push is unexpected") return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2286,7 +2287,9 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let childChannelPromise = self.channel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: nil) { channel in childChannelPromise.succeed(channel) - return channel.pipeline.addHandler(ReadAndFrameConsumer()) + return channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(ReadAndFrameConsumer()) + } } self.channel.embeddedEventLoop.run() @@ -2356,7 +2359,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { } } -final class ErrorRecorder: ChannelInboundHandler { +final class ErrorRecorder: ChannelInboundHandler, Sendable { typealias InboundIn = Any let errors = NIOLockedValueBox<[Error]>([]) diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecTests.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecTests.swift index 960d9aa..994deac 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecTests.swift @@ -32,7 +32,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testBasicRequestServerSide() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -58,7 +58,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testRequestWithOnlyHeadServerSide() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -78,7 +78,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testRequestWithTrailers() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -106,7 +106,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testSendingSimpleResponse() throws { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic response. let responseHeaders = HPACKHeaders([("server", "swift-nio"), ("other", "header")]) @@ -140,7 +140,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testResponseWithoutTrailers() throws { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic response. let responseHeaders = HPACKHeaders([("server", "swift-nio"), ("other", "header")]) @@ -167,7 +167,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testResponseWith100Blocks() throws { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // First, we're going to send a few 103 blocks. let informationalResponseHeaders = HPACKHeaders([("link", "no link really")]) @@ -213,8 +213,8 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let promiseRecorder = PromiseRecorder() let promises: [EventLoopPromise] = (0..<3).map { _ in self.channel.eventLoop.makePromise() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(promiseRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(promiseRecorder)) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic response. let responseHeaders = HTTPHeaders([("server", "swift-nio"), ("other", "header")]) @@ -242,7 +242,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request @@ -283,7 +285,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request @@ -317,7 +321,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request @@ -357,7 +363,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -393,7 +401,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .http)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .http) + ) ) // A basic request. @@ -422,7 +432,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -472,9 +484,11 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let promiseRecorder = PromiseRecorder() let promises: [EventLoopPromise] = (0..<3).map { _ in self.channel.eventLoop.makePromise() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(promiseRecorder).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(promiseRecorder)) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic response. @@ -501,7 +515,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithoutMethod() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -518,7 +532,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithDuplicateMethod() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -539,7 +553,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithoutPath() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -556,7 +570,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithDuplicatePath() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -577,7 +591,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithoutAuthority() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -597,7 +611,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithDuplicateAuthority() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -618,7 +632,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithoutScheme() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -635,7 +649,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testReceiveRequestWithDuplicateScheme() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -657,7 +671,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testReceiveResponseWithoutStatus() throws { XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic response. @@ -674,7 +690,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testReceiveResponseWithDuplicateStatus() throws { XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -694,7 +712,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testReceiveResponseWithNonNumericalStatus() throws { XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basis request @@ -720,7 +740,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testSendRequestWithoutHost() throws { XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request without Host. @@ -737,7 +759,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testSendRequestWithDuplicateHost() throws { XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request with too many host headers. @@ -758,7 +782,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testFramesWithoutHTTP1EquivalentAreIgnored() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) let headers = HPACKHeaders([(":method", "GET"), (":scheme", "https"), (":path", "/x")]) @@ -783,7 +809,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -806,7 +834,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testWeTolerateUpperCasedHTTP1HeadersForResponses() throws { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) var responseHead = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) responseHead.headers = HTTPHeaders([("UpperCased", "Header")]) @@ -825,12 +853,12 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler( + try self.channel.pipeline.syncOperations.addHandler( HTTP2FramePayloadToHTTP1ClientCodec( httpProtocol: .https, normalizeHTTPHeaders: false ) - ).wait() + ) ) // A basic request. @@ -854,8 +882,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec(normalizeHTTPHeaders: false)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ServerCodec(normalizeHTTPHeaders: false) + ) ) var responseHead = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) @@ -875,7 +904,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -903,7 +934,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -929,7 +962,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testWeStripIllegalHeadersAsWellAsTheHeadersNominatedByTheConnectionHeaderForResponses() throws { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) var responseHead = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) responseHead.headers = HTTPHeaders([ @@ -949,7 +982,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { } func testServerSideWithEmptyFinalPackage() throws { - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -979,7 +1012,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { func testClientSideWithEmptyFinalPackage() throws { XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)).wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https) + ) ) // A basic request. @@ -1023,7 +1058,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { for method in methods { let embedded = EmbeddedChannel() - XCTAssertNoThrow(try embedded.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try embedded.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -1060,7 +1095,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { for method in methods { let embedded = EmbeddedChannel() - XCTAssertNoThrow(try embedded.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try embedded.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -1108,7 +1143,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { for method in methods { let embedded = EmbeddedChannel() - XCTAssertNoThrow(try embedded.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try embedded.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -1145,7 +1180,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { for method in methods { let embedded = EmbeddedChannel() - XCTAssertNoThrow(try embedded.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try embedded.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -1191,7 +1226,7 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { for method in methods { let embedded = EmbeddedChannel() - XCTAssertNoThrow(try embedded.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).wait()) + XCTAssertNoThrow(try embedded.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())) // A basic request. let requestHeaders = HPACKHeaders([ @@ -1240,9 +1275,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try embedded.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try embedded.pipeline.addHandler( + try embedded.pipeline.syncOperations.addHandler( HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https, normalizeHTTPHeaders: true) - ).wait() + ) ) let requestHead = HTTPRequestHead( @@ -1290,9 +1325,9 @@ final class HTTP2FramePayloadToHTTP1CodecTests: XCTestCase { let writeRecorder = FramePayloadWriteRecorder() XCTAssertNoThrow(try embedded.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try embedded.pipeline.addHandler( + try embedded.pipeline.syncOperations.addHandler( HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https, normalizeHTTPHeaders: true) - ).wait() + ) ) let requestHead = HTTPRequestHead( diff --git a/Tests/NIOHTTP2Tests/HTTP2InlineStreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2InlineStreamMultiplexerTests.swift index ba9f2d6..b5d4275 100644 --- a/Tests/NIOHTTP2Tests/HTTP2InlineStreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2InlineStreamMultiplexerTests.swift @@ -26,15 +26,16 @@ extension Channel { /// Adds a simple no-op ``HTTP2StreamMultiplexer`` to the pipeline. fileprivate func addNoOpInlineMultiplexer(mode: NIOHTTP2Handler.ParserMode, eventLoop: EventLoop) { XCTAssertNoThrow( - try self.pipeline.addHandler( - NIOHTTP2Handler( + try self.eventLoop.makeCompletedFuture { + let handler = NIOHTTP2Handler( mode: mode, eventLoop: eventLoop, inboundStreamInitializer: { channel in self.eventLoop.makeSucceededFuture(()) } ) - ).wait() + try self.pipeline.syncOperations.addHandler(handler) + }.wait() ) } } @@ -123,13 +124,13 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } // a ChannelInboundHandler which executes the provided closure on channelRead - class TestHookHandler: ChannelInboundHandler { + final class TestHookHandler: ChannelInboundHandler, Sendable { typealias InboundIn = HTTP2Frame.FramePayload typealias OutboundOut = HTTP2Frame.FramePayload - let channelReadHook: (ChannelHandlerContext, HTTP2Frame.FramePayload) -> Void + let channelReadHook: @Sendable (ChannelHandlerContext, HTTP2Frame.FramePayload) -> Void - init(channelReadHook: @escaping (ChannelHandlerContext, HTTP2Frame.FramePayload) -> Void) { + init(channelReadHook: @escaping @Sendable (ChannelHandlerContext, HTTP2Frame.FramePayload) -> Void) { self.channelReadHook = channelReadHook } @@ -164,7 +165,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's send a bunch of headers frames. @@ -204,7 +205,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { ) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's send a bunch of headers frames with endStream on them. This should open some streams. @@ -250,7 +251,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { ) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open the stream up. @@ -295,7 +296,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededVoidFuture() } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup(mode: .client)) // Let's open the stream up. @@ -341,7 +342,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try frameReceiver.drainConnectionSetupWrites() @@ -382,7 +383,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try frameReceiver.drainConnectionSetupWrites() @@ -426,7 +427,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try frameReceiver.drainConnectionSetupWrites() @@ -470,7 +471,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try frameReceiver.drainConnectionSetupWrites() @@ -535,7 +536,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try frameReceiver.drainConnectionSetupWrites() @@ -595,7 +596,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's send a headers frame to open the stream. @@ -654,7 +655,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try writeRecorder.drainConnectionSetupWrites() @@ -728,7 +729,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeTracker).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) try writeTracker.drainConnectionSetupWrites() @@ -777,7 +778,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededVoidFuture() } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -835,7 +836,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { ) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -887,7 +888,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -946,7 +947,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(recorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open two streams. @@ -990,7 +991,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(frameRecorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -1042,7 +1043,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } } XCTAssertNoThrow(try self.channel.pipeline.addHandler(readCounter).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -1140,7 +1141,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { ]) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -1184,7 +1185,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { }, ]) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -1218,7 +1219,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) // to make the channel active XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -1278,7 +1279,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) (self.channel.eventLoop as! EmbeddedEventLoop).run() try writeRecorder.drainConnectionSetupWrites(mode: .client) @@ -1314,7 +1315,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) let multiplexer = try http2Handler.multiplexer.wait() multiplexer.createStreamChannel(promise: nil) { channel in childChannelPromise.succeed(channel) @@ -1357,7 +1358,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { } ) XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) let multiplexer = try http2Handler.multiplexer.wait() multiplexer.createStreamChannel(promise: nil) { channel in configurePromise.futureResult @@ -1388,7 +1389,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) let multiplexer = try http2Handler.multiplexer.wait() multiplexer.createStreamChannel(promise: nil) { channel in let activeRecorder = ActiveHandler(activatedPromise: activePromise) @@ -1422,7 +1423,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 8765)).wait()) XCTAssertFalse(activated.load(ordering: .sequentiallyConsistent)) @@ -1458,7 +1459,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.pipeline.addHandler(activeRecorder) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) self.channel.pipeline.fireChannelActive() @@ -1513,7 +1514,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertFalse(closed.load(ordering: .sequentiallyConsistent)) let multiplexer = try http2Handler.multiplexer.wait() @@ -1537,7 +1538,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) let channelPromise = self.channel.eventLoop.makePromise(of: Channel.self) let multiplexer = try http2Handler.multiplexer.wait() @@ -1572,7 +1573,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertEqual(try self.channel.readAllBuffers().count, 2) // magic & settings XCTAssertFalse(closed.load(ordering: .sequentiallyConsistent)) @@ -1601,7 +1602,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertEqual(try self.channel.readAllBuffers().count, 2) // magic & settings let channelPromise = self.channel.eventLoop.makePromise(of: Channel.self) @@ -1637,7 +1638,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(QuickFramePayloadResponseHandler()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // We're going to send in 10 request frames. @@ -1650,7 +1651,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(.init(headers: requestHeaders, endStream: true))) } for frame in framesToSend { - self.channel.pipeline.fireChannelRead(NIOAny(try frame.encode())) + self.channel.pipeline.fireChannelRead(try frame.encode()) } self.channel.embeddedEventLoop.run() @@ -1673,7 +1674,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { childChannel.pipeline.addHandler(readCompleteCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) XCTAssertEqual(frameRecorder.receivedFrames.count, 0) @@ -1686,7 +1687,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { (":path", "/"), (":method", "GET"), (":authority", "localhost"), (":scheme", "https"), ]) let requestFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: requestHeaders, endStream: false))) - XCTAssertNoThrow(self.channel.pipeline.fireChannelRead(NIOAny(try requestFrame.encode()))) + XCTAssertNoThrow(self.channel.pipeline.fireChannelRead(try requestFrame.encode())) self.channel.embeddedEventLoop.run() XCTAssertEqual(frameRecorder.receivedFrames.count, 1) @@ -1700,7 +1701,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { payload: .data(.init(data: .byteBuffer(ByteBuffer(string: "Hello, world!")))) ).encode() for _ in 0..<9 { - self.channel.pipeline.fireChannelRead(NIOAny(dataFrame)) + self.channel.pipeline.fireChannelRead(dataFrame) } // We should have 1 read (the HEADERS), and one read complete. @@ -1737,7 +1738,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { childChannel.pipeline.addHandler(readCompleteCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) XCTAssertEqual(frameRecorder.receivedFrames.count, 0) @@ -1754,7 +1755,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { streamID: streamID, payload: .headers(.init(headers: requestHeaders, endStream: false)) ) - try self.channel.pipeline.fireChannelRead(NIOAny(requestFrame.encode())) + try self.channel.pipeline.fireChannelRead(requestFrame.encode()) } self.channel.embeddedEventLoop.run() @@ -1775,7 +1776,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { streamID: 1, payload: .data(.init(data: .byteBuffer(ByteBuffer(string: "Hello, world!")))) ) - self.channel.pipeline.fireChannelRead(NIOAny(try dataFrame.encode())) + self.channel.pipeline.fireChannelRead(try dataFrame.encode()) // We should have 3 reads, and 3 read completes. The frame is not delivered as we have no frame fast-path. XCTAssertEqual(frameRecorder.receivedFrames.count, 3) @@ -1814,7 +1815,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -1874,7 +1875,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -1928,7 +1929,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2006,7 +2007,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5)).wait()) @@ -2034,7 +2035,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(readCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's open a stream. @@ -2095,7 +2096,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.close() } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) let frame = HTTP2Frame(streamID: HTTP2StreamID(1), payload: .headers(.init(headers: .basicRequestValues))) @@ -2116,7 +2117,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { channel.eventLoop.makeSucceededFuture(()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup(mode: .client)) XCTAssertEqual(try self.channel.readAllBuffers().count, 3) // drain outbound magic, settings & ACK @@ -2198,7 +2199,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup(mode: .client)) XCTAssertEqual(try self.channel.readAllBuffers().count, 3) // drain outbound magic, settings & ACK @@ -2206,8 +2207,10 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { let childChannelPromise = self.channel.eventLoop.makePromise(of: Channel.self) let multiplexer = try http2Handler.multiplexer.wait() multiplexer.createStreamChannel(promise: childChannelPromise) { channel in - let consumer = ReadAndFrameConsumer() - return channel.pipeline.addHandler(consumer) + channel.eventLoop.makeCompletedFuture { + let consumer = ReadAndFrameConsumer() + return try channel.pipeline.syncOperations.addHandler(consumer) + } } self.channel.embeddedEventLoop.run() @@ -2353,7 +2356,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { ) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) XCTAssertNoThrow(try connectionSetup()) // Let's send a bunch of headers frames. This should open some streams. @@ -2405,7 +2408,7 @@ final class HTTP2InlineStreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } ) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(http2Handler).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(http2Handler)) try connectionSetup(mode: .client) let multiplexer = try http2Handler.multiplexer.wait() diff --git a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift index 3fecd6f..f97cb01 100644 --- a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift @@ -27,11 +27,12 @@ extension Channel { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") fileprivate func addNoOpMultiplexer(mode: NIOHTTP2Handler.ParserMode) { XCTAssertNoThrow( - try self.pipeline.addHandler( - HTTP2StreamMultiplexer(mode: mode, channel: self) { (channel, streamID) in + try self.eventLoop.makeCompletedFuture { + let mux = HTTP2StreamMultiplexer(mode: mode, channel: self) { (channel, streamID) in self.eventLoop.makeSucceededFuture(()) } - ).wait() + try self.pipeline.syncOperations.addHandler(mux) + }.wait() ) } } @@ -72,21 +73,39 @@ private final class FrameExpecter: ChannelInboundHandler { // A handler that keeps track of the writes made on a channel. Used to work around the limitations // in `EmbeddedChannel`. -final class WriteRecorder: ChannelOutboundHandler { +final class WriteRecorder: ChannelOutboundHandler, Sendable { typealias OutboundIn = Write typealias OutboundOut = Write - var flushedWrites: [Write] = [] - private var unflushedWrites: [Write] = [] + struct Writes: Sendable { + var flushed: [Write] = [] + var unflushed: [Write] = [] + } + + private let writes = NIOLockedValueBox(Writes()) + + var flushedWrites: [Write] { + get { + self.writes.withLockedValue { $0.flushed } + } + set { + self.writes.withLockedValue { $0.flushed = newValue } + } + } func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { - self.unflushedWrites.append(self.unwrapOutboundIn(data)) + self.writes.withLockedValue { + $0.unflushed.append(self.unwrapOutboundIn(data)) + } context.write(data, promise: promise) } func flush(context: ChannelHandlerContext) { - self.flushedWrites.append(contentsOf: self.unflushedWrites) - self.unflushedWrites = [] + self.writes.withLockedValue { + $0.flushed.append(contentsOf: $0.unflushed) + $0.unflushed.removeAll() + + } context.flush() } } @@ -137,14 +156,18 @@ final class ReadCounter: ChannelOutboundHandler, Sendable { } /// A handler that tracks the number of times flush() was called on the channel. -final class FlushCounter: ChannelOutboundHandler { +final class FlushCounter: ChannelOutboundHandler, Sendable { typealias OutboundIn = Any typealias OutboundOut = Any - var flushCount = 0 + private let count = NIOLockedValueBox(0) + + var flushCount: Int { + self.count.withLockedValue { $0 } + } func flush(context: ChannelHandlerContext) { - self.flushCount += 1 + self.count.withLockedValue { $0 += 1 } context.flush() } } @@ -186,7 +209,7 @@ final class QuickResponseHandler: ChannelInboundHandler { } } -final class QuickFramePayloadResponseHandler: ChannelInboundHandler { +final class QuickFramePayloadResponseHandler: ChannelInboundHandler, Sendable { typealias InboundIn = HTTP2Frame.FramePayload typealias OutboundOut = HTTP2Frame.FramePayload @@ -207,7 +230,7 @@ final class QuickFramePayloadResponseHandler: ChannelInboundHandler { /// A channel handler that succeeds a promise when removed from /// a pipeline. -final class HandlerRemovedHandler: ChannelInboundHandler { +final class HandlerRemovedHandler: ChannelInboundHandler, Sendable { typealias InboundIn = HTTP2Frame let removedPromise: EventLoopPromise @@ -222,7 +245,7 @@ final class HandlerRemovedHandler: ChannelInboundHandler { } /// A channel handler that succeeds a promise when its channel becomes active. -final class ActiveHandler: ChannelInboundHandler { +final class ActiveHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Any let activatedPromise: EventLoopPromise @@ -283,7 +306,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { channelCount.wrappingIncrement(ordering: .sequentiallyConsistent) return channel.close() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a bunch of headers frames. for streamID in stride(from: 1, to: 100, by: 2) { @@ -304,7 +327,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { } return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a bunch of headers frames with endStream on them. This should open some streams. let streamIDs = stride(from: 1, to: 100, by: 2).map { HTTP2StreamID($0) } @@ -342,14 +365,16 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { let rstStreamFrame = HTTP2Frame(streamID: streamID, payload: .rstStream(.cancel)) let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { (channel, _) in - try? channel.pipeline.syncOperations.addHandler(errorEncounteredHandler) - XCTAssertNil(errorEncounteredHandler.encounteredError) - channel.closeFuture.whenSuccess { - streamChannelClosed.withLockedValue { $0 = true } + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(errorEncounteredHandler) + XCTAssertNil(errorEncounteredHandler.encounteredError) + channel.closeFuture.whenSuccess { + streamChannelClosed.withLockedValue { $0 = true } + } + try channel.pipeline.syncOperations.addHandler(FrameExpecter(expectedFrames: [frame, rstStreamFrame])) } - return channel.pipeline.addHandler(FrameExpecter(expectedFrames: [frame, rstStreamFrame])) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open the stream up. XCTAssertNoThrow(try self.channel.writeInbound(frame)) @@ -390,15 +415,17 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { ) let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { (channel, _) in - try? channel.pipeline.syncOperations.addHandler(errorEncounteredHandler) - XCTAssertNil(errorEncounteredHandler.encounteredError) - channel.closeFuture.whenSuccess { - streamChannelClosed.withLockedValue { $0 = true } + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(errorEncounteredHandler) + XCTAssertNil(errorEncounteredHandler.encounteredError) + channel.closeFuture.whenSuccess { + streamChannelClosed.withLockedValue { $0 = true } + } + // The channel won't see the goaway frame. + try channel.pipeline.syncOperations.addHandler(FrameExpecter(expectedFrames: [frame])) } - // The channel won't see the goaway frame. - return channel.pipeline.addHandler(FrameExpecter(expectedFrames: [frame])) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open the stream up. XCTAssertNoThrow(try self.channel.writeInbound(frame)) @@ -474,7 +501,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { channel.close() } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a bunch of headers frames. These will all be answered by RST_STREAM frames. let streamIDs = stride(from: 1, to: 100, by: 2).map { HTTP2StreamID($0) } @@ -502,7 +529,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -533,7 +560,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -573,7 +600,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -632,7 +659,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(frameReceiver).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -685,7 +712,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { setupCompletePromise.futureResult } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -697,9 +724,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { // The channel should now be available, but no frames should have been received on either the parent or child channel. let childChannel = try channelPromise.futureResult.wait() - let frameRecorder = - try childChannel.pipeline.context(handlerType: InboundFrameRecorder.self).wait().handler - as! InboundFrameRecorder + let frameRecorder = try childChannel.pipeline.handler(type: InboundFrameRecorder.self).wait() self.channel.assertNoFramesReceived() XCTAssertEqual(frameRecorder.receivedFrames.count, 0) @@ -744,7 +769,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { } } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's send a headers frame to open the stream, along with some DATA frames. let streamID = HTTP2StreamID(1) @@ -761,9 +786,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { // The channel should now be available, but no frames should have been received on either the parent or child channel. let childChannel = try channelPromise.futureResult.wait() - let frameRecorder = - try childChannel.pipeline.context(handlerType: InboundFrameRecorder.self).wait().handler - as! InboundFrameRecorder + let frameRecorder = try childChannel.pipeline.handler(type: InboundFrameRecorder.self).wait() self.channel.assertNoFramesReceived() XCTAssertEqual(frameRecorder.receivedFrames.count, 0) @@ -811,7 +834,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeSucceededFuture(()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeTracker).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -863,7 +886,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { childChannelPromise.succeed(channel) return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -901,7 +924,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { childChannelPromise.succeed(channel) return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -943,7 +966,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { childChannelPromise.succeed(channel) return channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -993,7 +1016,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(frameRecorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -1044,7 +1067,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(recorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -1100,7 +1123,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(frameRecorder) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -1154,7 +1177,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { } } XCTAssertNoThrow(try self.channel.pipeline.addHandler(readCounter).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) @@ -1237,7 +1260,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { (channel, _) in channel.pipeline.addHandler(HandlerRemovedHandler(removedPromise: handlerRemovedPromise)) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -1270,7 +1293,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { (channel, _) in channel.pipeline.addHandler(HandlerRemovedHandler(removedPromise: handlerRemovedPromise)) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // Let's open a stream. let streamID = HTTP2StreamID(1) @@ -1302,7 +1325,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) for _ in 0..<3 { let channelPromise: EventLoopPromise = self.channel.eventLoop.makePromise() @@ -1347,7 +1370,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) for _ in 0..<3 { let channelPromise: EventLoopPromise = self.channel.eventLoop.makePromise() @@ -1394,7 +1417,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { (channel, streamID) in childChannelPromise.succeed(channel) childStreamIDPromise.succeed(streamID) @@ -1429,7 +1452,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { (channel, streamID) in childChannelPromise.succeed(channel) childStreamIDPromise.succeed(streamID) @@ -1473,7 +1496,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { return channel.eventLoop.makeFailedFuture(MyError()) } XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { (channel, streamID) in configurePromise.futureResult } @@ -1500,7 +1523,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) multiplexer.createStreamChannel(promise: nil) { (channel, streamID) in let activeRecorder = ActiveHandler(activatedPromise: activePromise) return channel.pipeline.addHandler(activeRecorder) @@ -1530,7 +1553,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 8765)).wait()) XCTAssertFalse(activated.load(ordering: .sequentiallyConsistent)) @@ -1562,7 +1585,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { let activeRecorder = ActiveHandler(activatedPromise: activePromise) return channel.pipeline.addHandler(activeRecorder) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) self.channel.pipeline.fireChannelActive() // Open a new stream. @@ -1614,7 +1637,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertFalse(closed.load(ordering: .sequentiallyConsistent)) multiplexer.createStreamChannel(promise: nil) { (channel, streamID) in @@ -1636,7 +1659,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) let channelPromise = self.channel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: channelPromise) { (channel, streamID) in @@ -1667,7 +1690,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertFalse(closed.load(ordering: .sequentiallyConsistent)) multiplexer.createStreamChannel(promise: nil) { (channel, streamID) in @@ -1692,7 +1715,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) let channelPromise = self.channel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: channelPromise) { (channel, streamID) in @@ -1723,9 +1746,11 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { // Add a server-mode multiplexer that will add an auto-response handler. let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { (channel, _) in - channel.pipeline.addHandler(QuickResponseHandler()) + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandlers(QuickResponseHandler()) + } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We're going to send in 10 request frames. let requestHeaders = HPACKHeaders([ @@ -1764,7 +1789,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { childChannel.pipeline.addHandler(readCompleteCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertEqual(frameRecorder.receivedFrames.count, 0) readCompleteCounter.readCompleteCount.withLockedValue { readCompleteCount in @@ -1835,7 +1860,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { childChannel.pipeline.addHandler(readCompleteCounter) } } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertEqual(frameRecorder.receivedFrames.count, 0) readCompleteCounter.readCompleteCount.withLockedValue { readCompleteCount in @@ -1911,7 +1936,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -1967,7 +1992,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2018,7 +2043,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait()) @@ -2090,7 +2115,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { XCTFail("Must not be called") return channel.eventLoop.makeFailedFuture(MyError()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) // We need to activate the underlying channel here. XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5)).wait()) @@ -2117,7 +2142,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { channel.eventLoop.makeSucceededFuture(()) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5)).wait()) // Ok, create an inbound channel. @@ -2189,8 +2214,8 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { ) } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(BufferingChannelHandler()).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(BufferingChannelHandler())) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(multiplexer)) XCTAssertNoThrow(try self.channel.writeInbound(headerFrame)) XCTAssertNoThrow(try self.channel.writeInbound(dataFrame)) diff --git a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift index 6a7a6d1..9d359f7 100644 --- a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift @@ -97,7 +97,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testBasicRequestServerSide() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -132,7 +134,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testRequestWithOnlyHeadServerSide() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -157,7 +161,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testRequestWithTrailers() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -187,7 +193,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let streamID = HTTP2StreamID(1) let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic response. let responseHeaders = HPACKHeaders([("server", "swift-nio"), ("other", "header")]) @@ -227,7 +235,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let streamID = HTTP2StreamID(1) let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic response. let responseHeaders = HPACKHeaders([("server", "swift-nio"), ("other", "header")]) @@ -260,7 +270,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let streamID = HTTP2StreamID(1) let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // First, we're going to send a few 103 blocks. let informationalResponseHeaders = HPACKHeaders([("link", "no link really")]) @@ -313,8 +325,10 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let promiseRecorder = PromiseRecorder() let promises: [EventLoopPromise] = (0..<3).map { _ in self.channel.eventLoop.makePromise() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(promiseRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(promiseRecorder)) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic response. let responseHeaders = HTTPHeaders([("server", "swift-nio"), ("other", "header")]) @@ -342,8 +356,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testBasicResponseClientSide() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -383,8 +398,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testResponseWithOnlyHeadClientSide() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -412,8 +428,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testResponseWithTrailers() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -452,8 +469,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -495,8 +513,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .http)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .http) + ) ) // A basic request. @@ -529,8 +548,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testResponseWith100BlocksClientSide() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -582,10 +602,11 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let promiseRecorder = PromiseRecorder() let promises: [EventLoopPromise] = (0..<3).map { _ in self.channel.eventLoop.makePromise() } - XCTAssertNoThrow(try self.channel.pipeline.addHandler(promiseRecorder).wait()) + XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(promiseRecorder)) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic response. @@ -614,7 +635,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithoutMethod() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -635,7 +658,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithDuplicateMethod() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -660,7 +685,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithoutPath() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -681,7 +708,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithDuplicatePath() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -706,7 +735,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithoutAuthority() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -730,7 +761,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithDuplicateAuthority() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -755,7 +788,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithoutScheme() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -776,7 +811,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") func testReceiveRequestWithDuplicateScheme() throws { let streamID = HTTP2StreamID(1) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) // A basic request. let requestHeaders = HPACKHeaders([ @@ -802,8 +839,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testReceiveResponseWithoutStatus() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic response. @@ -824,8 +862,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testReceiveResponseWithDuplicateStatus() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -849,8 +888,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testReceiveResponseWithNonNumericalStatus() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -878,8 +918,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testSendRequestWithoutHost() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request without Host. @@ -898,8 +939,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testSendRequestWithDuplicateHost() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request with too many host headers. @@ -921,12 +963,12 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { func testFramesWithoutHTTP1EquivalentAreIgnored() throws { let streamID = HTTP2StreamID(1) XCTAssertNoThrow( - try self.channel.pipeline.addHandler( + try self.channel.pipeline.syncOperations.addHandler( HTTP2ToHTTP1ClientCodec( streamID: streamID, httpProtocol: .https ) - ).wait() + ) ) let headers = HPACKHeaders([(":method", "GET"), (":scheme", "https"), (":path", "/x")]) @@ -954,8 +996,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -981,7 +1024,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let streamID = HTTP2StreamID(1) let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) var responseHead = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) responseHead.headers = HTTPHeaders([("UpperCased", "Header")]) @@ -1003,13 +1048,13 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler( + try self.channel.pipeline.syncOperations.addHandler( HTTP2ToHTTP1ClientCodec( streamID: streamID, httpProtocol: .https, normalizeHTTPHeaders: false ) - ).wait() + ) ) // A basic request. @@ -1036,12 +1081,12 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler( + try self.channel.pipeline.syncOperations.addHandler( HTTP2ToHTTP1ServerCodec( streamID: streamID, normalizeHTTPHeaders: false ) - ).wait() + ) ) var responseHead = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) @@ -1064,8 +1109,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) XCTAssertNoThrow( - try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https)) - .wait() + try self.channel.pipeline.syncOperations.addHandler( + HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https) + ) ) // A basic request. @@ -1095,7 +1141,9 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { let streamID = HTTP2StreamID(1) let writeRecorder = FrameWriteRecorder() XCTAssertNoThrow(try self.channel.pipeline.addHandler(writeRecorder).wait()) - XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).wait()) + XCTAssertNoThrow( + try self.channel.pipeline.syncOperations.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)) + ) var responseHead = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) responseHead.headers = HTTPHeaders([ diff --git a/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift b/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift index 55af3b7..a132d02 100644 --- a/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift +++ b/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift @@ -507,7 +507,7 @@ extension OutboundFlowControlBufferTests { func createDataFrame(_ streamID: HTTP2StreamID, fileRegionSize: Int, endStream: Bool = false) -> HTTP2Frame { // We create a deliberately-invalid closed file handle, as we'll never actually use it. - let handle = NIOFileHandle(descriptor: -1) + let handle = NIOFileHandle(_deprecatedTakingOwnershipOfDescriptor: -1) XCTAssertNoThrow(try handle.takeDescriptorOwnership()) let region = FileRegion(fileHandle: handle, readerIndex: 0, endIndex: fileRegionSize) return HTTP2Frame(streamID: streamID, payload: .data(.init(data: .fileRegion(region), endStream: endStream))) diff --git a/Tests/NIOHTTP2Tests/ReentrancyTests.swift b/Tests/NIOHTTP2Tests/ReentrancyTests.swift index bc96eef..b6671c8 100644 --- a/Tests/NIOHTTP2Tests/ReentrancyTests.swift +++ b/Tests/NIOHTTP2Tests/ReentrancyTests.swift @@ -44,7 +44,7 @@ final class ReenterOnReadHandler: ChannelInboundHandler { } } -final class WindowUpdatedEventHandler: ChannelInboundHandler { +final class WindowUpdatedEventHandler: ChannelInboundHandler, Sendable { public typealias InboundIn = HTTP2Frame public typealias InboundOut = HTTP2Frame public typealias OutboundOut = HTTP2Frame @@ -79,8 +79,8 @@ final class ReentrancyTests: XCTestCase { /// Establish a basic HTTP/2 connection. func basicHTTP2Connection() throws { - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .client)).wait()) - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .server)).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .client))) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .server))) try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel) } @@ -109,8 +109,8 @@ final class ReentrancyTests: XCTestCase { // Ok, now we can add in the re-entrancy handler to the server channel. When it first gets a frame it's // going to fire in the buffer again. - let reEntrancyHandler = ReenterOnReadHandler { $0.fireChannelRead(NIOAny(frameBuffer)) } - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(reEntrancyHandler).wait()) + let reEntrancyHandler = ReenterOnReadHandler { $0.fireChannelRead(frameBuffer) } + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(reEntrancyHandler)) // Now we can deliver these bytes. XCTAssertTrue(try self.serverChannel.writeInbound(frameBuffer).isFull) @@ -144,7 +144,7 @@ final class ReentrancyTests: XCTestCase { // Ok, now we can add in the re-entrancy handler to the server channel. When it first gets a frame it's // going to fire channelInactive. let reEntrancyHandler = ReenterOnReadHandler { $0.fireChannelInactive() } - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(reEntrancyHandler).wait()) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(reEntrancyHandler)) // Now we can deliver these bytes. self.deliverAllBytes(from: self.clientChannel, to: self.serverChannel) @@ -175,7 +175,7 @@ final class ReentrancyTests: XCTestCase { // Ok, now we can add in the re-entrancy handler to the server channel. When it first gets a frame it's // going to fire channelInactive. let reEntrancyHandler = ReenterOnReadHandler { $0.fireUserInboundEventTriggered(ChannelEvent.inputClosed) } - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(reEntrancyHandler).wait()) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(reEntrancyHandler)) // Now we can deliver these bytes. self.deliverAllBytes(from: self.clientChannel, to: self.serverChannel) diff --git a/Tests/NIOHTTP2Tests/SimpleClientServerFramePayloadStreamTests.swift b/Tests/NIOHTTP2Tests/SimpleClientServerFramePayloadStreamTests.swift index 61a8405..9f300f2 100644 --- a/Tests/NIOHTTP2Tests/SimpleClientServerFramePayloadStreamTests.swift +++ b/Tests/NIOHTTP2Tests/SimpleClientServerFramePayloadStreamTests.swift @@ -181,12 +181,12 @@ class HTTP2ParserProxyHandler: ChannelInboundHandler, RemovableChannelHandler { func channelRead(context: ChannelHandlerContext, data: NIOAny) { XCTAssertNoThrow( - try context.pipeline.addHandler( + try context.pipeline.syncOperations.addHandler( NIOHTTP2Handler(mode: .server) - ).wait() + ) ) context.fireChannelRead(data) - _ = context.pipeline.removeHandler(context: context) + _ = context.pipeline.syncOperations.removeHandler(context: context) } } @@ -213,44 +213,44 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { withMultiplexerCallback multiplexerCallback: NIOChannelInitializer? = nil ) throws { XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler( + try self.clientChannel.pipeline.syncOperations.addHandler( NIOHTTP2Handler( mode: .client, initialSettings: clientSettings, maximumBufferedControlFrames: maximumBufferedControlFrames, maximumSequentialContinuationFrames: maximumSequentialContinuationFrames ) - ).wait() + ) ) XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( NIOHTTP2Handler( mode: .server, initialSettings: serverSettings, maximumBufferedControlFrames: maximumBufferedControlFrames, maximumSequentialContinuationFrames: maximumSequentialContinuationFrames ) - ).wait() + ) ) if let multiplexerCallback = multiplexerCallback { XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler( + try self.clientChannel.pipeline.syncOperations.addHandler( HTTP2StreamMultiplexer( mode: .client, channel: self.clientChannel, inboundStreamInitializer: multiplexerCallback ) - ).wait() + ) ) XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( HTTP2StreamMultiplexer( mode: .server, channel: self.serverChannel, inboundStreamInitializer: multiplexerCallback ) - ).wait() + ) ) } @@ -264,8 +264,8 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { /// Establish a basic HTTP/2 connection where the HTTP2Parser handler is added after the channel has been activated. func basicHTTP2DynamicPipelineConnection() throws { - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .client)).wait()) - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(HTTP2ParserProxyHandler()).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .client))) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(HTTP2ParserProxyHandler())) try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel) } @@ -618,11 +618,11 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { let clientHandler = InboundFramePayloadRecorder() let childChannelPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) - try - (self.clientChannel.pipeline.context(handlerType: HTTP2StreamMultiplexer.self).wait().handler - as! HTTP2StreamMultiplexer).createStreamChannel(promise: childChannelPromise) { channel in + self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).whenSuccess { multiplexer in + multiplexer.createStreamChannel(promise: childChannelPromise) { channel in channel.pipeline.addHandler(clientHandler) } + } (self.clientChannel.eventLoop as! EmbeddedEventLoop).run() let childChannel = try childChannelPromise.futureResult.wait() @@ -741,10 +741,11 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { HTTP2Setting(parameter: .enablePush, value: 0), ] XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .client, initialSettings: initialSettings)) - .wait() + try self.clientChannel.pipeline.syncOperations.addHandler( + NIOHTTP2Handler(mode: .client, initialSettings: initialSettings) + ) ) - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .server)).wait()) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .server))) try self.assertDoHandshake( client: self.clientChannel, server: self.serverChannel, @@ -801,11 +802,11 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { let requestBody = _requestBody // We're going to open a stream and queue up the frames for that stream. - let handler = - try self.clientChannel.pipeline.context(handlerType: HTTP2StreamMultiplexer.self).wait().handler - as! HTTP2StreamMultiplexer + let multiplexer = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).map { + $0.sendableView + }.wait() let childHandler = InboundFramePayloadRecorder() - handler.createStreamChannel(promise: nil) { channel in + multiplexer.createStreamChannel(promise: nil) { channel in let reqFramePayload = HTTP2Frame.FramePayload.headers(.init(headers: headers)) channel.write(reqFramePayload, promise: nil) @@ -976,8 +977,8 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Now add handlers to record user events. let clientHandler = UserEventRecorder() let serverHandler = UserEventRecorder() - try self.clientChannel.pipeline.addHandler(clientHandler).wait() - try self.serverChannel.pipeline.addHandler(serverHandler).wait() + try self.clientChannel.pipeline.syncOperations.addHandler(clientHandler) + try self.serverChannel.pipeline.syncOperations.addHandler(serverHandler) // We're now going to try to send a request from the client to the server and send a server response back. This // stream will terminate cleanly. @@ -1051,8 +1052,8 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Now add handlers to record user events. let clientHandler = UserEventRecorder() let serverHandler = UserEventRecorder() - try self.clientChannel.pipeline.addHandler(clientHandler).wait() - try self.serverChannel.pipeline.addHandler(serverHandler).wait() + try self.clientChannel.pipeline.syncOperations.addHandler(clientHandler) + try self.serverChannel.pipeline.syncOperations.addHandler(serverHandler) // Initiate a stream from the client. No need to send body data, we don't need it. let headers = HPACKHeaders([ @@ -1118,8 +1119,8 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Now add handlers to record user events. let clientHandler = UserEventRecorder() let serverHandler = UserEventRecorder() - try self.clientChannel.pipeline.addHandler(clientHandler).wait() - try self.serverChannel.pipeline.addHandler(serverHandler).wait() + try self.clientChannel.pipeline.syncOperations.addHandler(clientHandler) + try self.serverChannel.pipeline.syncOperations.addHandler(serverHandler) // Initiate a stream from the client. No need to send body data, we don't need it. let headers = HPACKHeaders([ @@ -1190,7 +1191,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Add handler to record stream closed event. let handler = ClosedEventVsFrameOrderingHandler(targetStreamID: serverStreamID) - try self.serverChannel.pipeline.addHandler(handler).wait() + try self.serverChannel.pipeline.syncOperations.addHandler(handler) // The client will reset the stream. let rstStreamFrame = HTTP2Frame(streamID: serverStreamID, payload: .rstStream(.cancel)) @@ -1223,7 +1224,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Add handler to record stream closed event. let handler = ClosedEventVsFrameOrderingHandler(targetStreamID: clientStreamID) - try self.clientChannel.pipeline.addHandler(handler).wait() + try self.clientChannel.pipeline.syncOperations.addHandler(handler) // The server will send GOAWAY. let goawayFrame = HTTP2Frame( @@ -1286,10 +1287,10 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { } func testBadClientMagic() throws { - class WaitForErrorHandler: ChannelInboundHandler { + final class WaitForErrorHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Never - private var errorSeenPromise: EventLoopPromise? + private let errorSeenPromise: EventLoopPromise? init(errorSeenPromise: EventLoopPromise) { self.errorSeenPromise = errorSeenPromise @@ -1309,7 +1310,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { } let errorSeenPromise: EventLoopPromise = self.clientChannel.eventLoop.makePromise() - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .server)).wait()) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .server))) XCTAssertNoThrow( try self.serverChannel.pipeline.addHandler(WaitForErrorHandler(errorSeenPromise: errorSeenPromise)).wait() ) @@ -1449,7 +1450,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Begin by getting the connection up. try self.basicHTTP2Connection() let recorder = UserEventRecorder() - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(recorder).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(recorder)) // Let's open a few streams. let headers = HPACKHeaders([ @@ -1500,9 +1501,9 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { // Begin by getting the connection up and add a stream multiplexer. try self.basicHTTP2Connection() XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( HTTP2StreamMultiplexer(mode: .server, channel: self.serverChannel, inboundStreamInitializer: nil) - ).wait() + ) ) var largeBuffer = self.clientChannel.allocator.buffer(capacity: 32767) @@ -1670,10 +1671,11 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { func testInvalidRequestHeaderBlockAllowsRstStream() throws { XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .client, headerBlockValidation: .disabled)) - .wait() + try self.clientChannel.pipeline.syncOperations.addHandler( + NIOHTTP2Handler(mode: .client, headerBlockValidation: .disabled) + ) ) - XCTAssertNoThrow(try self.serverChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .server)).wait()) + XCTAssertNoThrow(try self.serverChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .server))) try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel) // We're going to send some invalid HTTP/2 request headers. The client has validation disabled, and will allow it, but the server will not. @@ -1985,11 +1987,11 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { } func testSequentialEmptyDataFramesLimitIsConfigurable() throws { - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .client)).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(NIOHTTP2Handler(mode: .client))) XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( NIOHTTP2Handler(mode: .server, maximumSequentialEmptyDataFrames: 5) - ).wait() + ) ) XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel)) @@ -2456,7 +2458,8 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { ]) // We're going to open a stream and queue up the frames for that stream. - let handler = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).wait() + let handler = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).map { $0.sendableView } + .wait() let reqFrame = HTTP2Frame.FramePayload.headers(.init(headers: headers, endStream: true)) handler.createStreamChannel(promise: nil) { channel in @@ -2500,7 +2503,8 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { ]) // We're going to open a stream and queue up the frames for that stream. - let handler = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).wait() + let handler = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).map { $0.sendableView } + .wait() handler.createStreamChannel(promise: nil) { channel in let reqFrame = HTTP2Frame.FramePayload.headers(.init(headers: headers)) @@ -2544,7 +2548,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { channel: self.clientChannel, inboundStreamInitializer: nil ) - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(multiplexer)) let streamAPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: streamAPromise) { channel in @@ -2700,7 +2704,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { channel: self.clientChannel, inboundStreamInitializer: nil ) - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(multiplexer)) let streamPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: streamPromise) { channel in @@ -2809,7 +2813,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { ).wait() // Create the stream channel. - let multiplexer = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).wait() + let multiplexer = try self.clientChannel.pipeline.syncOperations.handler(type: HTTP2StreamMultiplexer.self) let streamPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: streamPromise) { $0.eventLoop.makeSucceededVoidFuture() @@ -2834,7 +2838,7 @@ class SimpleClientServerFramePayloadStreamTests: XCTestCase { } } -final class ShouldQuiesceEventWaiter: ChannelInboundHandler { +final class ShouldQuiesceEventWaiter: ChannelInboundHandler, Sendable { typealias InboundIn = Never private let promise: EventLoopPromise diff --git a/Tests/NIOHTTP2Tests/SimpleClientServerInlineStreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/SimpleClientServerInlineStreamMultiplexerTests.swift index 07eb0ab..f54e127 100644 --- a/Tests/NIOHTTP2Tests/SimpleClientServerInlineStreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/SimpleClientServerInlineStreamMultiplexerTests.swift @@ -51,27 +51,27 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { clientConnectionConfiguration.maximumBufferedControlFrames = maximumBufferedControlFrames clientConnectionConfiguration.headerBlockValidation = clientHeaderBlockValidation XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler( + try self.clientChannel.pipeline.syncOperations.addHandler( NIOHTTP2Handler( mode: .client, eventLoop: self.clientChannel.eventLoop, connectionConfiguration: clientConnectionConfiguration, inboundStreamInitializer: multiplexerCallback ) - ).wait() + ) ) var serverConnectionConfiguration = NIOHTTP2Handler.ConnectionConfiguration() serverConnectionConfiguration.initialSettings = serverSettings serverConnectionConfiguration.maximumBufferedControlFrames = maximumBufferedControlFrames XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( NIOHTTP2Handler( mode: .server, eventLoop: self.serverChannel.eventLoop, connectionConfiguration: serverConnectionConfiguration, inboundStreamInitializer: multiplexerCallback ) - ).wait() + ) ) try self.assertDoHandshake( @@ -105,9 +105,9 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { let clientHandler = InboundFramePayloadRecorder() let childChannelPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) - let multiplexer = try - (self.clientChannel.pipeline.context(handlerType: NIOHTTP2Handler.self).wait().handler - as! NIOHTTP2Handler).multiplexer.wait() + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() multiplexer.createStreamChannel(promise: childChannelPromise) { channel in channel.pipeline.addHandler(clientHandler) } @@ -171,11 +171,10 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { let requestBody = ByteBuffer(repeating: 0x04, count: 1024) // We're going to open a stream and queue up the frames for that stream. - let handler = - try self.clientChannel.pipeline.context(handlerType: NIOHTTP2Handler.self).wait().handler - as! NIOHTTP2Handler + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() let childHandler = InboundFramePayloadRecorder() - let multiplexer = try handler.multiplexer.wait() multiplexer.createStreamChannel(promise: nil) { channel in let reqFramePayload = HTTP2Frame.FramePayload.headers(.init(headers: headers)) channel.write(reqFramePayload, promise: nil) @@ -241,10 +240,12 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { ]) // We're going to open a stream and queue up the frames for that stream. - let handler = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).wait() + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() + // We need END_STREAM set here, because that will force the stream to be closed on the response. let reqFrame = HTTP2Frame.FramePayload.headers(.init(headers: headers, endStream: true)) - let multiplexer = try handler.multiplexer.wait() multiplexer.createStreamChannel(promise: nil) { channel in channel.writeAndFlush(reqFrame, promise: nil) return channel.eventLoop.makeSucceededFuture(()) @@ -284,8 +285,9 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { ]) // We're going to open a stream and queue up the frames for that stream. - let handler = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).wait() - let multiplexer = try handler.multiplexer.wait() + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() multiplexer.createStreamChannel(promise: nil) { channel in let reqFrame = HTTP2Frame.FramePayload.headers(.init(headers: headers)) channel.write(reqFrame, promise: nil) @@ -318,8 +320,9 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { func testStreamCreationOrder() throws { try self.basicHTTP2Connection() - let handler = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).wait() - let multiplexer = try handler.multiplexer.wait() + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() let streamAPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: streamAPromise) { channel in @@ -351,8 +354,9 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { func testWriteWithAlreadyCompletedPromise() throws { try self.basicHTTP2Connection() - let handler = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).wait() - let multiplexer = try handler.multiplexer.wait() + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() let streamPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: streamPromise) { channel in @@ -377,13 +381,24 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { channel.pipeline.addHandler(serverHandler) } - let clientHandler = InboundFramePayloadRecorder() let errorEncounteredHandler = ErrorEncounteredHandler() + let loopBoundErrorHandler = NIOLoopBound( + errorEncounteredHandler, + eventLoop: self.clientChannel.embeddedEventLoop + ) + let clientHandler = InboundFramePayloadRecorder() + let childChannelPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) - let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).wait().multiplexer.wait() + let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { + $0.multiplexer + }.wait() + multiplexer.createStreamChannel(promise: childChannelPromise) { channel in - try? channel.pipeline.syncOperations.addHandler(errorEncounteredHandler) - return channel.pipeline.addHandler(clientHandler) + channel.eventLoop.makeCompletedFuture { + let errorHandler = loopBoundErrorHandler.value + try channel.pipeline.syncOperations.addHandler(errorHandler) + try channel.pipeline.syncOperations.addHandler(clientHandler) + } } self.clientChannel.embeddedEventLoop.run() let childChannel = try childChannelPromise.futureResult.wait() @@ -538,13 +553,17 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { // Setup the connection. let receivedShouldQuiesceEvent = self.clientChannel.eventLoop.makePromise(of: Void.self) try self.basicHTTP2Connection { stream in - stream.pipeline.addHandler(ShouldQuiesceEventWaiter(promise: receivedShouldQuiesceEvent)) + stream.eventLoop.makeCompletedFuture { + try stream.pipeline.syncOperations.addHandler( + ShouldQuiesceEventWaiter(promise: receivedShouldQuiesceEvent) + ) + } } let connectionReceivedShouldQuiesceEvent = self.clientChannel.eventLoop.makePromise(of: Void.self) - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( ShouldQuiesceEventWaiter(promise: connectionReceivedShouldQuiesceEvent) - ).wait() + ) // Create the stream channel. let multiplexer = try self.clientChannel.pipeline.handler(type: NIOHTTP2Handler.self).flatMap { $0.multiplexer } @@ -641,7 +660,7 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase { // headers. try self.basicHTTP2Connection(clientHeaderBlockValidation: .disabled) - final class CloseOnExcessiveStreamErrors: ChannelInboundHandler { + final class CloseOnExcessiveStreamErrors: ChannelInboundHandler, Sendable { typealias InboundIn = Any func errorCaught(context: ChannelHandlerContext, error: any Error) { if error is NIOHTTP2Errors.ExcessiveStreamErrors { diff --git a/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift b/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift index 9d88477..e906524 100644 --- a/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift +++ b/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift @@ -44,32 +44,34 @@ class SimpleClientServerTests: XCTestCase { withMultiplexerCallback multiplexerCallback: NIOChannelInitializerWithStreamID? = nil ) throws { XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .client, initialSettings: clientSettings)) - .wait() + try self.clientChannel.pipeline.syncOperations.addHandler( + NIOHTTP2Handler(mode: .client, initialSettings: clientSettings) + ) ) XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler(NIOHTTP2Handler(mode: .server, initialSettings: serverSettings)) - .wait() + try self.serverChannel.pipeline.syncOperations.addHandler( + NIOHTTP2Handler(mode: .server, initialSettings: serverSettings) + ) ) if let multiplexerCallback = multiplexerCallback { XCTAssertNoThrow( - try self.clientChannel.pipeline.addHandler( + try self.clientChannel.pipeline.syncOperations.addHandler( HTTP2StreamMultiplexer( mode: .client, channel: self.clientChannel, inboundStreamStateInitializer: multiplexerCallback ) - ).wait() + ) ) XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( HTTP2StreamMultiplexer( mode: .server, channel: self.serverChannel, inboundStreamStateInitializer: multiplexerCallback ) - ).wait() + ) ) } @@ -105,11 +107,16 @@ class SimpleClientServerTests: XCTestCase { let clientHandler = FrameRecorderHandler() let childChannelPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) - try - (self.clientChannel.pipeline.context(handlerType: HTTP2StreamMultiplexer.self).wait().handler - as! HTTP2StreamMultiplexer).createStreamChannel(promise: childChannelPromise) { channel, streamID in - channel.pipeline.addHandler(clientHandler) + self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).whenComplete { + switch $0 { + case .success(let multiplexer): + multiplexer.createStreamChannel(promise: childChannelPromise) { channel, streamID in + channel.pipeline.addHandler(clientHandler) + } + case .failure(let error): + childChannelPromise.fail(error) } + } (self.clientChannel.eventLoop as! EmbeddedEventLoop).run() let childChannel = try childChannelPromise.futureResult.wait() @@ -175,25 +182,25 @@ class SimpleClientServerTests: XCTestCase { _requestBody.writeBytes(Array(repeating: UInt8(0x04), count: 1024)) let requestBody = _requestBody - // We're going to open a stream and queue up the frames for that stream. - let handler = - try self.clientChannel.pipeline.context(handlerType: HTTP2StreamMultiplexer.self).wait().handler - as! HTTP2StreamMultiplexer let childHandler = FrameRecorderHandler() - handler.createStreamChannel(promise: nil) { channel, streamID in - let reqFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: headers))) - channel.write(reqFrame, promise: nil) + // We're going to open a stream and queue up the frames for that stream. + self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).whenSuccess { multiplexer in + multiplexer.createStreamChannel(promise: nil) { channel, streamID in + let reqFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: headers))) + channel.write(reqFrame, promise: nil) - // Now prepare the large body. - var reqBodyFrame = HTTP2Frame(streamID: streamID, payload: .data(.init(data: .byteBuffer(requestBody)))) - for _ in 0..<63 { - channel.write(reqBodyFrame, promise: nil) + // Now prepare the large body. + var reqBodyFrame = HTTP2Frame(streamID: streamID, payload: .data(.init(data: .byteBuffer(requestBody)))) + for _ in 0..<63 { + channel.write(reqBodyFrame, promise: nil) + } + reqBodyFrame.payload = .data(.init(data: .byteBuffer(requestBody), endStream: true)) + channel.writeAndFlush(reqBodyFrame, promise: nil) + + return channel.pipeline.addHandler(childHandler) } - reqBodyFrame.payload = .data(.init(data: .byteBuffer(requestBody), endStream: true)) - channel.writeAndFlush(reqBodyFrame, promise: nil) - - return channel.pipeline.addHandler(childHandler) } + (self.clientChannel.eventLoop as! EmbeddedEventLoop).run() // Ok, we now want to send this data to the server. @@ -221,9 +228,9 @@ class SimpleClientServerTests: XCTestCase { // Begin by getting the connection up and add a stream multiplexer. try self.basicHTTP2Connection() XCTAssertNoThrow( - try self.serverChannel.pipeline.addHandler( + try self.serverChannel.pipeline.syncOperations.addHandler( HTTP2StreamMultiplexer(mode: .server, channel: self.serverChannel) - ).wait() + ) ) var largeBuffer = self.clientChannel.allocator.buffer(capacity: 32767) @@ -314,17 +321,22 @@ class SimpleClientServerTests: XCTestCase { ]) // We're going to open a stream and queue up the frames for that stream. - let handler = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).wait() let reqFrame = NIOLockedValueBox(nil) - handler.createStreamChannel(promise: nil) { channel, streamID in - // We need END_STREAM set here, because that will force the stream to be closed on the response. - reqFrame.withLockedValue { reqFrame in - reqFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: headers, endStream: true))) - channel.writeAndFlush(reqFrame, promise: nil) + self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).whenSuccess { multiplexer in + multiplexer.createStreamChannel(promise: nil) { channel, streamID in + // We need END_STREAM set here, because that will force the stream to be closed on the response. + reqFrame.withLockedValue { reqFrame in + reqFrame = HTTP2Frame( + streamID: streamID, + payload: .headers(.init(headers: headers, endStream: true)) + ) + channel.writeAndFlush(reqFrame, promise: nil) + } + return channel.eventLoop.makeSucceededFuture(()) } - return channel.eventLoop.makeSucceededFuture(()) } + self.clientChannel.embeddedEventLoop.run() // Ok, we now want to send this data to the server. @@ -363,23 +375,23 @@ class SimpleClientServerTests: XCTestCase { ]) // We're going to open a stream and queue up the frames for that stream. - let handler = try self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).wait() + self.clientChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self).whenSuccess { multiplexer in + multiplexer.createStreamChannel(promise: nil) { channel, streamID in + let reqFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: headers))) + channel.write(reqFrame, promise: nil) - handler.createStreamChannel(promise: nil) { channel, streamID in - let reqFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: headers))) - channel.write(reqFrame, promise: nil) + var requestBody = channel.allocator.buffer(capacity: 65535) + requestBody.writeBytes(Array(repeating: UInt8(0x04), count: 65535)) - var requestBody = channel.allocator.buffer(capacity: 65535) - requestBody.writeBytes(Array(repeating: UInt8(0x04), count: 65535)) + // Now prepare the large body. We need END_STREAM set. + let reqBodyFrame = HTTP2Frame( + streamID: streamID, + payload: .data(.init(data: .byteBuffer(requestBody), endStream: true)) + ) + channel.writeAndFlush(reqBodyFrame, promise: nil) - // Now prepare the large body. We need END_STREAM set. - let reqBodyFrame = HTTP2Frame( - streamID: streamID, - payload: .data(.init(data: .byteBuffer(requestBody), endStream: true)) - ) - channel.writeAndFlush(reqBodyFrame, promise: nil) - - return channel.pipeline.addHandler(childHandler) + return channel.pipeline.addHandler(childHandler) + } } self.clientChannel.embeddedEventLoop.run() @@ -402,7 +414,7 @@ class SimpleClientServerTests: XCTestCase { func testStreamCreationOrder() throws { try self.basicHTTP2Connection() let multiplexer = HTTP2StreamMultiplexer(mode: .client, channel: self.clientChannel) - XCTAssertNoThrow(try self.clientChannel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.clientChannel.pipeline.syncOperations.addHandler(multiplexer)) let streamAPromise = self.clientChannel.eventLoop.makePromise(of: Channel.self) multiplexer.createStreamChannel(promise: streamAPromise) { channel, _ in diff --git a/Tests/NIOHTTP2Tests/TestUtilities.swift b/Tests/NIOHTTP2Tests/TestUtilities.swift index 09563aa..8a79121 100644 --- a/Tests/NIOHTTP2Tests/TestUtilities.swift +++ b/Tests/NIOHTTP2Tests/TestUtilities.swift @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +import NIOConcurrencyHelpers import NIOCore import NIOEmbedded import NIOHPACK @@ -1341,7 +1342,7 @@ extension Array where Element == HTTP2Frame.FramePayload { /// Runs the body with a temporary file, optionally containing some file content. func withTemporaryFile(content: String? = nil, _ body: (NIOFileHandle, String) throws -> T) rethrows -> T { let (fd, path) = openTemporaryFile() - let fileHandle = NIOFileHandle(descriptor: fd) + let fileHandle = NIOFileHandle(_deprecatedTakingOwnershipOfDescriptor: fd) defer { XCTAssertNoThrow(try fileHandle.close()) XCTAssertEqual(0, unlink(path)) @@ -1504,10 +1505,19 @@ internal func assertThrowsError( } } -final class ErrorEncounteredHandler: ChannelInboundHandler { +final class ErrorEncounteredHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Never - var encounteredError: Error? + private let error = NIOLockedValueBox(nil) + + var encounteredError: Error? { + get { + self.error.withLockedValue { $0 } + } + set { + self.error.withLockedValue { $0 = newValue } + } + } func errorCaught(context: ChannelHandlerContext, error: Error) { self.encounteredError = error