Skip to content
Open
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
3 changes: 1 addition & 2 deletions Sources/Examples/DispatchQueue+PendingWork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Dispatch
extension DispatchQueue {
/// Returns once any pending work has been completed.
func pendingWorkComplete() async {
// TODO: update to withCheckedContinuation https://github.com/apple/swift/issues/74206
await withUnsafeContinuation { continuation in
await withCheckedContinuation { continuation in
self.async(flags: .barrier) {
continuation.resume()
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/Examples/DispatchQueuePendingWorkTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import XCTest

final class DispatchQueuePendingWorkTests: XCTestCase {
func testPendingWorkCompleteRunsAfterAsyncBlock() async {
let queue = DispatchQueue(label: "test.queue", attributes: .concurrent)
let expectation = XCTestExpectation(description: "Work finished")
var value = 0

// Enqueue some async work on the queue
queue.async {
value = 42
expectation.fulfill()
}

// Wait until the queue says all prior work has completed
await queue.pendingWorkComplete()

// At this point, value should be updated
XCTAssertEqual(value, 42)

// Make sure the async block really executed
wait(for: [expectation], timeout: 1.0)
}
}