Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

NonBlockingFileIO: tolerate chunk handlers from other ELs #2562

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/NIOPosix/NonBlockingFileIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public struct NonBlockingFileIO: Sendable {
return
}
let bytesRead = Int64(buffer.readableBytes)
chunkHandler(buffer).whenComplete { result in
chunkHandler(buffer).hop(to: eventLoop).whenComplete { result in
switch result {
case .success(_):
eventLoop.assertInEventLoop()
Expand Down
25 changes: 25 additions & 0 deletions Tests/NIOPosixTests/NonBlockingFileIOTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1016,4 +1016,29 @@ class NonBlockingFileIOTest: XCTestCase {
}
})
}

func testChunkedReadingToleratesChunkHandlersWithForeignEventLoops() throws {
let content = "hello"
let contentBytes = Array(content.utf8)
var numCalls = 0
let otherGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! otherGroup.syncShutdownGracefully()
}
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 5)
try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 1,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
XCTAssertTrue(self.eventLoop.inEventLoop)
XCTAssertEqual(1, buf.readableBytes)
XCTAssertEqual(contentBytes[numCalls], buf.readBytes(length: 1)?.first!)
numCalls += 1
return otherGroup.next().makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(content.utf8.count, numCalls)
}
}