Skip to content

Commit

Permalink
Sendability warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rnro committed Oct 28, 2024
1 parent 3a47b8e commit c198c58
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
9 changes: 5 additions & 4 deletions Sources/HTTPServerWithQuiescingDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOExtras/HTTP1ProxyConnectHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 15 additions & 14 deletions Sources/NIOWritePartialPCAPDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 8 additions & 15 deletions Tests/NIOExtrasTests/PCAPRingBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ class PCAPRingBufferTest: XCTestCase {
)
}

let channel = EmbeddedChannel()
let trigger = self.dataForTests()[0..<triggerEndIndex]
.compactMap { t in t.readableBytes }.reduce(0, +)

Expand All @@ -213,20 +212,14 @@ class PCAPRingBufferTest: XCTestCase {
maximumBytes: 1_000_000
)

let addHandlers =
channel.pipeline.addHandler(
NIOWritePCAPHandler(mode: .client, fileSink: pcapRingBuffer.addFragment),
name: "capture"
)
.flatMap { () -> EventLoopFuture<Void> in
let triggerHandler = TriggerOnCumulativeSizeHandler(
triggerBytes: trigger,
pcapRingBuffer: pcapRingBuffer,
sink: testRecordedBytes
)
return channel.pipeline.addHandler(triggerHandler, name: "trigger")
}
XCTAssertNoThrow(try addHandlers.wait())
let channel = EmbeddedChannel(handlers: [
NIOWritePCAPHandler(mode: .client, fileSink: pcapRingBuffer.addFragment),
TriggerOnCumulativeSizeHandler(
triggerBytes: trigger,
pcapRingBuffer: pcapRingBuffer,
sink: testRecordedBytes
),
])

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())
Expand Down
4 changes: 2 additions & 2 deletions Tests/NIOExtrasTests/SynchronizedFileSinkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private func withTemporaryFile<T>(
_ 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))
}
Expand All @@ -86,7 +86,7 @@ private func withTemporaryFile<T>(
_ 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))
}
Expand Down

0 comments on commit c198c58

Please sign in to comment.