diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77668ed..c3589f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Tests/AsyncQueueTests/CancellableQueueTests.swift b/Tests/AsyncQueueTests/CancellableQueueTests.swift index bbedea2..c5b122b 100644 --- a/Tests/AsyncQueueTests/CancellableQueueTests.swift +++ b/Tests/AsyncQueueTests/CancellableQueueTests.swift @@ -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 diff --git a/Tests/AsyncQueueTests/FIFOQueueTests.swift b/Tests/AsyncQueueTests/FIFOQueueTests.swift index 570681a..5eed107 100644 --- a/Tests/AsyncQueueTests/FIFOQueueTests.swift +++ b/Tests/AsyncQueueTests/FIFOQueueTests.swift @@ -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() @@ -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() @@ -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 diff --git a/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift b/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift index 82bddae..7c2389f 100644 --- a/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift +++ b/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift @@ -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 @@ -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) @@ -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() - DispatchQueue.main.setSpecific(key: key, value: true) - return Mutex(key) - }() - private let enqueuedJobsContinuation: AsyncStream.Continuation private let state = Mutex(State()) } diff --git a/Tests/AsyncQueueTests/Utilities/TestHelpers.swift b/Tests/AsyncQueueTests/Utilities/TestHelpers.swift index b437947..8485d7e 100644 --- a/Tests/AsyncQueueTests/Utilities/TestHelpers.swift +++ b/Tests/AsyncQueueTests/Utilities/TestHelpers.swift @@ -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. @@ -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() + DispatchQueue.main.setSpecific(key: key, value: true) + return Mutex(key) +}()