From 8d993e8045752b6162471333174317cc40e0961f Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 26 Jun 2023 18:17:58 -0400 Subject: [PATCH] Merge pull request #4787 from apple/jgrynspan/test_readToEndOfFileAndNotify-concurrency-bug Fix a concurrency bug in the TestFileHandle.test_readToEndOfFileAndNotify() unit test. --- Tests/Foundation/Tests/TestFileHandle.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Tests/Foundation/Tests/TestFileHandle.swift b/Tests/Foundation/Tests/TestFileHandle.swift index 09879e4572..67618ea858 100644 --- a/Tests/Foundation/Tests/TestFileHandle.swift +++ b/Tests/Foundation/Tests/TestFileHandle.swift @@ -481,8 +481,7 @@ class TestFileHandle : XCTestCase { func test_readToEndOfFileAndNotify() { let handle = createFileHandle() - var readSomeData = false - + let readSomeData = XCTestExpectation(description: "At least some data must've been read") let done = expectation(forNotification: FileHandle.readCompletionNotification, object: handle, notificationCenter: .default) { (notification) -> Bool in guard let data = notification.userInfo?[NSFileHandleNotificationDataItem] as? Data else { XCTFail("Couldn't find the data in the user info: \(notification)") @@ -490,7 +489,7 @@ class TestFileHandle : XCTestCase { } if !data.isEmpty { - readSomeData = true + readSomeData.fulfill() handle.readInBackgroundAndNotify() return false } else { @@ -500,8 +499,7 @@ class TestFileHandle : XCTestCase { handle.readInBackgroundAndNotify() - wait(for: [done], timeout: 10) - XCTAssertTrue(readSomeData, "At least some data must've been read") + wait(for: [readSomeData, done], timeout: 10) } func test_readToEndOfFileAndNotify_readError() {