Skip to content

Commit 9062587

Browse files
authored
Merge pull request #4787 from apple/jgrynspan/test_readToEndOfFileAndNotify-concurrency-bug
Fix a concurrency bug in the TestFileHandle.test_readToEndOfFileAndNotify() unit test.
2 parents 461c9a3 + c7f2927 commit 9062587

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

Tests/Foundation/Tests/TestFileHandle.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,16 +481,15 @@ class TestFileHandle : XCTestCase {
481481

482482
func test_readToEndOfFileAndNotify() {
483483
let handle = createFileHandle()
484-
var readSomeData = false
485-
484+
let readSomeData = XCTestExpectation(description: "At least some data must've been read")
486485
let done = expectation(forNotification: FileHandle.readCompletionNotification, object: handle, notificationCenter: .default) { (notification) -> Bool in
487486
guard let data = notification.userInfo?[NSFileHandleNotificationDataItem] as? Data else {
488487
XCTFail("Couldn't find the data in the user info: \(notification)")
489488
return true
490489
}
491490

492491
if !data.isEmpty {
493-
readSomeData = true
492+
readSomeData.fulfill()
494493
handle.readInBackgroundAndNotify()
495494
return false
496495
} else {
@@ -500,8 +499,7 @@ class TestFileHandle : XCTestCase {
500499

501500
handle.readInBackgroundAndNotify()
502501

503-
wait(for: [done], timeout: 10)
504-
XCTAssertTrue(readSomeData, "At least some data must've been read")
502+
wait(for: [readSomeData, done], timeout: 10)
505503
}
506504

507505
func test_readToEndOfFileAndNotify_readError() {

0 commit comments

Comments
 (0)