Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ jobs:
# flag, which the next step catches.
- name: Build and Test Framework
run: swift test -c release --enable-code-coverage -Xswiftc -enable-testing -Xswiftc -Xllvm -Xswiftc -instrprof-atomic-counter-update-all
# A release build compiles out the tests under `#if DEBUG`, which
# use test hooks that only debug builds have, so run the tests in
# a debug build too.
- name: Test Debug Build
run: swift test
# Fails unless the test binary updates coverage counters and
# every update is atomic. It reads x86_64 code: an atomic update
# follows a `lock` prefix, which llvm-objdump prints on its own
Expand Down
16 changes: 8 additions & 8 deletions Tests/AsyncQueueTests/CancellableQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -856,31 +856,31 @@ struct CancellableQueueTests {
#if DEBUG
@Test
func task_fifoQueue_withMainActorOperation_endsItsTurnOnMainActor() async {
let wasEachTurnEndedOnMainThread = Mutex([Bool]())
let wasEachTurnEndedOnMainQueue = Mutex([Bool]())
let turns = Turns(didEndTurn: {
wasEachTurnEndedOnMainThread.withLock { $0.append(Thread.isMainThread) }
wasEachTurnEndedOnMainQueue.withLock { $0.append(isOnMainQueue) }
})
let task = Task(on: CancellableQueue(underlyingQueue: FIFOQueue(turns: turns))) { @MainActor in
MainActor.assertIsolated()
}
await task.value
// A task that hopped off of the main actor before ending its turn would end it on another thread.
#expect(wasEachTurnEndedOnMainThread.withLock(\.self) == [true])
// A task that hopped off of the main actor before ending its turn would end it off of the main queue.
#expect(wasEachTurnEndedOnMainQueue.withLock(\.self) == [true])
}

@Test
func throwingTask_fifoQueue_withMainActorOperation_endsItsTurnOnMainActor() async throws {
let wasEachTurnEndedOnMainThread = Mutex([Bool]())
let wasEachTurnEndedOnMainQueue = Mutex([Bool]())
let turns = Turns(didEndTurn: {
wasEachTurnEndedOnMainThread.withLock { $0.append(Thread.isMainThread) }
wasEachTurnEndedOnMainQueue.withLock { $0.append(isOnMainQueue) }
})
let task = Task(on: CancellableQueue(underlyingQueue: FIFOQueue(turns: turns))) { @MainActor in
MainActor.assertIsolated()
try doWork()
}
try await task.value
// A task that hopped off of the main actor before ending its turn would end it on another thread.
#expect(wasEachTurnEndedOnMainThread.withLock(\.self) == [true])
// A task that hopped off of the main actor before ending its turn would end it off of the main queue.
#expect(wasEachTurnEndedOnMainQueue.withLock(\.self) == [true])
}
#endif

Expand Down
32 changes: 16 additions & 16 deletions Tests/AsyncQueueTests/FIFOQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1115,38 +1115,38 @@ struct FIFOQueueTests {
#if DEBUG
@Test
func task_withMainActorOperation_whenTurnBeginsWithoutWaiting_endsItsTurnOnMainActor() async {
let wasEachTurnEndedOnMainThread = Mutex([Bool]())
let wasEachTurnEndedOnMainQueue = Mutex([Bool]())
let turns = Turns(didEndTurn: {
wasEachTurnEndedOnMainThread.withLock { $0.append(Thread.isMainThread) }
wasEachTurnEndedOnMainQueue.withLock { $0.append(isOnMainQueue) }
})
let task = Task(on: FIFOQueue(turns: turns)) { @MainActor in
MainActor.assertIsolated()
}
await task.value
// A task that hopped off of the main actor before ending its turn would end it on another thread.
#expect(wasEachTurnEndedOnMainThread.withLock(\.self) == [true])
// A task that hopped off of the main actor before ending its turn would end it off of the main queue.
#expect(wasEachTurnEndedOnMainQueue.withLock(\.self) == [true])
}

@Test
func throwingTask_withMainActorOperation_whenTurnBeginsWithoutWaiting_endsItsTurnOnMainActor() async throws {
let wasEachTurnEndedOnMainThread = Mutex([Bool]())
let wasEachTurnEndedOnMainQueue = Mutex([Bool]())
let turns = Turns(didEndTurn: {
wasEachTurnEndedOnMainThread.withLock { $0.append(Thread.isMainThread) }
wasEachTurnEndedOnMainQueue.withLock { $0.append(isOnMainQueue) }
})
let task = Task(on: FIFOQueue(turns: turns)) { @MainActor in
MainActor.assertIsolated()
try doWork()
}
try await task.value
// A task that hopped off of the main actor before ending its turn would end it on another thread.
#expect(wasEachTurnEndedOnMainThread.withLock(\.self) == [true])
// A task that hopped off of the main actor before ending its turn would end it off of the main queue.
#expect(wasEachTurnEndedOnMainQueue.withLock(\.self) == [true])
}

@Test
func task_withMainActorOperation_whenTurnCannotBeginYet_endsItsTurnOnMainActor() async {
let wasEachTurnEndedOnMainThread = Mutex([Bool]())
let wasEachTurnEndedOnMainQueue = Mutex([Bool]())
let turns = Turns(didEndTurn: {
wasEachTurnEndedOnMainThread.withLock { $0.append(Thread.isMainThread) }
wasEachTurnEndedOnMainQueue.withLock { $0.append(isOnMainQueue) }
})
// Hold the turn before the task's turn, so that the task waits for its turn off of the main actor.
let heldTurn = turns.reserve()
Expand All @@ -1157,15 +1157,15 @@ struct FIFOQueueTests {
heldTurn.end()
await task.value
// The test ends the held turn off of the main actor. A task that hopped off of the main actor before ending its turn
// would end it on another thread too.
#expect(wasEachTurnEndedOnMainThread.withLock(\.self) == [false, true])
// would end it off of the main queue too.
#expect(wasEachTurnEndedOnMainQueue.withLock(\.self) == [false, true])
}

@Test
func throwingTask_withMainActorOperation_whenTurnCannotBeginYet_endsItsTurnOnMainActor() async throws {
let wasEachTurnEndedOnMainThread = Mutex([Bool]())
let wasEachTurnEndedOnMainQueue = Mutex([Bool]())
let turns = Turns(didEndTurn: {
wasEachTurnEndedOnMainThread.withLock { $0.append(Thread.isMainThread) }
wasEachTurnEndedOnMainQueue.withLock { $0.append(isOnMainQueue) }
})
// Hold the turn before the task's turn, so that the task waits for its turn off of the main actor.
let heldTurn = turns.reserve()
Expand All @@ -1177,8 +1177,8 @@ struct FIFOQueueTests {
heldTurn.end()
try await task.value
// The test ends the held turn off of the main actor. A task that hopped off of the main actor before ending its turn
// would end it on another thread too.
#expect(wasEachTurnEndedOnMainThread.withLock(\.self) == [false, true])
// would end it off of the main queue too.
#expect(wasEachTurnEndedOnMainQueue.withLock(\.self) == [false, true])
}
#endif

Expand Down
11 changes: 1 addition & 10 deletions Tests/AsyncQueueTests/Utilities/ManualExecutors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Dispatch
import Synchronization

@testable import AsyncQueue
Expand Down Expand Up @@ -60,7 +59,7 @@ final class ManualTaskExecutor: TaskExecutor {
let job = UnownedJob(job)
let enqueuedJob = EnqueuedJob(
priority: TaskPriority(rawValue: job.priority.rawValue),
wasEnqueuedOnMainQueue: Self.mainQueueKey.withLock { DispatchQueue.getSpecific(key: $0) } == true,
wasEnqueuedOnMainQueue: isOnMainQueue,
)
state.withLock { state in
state.pendingJobs.append(job)
Expand Down Expand Up @@ -106,14 +105,6 @@ final class ManualTaskExecutor: TaskExecutor {
var enqueuedJobs = [EnqueuedJob]()
}

/// A key whose value is `true` on the main dispatch queue, and `nil` on every other queue.
/// A `Mutex` holds the key because it is `Sendable` whether or not `DispatchSpecificKey` is on a given platform.
private static let mainQueueKey = {
let key = DispatchSpecificKey<Bool>()
DispatchQueue.main.setSpecific(key: key, value: true)
return Mutex(key)
}()

private let enqueuedJobsContinuation: AsyncStream<Void>.Continuation
private let state = Mutex(State())
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/AsyncQueueTests/Utilities/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@
// SOFTWARE.

import Dispatch
import Synchronization

/// A task-local value that tests use to check which task-local values an enqueued operation sees.
enum TestTaskLocal {
@TaskLocal static var value = "default"
}

/// Whether the calling code runs on the main dispatch queue.
///
/// The main actor runs its code on the main dispatch queue. On Linux, threads other than the main thread can run the
/// main dispatch queue's work, so code on the main actor doesn't always run on the main thread. Check the queue, not
/// `Thread.isMainThread`.
var isOnMainQueue: Bool {
mainQueueKey.withLock { DispatchQueue.getSpecific(key: $0) } == true
}

/// Returns the priority at which a task executes when `enqueue` creates it from a task with the given priority.
///
/// The task is never awaited, since awaiting it would escalate its priority.
Expand Down Expand Up @@ -102,3 +112,11 @@ actor DispatchQueueActor {

nonisolated private let executor = DispatchQueueExecutor()
}

/// A key whose value is `true` on the main dispatch queue, and `nil` on every other queue.
/// A `Mutex` holds the key because it is `Sendable` whether or not `DispatchSpecificKey` is on a given platform.
private let mainQueueKey = {
let key = DispatchSpecificKey<Bool>()
DispatchQueue.main.setSpecific(key: key, value: true)
return Mutex(key)
}()
Loading