mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Added write function to NonBlockingFileIO (#531)
Motivation: `NonBlockingFileIO` currently provides multiple read functions, but no functions to write to a `FileHandle` in an asynchronous non-blocking way. Modifications: `NonBlockingFileIO` struct now has simple basic `write` function that supports writing from a `ByteBuffer` to a `FileHandle`. Added `testWriting()` to `NonBlockingFileIOTest`. Result: `NonBlockingFileIO` now has a basic building block for asynchronous non-blocking writes. Resolves #406.
This commit is contained in:
1 parent
3913771129
commit
0bc206feaf
3 files changed
+62
No files matched your search
@@ -214,6 +214,46 @@ public struct NonBlockingFileIO {
|
||||
}
|
||||
}
|
||||
|
||||
/// Write `buffer` to `fileHandle` in `NonBlockingFileIO`'s private thread pool which is separate from any `EventLoop` thread.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - fileHandle: The `FileHandle` to write to.
|
||||
/// - buffer: The `ByteBuffer` to write.
|
||||
/// - eventLoop: The `EventLoop` to create the returned `EventLoopFuture` from.
|
||||
/// - returns: An `EventLoopFuture` which is fulfilled if the write was successful or fails on error.
|
||||
public func write(fileHandle: FileHandle,
|
||||
buffer: ByteBuffer,
|
||||
eventLoop: EventLoop) -> EventLoopFuture<()> {
|
||||
var byteCount = buffer.readableBytes
|
||||
|
||||
guard byteCount > 0 else {
|
||||
return eventLoop.newSucceededFuture(result: ())
|
||||
}
|
||||
|
||||
return self.threadPool.runIfActive(eventLoop: eventLoop) {
|
||||
var buf = buffer
|
||||
while byteCount > 0 {
|
||||
let n = try buf.readWithUnsafeReadableBytes { ptr in
|
||||
precondition(ptr.count == byteCount)
|
||||
let res = try fileHandle.withUnsafeFileDescriptor { descriptor in
|
||||
try Posix.write(descriptor: descriptor,
|
||||
pointer: ptr.baseAddress!,
|
||||
size: byteCount)
|
||||
}
|
||||
switch res {
|
||||
case .processed(let n):
|
||||
assert(n >= 0, "write claims to have written a negative number of bytes \(n)")
|
||||
return n
|
||||
case .wouldBlock:
|
||||
throw Error.descriptorSetToNonBlocking
|
||||
}
|
||||
}
|
||||
|
||||
byteCount -= n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Open the file at `path` on a private thread pool which is separate from any `EventLoop` thread.
|
||||
///
|
||||
/// This function will return (a future) of the `FileHandle` associated with the file opened and a `FileRegion`
|
||||
|
||||
@@ -45,6 +45,7 @@ extension NonBlockingFileIOTest {
|
||||
("testFileRegionReadFromPipeFails", testFileRegionReadFromPipeFails),
|
||||
("testReadFromNonBlockingPipeFails", testReadFromNonBlockingPipeFails),
|
||||
("testSeekPointerIsSetToFront", testSeekPointerIsSetToFront),
|
||||
("testWriting", testWriting),
|
||||
("testFileOpenWorks", testFileOpenWorks),
|
||||
("testFileOpenWorksWithEmptyFile", testFileOpenWorksWithEmptyFile),
|
||||
("testFileOpenFails", testFileOpenFails),
|
||||
|
||||
@@ -450,6 +450,27 @@ class NonBlockingFileIOTest: XCTestCase {
|
||||
XCTAssertEqual(2, numCalls)
|
||||
}
|
||||
|
||||
func testWriting() throws {
|
||||
var buffer = allocator.buffer(capacity: 3)
|
||||
buffer.write(staticString: "123")
|
||||
|
||||
try withTemporaryFile(content: "") { (fileHandle, path) in
|
||||
try self.fileIO.write(fileHandle: fileHandle,
|
||||
buffer: buffer,
|
||||
eventLoop: self.eventLoop).wait()
|
||||
let offset = try fileHandle.withUnsafeFileDescriptor {
|
||||
try Posix.lseek(descriptor: $0, offset: 0, whence: SEEK_SET)
|
||||
}
|
||||
XCTAssertEqual(offset, 0)
|
||||
|
||||
let readBuffer = try self.fileIO.read(fileHandle: fileHandle,
|
||||
byteCount: 3,
|
||||
allocator: self.allocator,
|
||||
eventLoop: self.eventLoop).wait()
|
||||
XCTAssertEqual(readBuffer.getString(at: 0, length: 3), "123")
|
||||
}
|
||||
}
|
||||
|
||||
func testFileOpenWorks() throws {
|
||||
let content = "123"
|
||||
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
|
||||
|
||||
Reference in new issue
Block a user