diff --git a/Sources/HTTPServerWithQuiescingDemo/main.swift b/Sources/HTTPServerWithQuiescingDemo/main.swift index 491fc241..f9da3985 100644 --- a/Sources/HTTPServerWithQuiescingDemo/main.swift +++ b/Sources/HTTPServerWithQuiescingDemo/main.swift @@ -31,8 +31,9 @@ private final class HTTPHandler: ChannelInboundHandler { self.wrapOutboundOut(.head(HTTPResponseHead(version: head.version, status: .badRequest))), promise: nil ) + let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop) context.writeAndFlush(self.wrapOutboundOut(.end(nil))).whenComplete { (_: Result<(), Error>) in - context.close(promise: nil) + loopBoundContext.value.close(promise: nil) } return } @@ -59,10 +60,10 @@ private final class HTTPHandler: ChannelInboundHandler { context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) buffer.clear() buffer.writeStaticString("done with the request now\n") + let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop) _ = context.eventLoop.scheduleTask(in: .seconds(30)) { [buffer] in - context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) - context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) - + loopBoundContext.value.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) + loopBoundContext.value.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) } } } diff --git a/Sources/NIOExtras/HTTP1ProxyConnectHandler.swift b/Sources/NIOExtras/HTTP1ProxyConnectHandler.swift index 8ee5fe9c..eb9dc2fd 100644 --- a/Sources/NIOExtras/HTTP1ProxyConnectHandler.swift +++ b/Sources/NIOExtras/HTTP1ProxyConnectHandler.swift @@ -162,13 +162,14 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC return } + let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop) let timeout = context.eventLoop.scheduleTask(deadline: self.deadline) { switch self.state { case .initialized: preconditionFailure("How can we have a scheduled timeout, if the connection is not even up?") case .connectSent, .headReceived: - self.failWithError(Error.httpProxyHandshakeTimeout(), context: context) + self.failWithError(Error.httpProxyHandshakeTimeout(), context: loopBoundContext.value) case .failed, .completed: break diff --git a/Sources/NIOWritePartialPCAPDemo/main.swift b/Sources/NIOWritePartialPCAPDemo/main.swift index f7b94774..abcddc9f 100644 --- a/Sources/NIOWritePartialPCAPDemo/main.swift +++ b/Sources/NIOWritePartialPCAPDemo/main.swift @@ -130,21 +130,22 @@ let allDonePromise = group.next().makePromise(of: Void.self) let maximumFragments = 4 let connection = try ClientBootstrap(group: group.next()) .channelInitializer { channel in - let pcapRingBuffer = NIOPCAPRingBuffer( - maximumFragments: maximumFragments, - maximumBytes: 1_000_000 - ) - return channel.pipeline.addHandler( - NIOWritePCAPHandler( - mode: .client, - fileSink: pcapRingBuffer.addFragment + channel.eventLoop.makeCompletedFuture { + let pcapRingBuffer = NIOPCAPRingBuffer( + maximumFragments: maximumFragments, + maximumBytes: 1_000_000 ) - ).flatMap { - channel.pipeline.addHTTPClientHandlers() - }.flatMap { - channel.pipeline.addHandler(TriggerPCAPHandler(pcapRingBuffer: pcapRingBuffer, sink: fileSink.write)) - }.flatMap { - channel.pipeline.addHandler(SendSimpleSequenceRequestHandler(allDonePromise: allDonePromise)) + try channel.pipeline.syncOperations.addHandler( + NIOWritePCAPHandler( + mode: .client, + fileSink: pcapRingBuffer.addFragment + ) + ) + try channel.pipeline.syncOperations.addHTTPClientHandlers() + try channel.pipeline.syncOperations.addHandlers([ + TriggerPCAPHandler(pcapRingBuffer: pcapRingBuffer, sink: fileSink.write), + SendSimpleSequenceRequestHandler(allDonePromise: allDonePromise), + ]) } } .connect(host: "httpbin.org", port: 80) diff --git a/Tests/NIOExtrasTests/PCAPRingBufferTest.swift b/Tests/NIOExtrasTests/PCAPRingBufferTest.swift index 7534fadc..1674ba9a 100644 --- a/Tests/NIOExtrasTests/PCAPRingBufferTest.swift +++ b/Tests/NIOExtrasTests/PCAPRingBufferTest.swift @@ -204,7 +204,6 @@ class PCAPRingBufferTest: XCTestCase { ) } - let channel = EmbeddedChannel() let trigger = self.dataForTests()[0.. EventLoopFuture in - let triggerHandler = TriggerOnCumulativeSizeHandler( - triggerBytes: trigger, - pcapRingBuffer: pcapRingBuffer, - sink: testRecordedBytes - ) - return channel.pipeline.addHandler(triggerHandler, name: "trigger") - } - XCTAssertNoThrow(try addHandlers.wait()) + ]) channel.localAddress = try! SocketAddress(ipAddress: "255.255.255.254", port: Int(UInt16.max) - 1) XCTAssertNoThrow(try channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5678)).wait()) diff --git a/Tests/NIOExtrasTests/SynchronizedFileSinkTests.swift b/Tests/NIOExtrasTests/SynchronizedFileSinkTests.swift index 9a9ce277..5f806322 100644 --- a/Tests/NIOExtrasTests/SynchronizedFileSinkTests.swift +++ b/Tests/NIOExtrasTests/SynchronizedFileSinkTests.swift @@ -67,7 +67,7 @@ private func withTemporaryFile( _ body: (NIOCore.NIOFileHandle, String) throws -> T ) throws -> T { let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())" - FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)) + XCTAssertTrue(FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8))) defer { XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath)) } @@ -86,7 +86,7 @@ private func withTemporaryFile( _ body: (NIOCore.NIOFileHandle, String) async throws -> T ) async throws -> T { let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())" - FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)) + XCTAssertTrue(FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8))) defer { XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath)) }