Skip to content

Commit 7860a61

Browse files
authored
Use non-main queue in main actor tests (#43)
1 parent 54c2fae commit 7860a61

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Tests/AsyncQueueTests/MainActorQueueTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import Testing
2828
struct MainActorQueueTests {
2929
// MARK: Behavior Tests
3030

31+
@TestingQueue
3132
@Test func test_shared_returnsSameInstance() async {
3233
#expect(MainActorQueue.shared === MainActorQueue.shared)
3334
}
3435

36+
@TestingQueue
3537
@Test func test_enqueue_executesOnMainThread() async {
3638
let key: DispatchSpecificKey<Void> = .init()
3739
DispatchQueue.main.setSpecific(key: key, value: ())
@@ -41,6 +43,7 @@ struct MainActorQueueTests {
4143
await systemUnderTest.enqueueAndWait { /* Drain the queue */ }
4244
}
4345

46+
@TestingQueue
4447
@Test func test_enqueue_sendsEventsInOrder() async {
4548
for iteration in 1...1_000 {
4649
systemUnderTest.enqueue { [counter] in
@@ -50,6 +53,7 @@ struct MainActorQueueTests {
5053
await systemUnderTest.enqueueAndWait { /* Drain the queue */ }
5154
}
5255

56+
@TestingQueue
5357
@Test func test_enqueue_startsExecutionOfNextTaskAfterSuspension() async {
5458
let semaphore = Semaphore()
5559

@@ -65,6 +69,7 @@ struct MainActorQueueTests {
6569
await systemUnderTest.enqueueAndWait { /* Drain the queue */ }
6670
}
6771

72+
@TestingQueue
6873
@Test func test_enqueueAndWait_executesOnMainThread() async {
6974
let key: DispatchSpecificKey<Void> = .init()
7075
DispatchQueue.main.setSpecific(key: key, value: ())
@@ -73,6 +78,7 @@ struct MainActorQueueTests {
7378
}
7479
}
7580

81+
@TestingQueue
7682
@Test func test_enqueueAndWait_allowsReentrancy() async {
7783
await systemUnderTest.enqueueAndWait { [systemUnderTest, counter] in
7884
await systemUnderTest.enqueueAndWait { [counter] in
@@ -82,6 +88,7 @@ struct MainActorQueueTests {
8288
}
8389
}
8490

91+
@TestingQueue
8592
@Test func test_enqueue_doesNotRetainTaskAfterExecution() async {
8693
final class Reference: Sendable {}
8794
final class ReferenceHolder: @unchecked Sendable {
@@ -128,6 +135,7 @@ struct MainActorQueueTests {
128135
#expect(referenceHolder.weakReference == nil)
129136
}
130137

138+
@TestingQueue
131139
@Test func test_enqueueAndWait_sendsEventsInOrder() async {
132140
for iteration in 1...1_000 {
133141
systemUnderTest.enqueue { [counter] in
@@ -147,12 +155,14 @@ struct MainActorQueueTests {
147155
await systemUnderTest.enqueueAndWait { /* Drain the queue */ }
148156
}
149157

158+
@TestingQueue
150159
@Test func test_enqueueAndWait_canReturn() async {
151160
let expectedValue = UUID()
152161
let returnedValue = await systemUnderTest.enqueueAndWait { expectedValue }
153162
#expect(expectedValue == returnedValue)
154163
}
155164

165+
@TestingQueue
156166
@Test func test_enqueueAndWait_throwing_canReturn() async throws {
157167
let expectedValue = UUID()
158168
@Sendable func throwingMethod() throws {}
@@ -163,6 +173,7 @@ struct MainActorQueueTests {
163173
#expect(expectedValue == returnedValue)
164174
}
165175

176+
@TestingQueue
166177
@Test func test_enqueueAndWait_canThrow() async {
167178
struct TestError: Error, Equatable {
168179
private let identifier = UUID()
@@ -180,3 +191,10 @@ struct MainActorQueueTests {
180191
private let systemUnderTest = MainActorQueue()
181192
private let counter = Counter()
182193
}
194+
195+
/// A global actor that forces the above `MainActorQueueTests` to not run on `main`, where tests may otherwise deadlock due to waiting for `main` from `main`.
196+
@globalActor
197+
private struct TestingQueue {
198+
fileprivate actor Shared {}
199+
fileprivate static let shared = Shared()
200+
}

0 commit comments

Comments
 (0)