Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implement FileHandle#waitForDataInBackgroundAndNotify #1427

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion Foundation/FileHandle.swift
Expand Up @@ -8,6 +8,7 @@
//

import CoreFoundation
import Dispatch

#if os(OSX) || os(iOS)
import Darwin
Expand Down Expand Up @@ -345,7 +346,19 @@ extension FileHandle {
}

open func waitForDataInBackgroundAndNotify() {
NSUnimplemented()
let global = DispatchQueue.global(qos: .background)
let channel = DispatchIO(type: .stream, fileDescriptor: fileDescriptor, queue: global) { _ in }

// The desire is to trigger the handler blocka as soon as there is any data in the channel.
channel.setLimit(lowWater: 1)

channel.read(offset: 0, length: Int.max, queue: global) { (_, _, error) in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this reads all the data for no reason
  • also if this is a socket/pipe this is a permanent leak of a file descriptor
  • you could just use a DispatchSource.makeReadSource and get the notifications but not the reading. As soon as you get the first notification you then want to .cancel the DispatchSource as you only want to get notified once.

var notification = Notification(name: .NSFileHandleDataAvailable, object: nil, userInfo: nil)
if error != 0 {
notification.userInfo = ["NSFileHandleError": NSNumber(value: error)]
}
NotificationCenter.default.post(notification)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't that need to happen on the main thread? CC @parkera

}
}

open var readabilityHandler: ((FileHandle) -> Void)? {
Expand Down
18 changes: 18 additions & 0 deletions TestFoundation/TestFileHandle.swift
Expand Up @@ -21,6 +21,7 @@ class TestFileHandle : XCTestCase {
("test_constants", test_constants),
("test_pipe", test_pipe),
("test_nullDevice", test_nullDevice),
("test_waitForDataInBackgroundAndNotify", test_waitForDataInBackgroundAndNotify),
]
}

Expand Down Expand Up @@ -64,4 +65,21 @@ class TestFileHandle : XCTestCase {
fh.seek(toFileOffset: 0)
XCTAssertEqual(fh.readDataToEndOfFile().count, 0)
}

func test_waitForDataInBackgroundAndNotify() {
let expect = expectation(description: "Receiving asynchronous data from pipe")
let pipe = Pipe()
pipe.fileHandleForReading.waitForDataInBackgroundAndNotify()

NotificationCenter.default.addObserver(forName: .NSFileHandleDataAvailable, object: nil, queue: nil) { (notification) in
XCTAssertEqual(notification.name, .NSFileHandleDataAvailable)
XCTAssertNil(notification.userInfo)
expect.fulfill()
}

let data = Data(bytes: [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21])
pipe.fileHandleForWriting.write(data)

waitForExpectations(timeout: 2)
}
}