20
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
// SOFTWARE.
22
22
23
- import XCTest
23
+ import Foundation
24
+ import Testing
24
25
25
26
@testable import AsyncQueue
26
27
27
- final class ActorQueueTests : XCTestCase {
28
+ struct ActorQueueTests {
28
29
29
- // MARK: XCTestCase
30
-
31
- override func setUp( ) async throws {
32
- try await super. setUp ( )
30
+ // MARK: Initialization
33
31
32
+ init ( ) {
34
33
systemUnderTest = ActorQueue < Counter > ( )
35
34
counter = Counter ( )
36
35
systemUnderTest. adoptExecutionContext ( of: counter)
37
36
}
38
37
39
38
// MARK: Behavior Tests
40
39
41
- func test_adoptExecutionContext_doesNotRetainActor( ) {
40
+ @ Test func test_adoptExecutionContext_doesNotRetainActor( ) {
42
41
let systemUnderTest = ActorQueue < Counter > ( )
43
42
var counter : Counter ? = Counter ( )
44
43
weak var weakCounter = counter
45
44
systemUnderTest. adoptExecutionContext ( of: counter!)
46
45
counter = nil
47
- XCTAssertNil ( weakCounter)
46
+ #expect ( weakCounter == nil )
48
47
}
49
48
50
- func test_enqueue_retainsAdoptedActorUntilEnqueuedTasksComplete( ) async {
49
+ @ Test func test_enqueue_retainsAdoptedActorUntilEnqueuedTasksComplete( ) async {
51
50
let systemUnderTest = ActorQueue < Counter > ( )
52
51
var counter : Counter ? = Counter ( )
53
52
weak var weakCounter = counter
@@ -59,27 +58,27 @@ final class ActorQueueTests: XCTestCase {
59
58
}
60
59
61
60
counter = nil
62
- XCTAssertNotNil ( weakCounter)
61
+ #expect ( weakCounter != nil )
63
62
await semaphore. signal ( )
64
63
}
65
64
66
- func test_enqueue_taskParameterIsAdoptedActor( ) async {
65
+ @ Test func test_enqueue_taskParameterIsAdoptedActor( ) async {
67
66
let semaphore = Semaphore ( )
68
67
systemUnderTest. enqueue { [ storedCounter = counter] counter in
69
- XCTAssertTrue ( counter === storedCounter)
68
+ #expect ( counter === storedCounter)
70
69
await semaphore. signal ( )
71
70
}
72
71
73
72
await semaphore. wait ( )
74
73
}
75
74
76
- func test_enqueueAndWait_taskParameterIsAdoptedActor( ) async {
75
+ @ Test func test_enqueueAndWait_taskParameterIsAdoptedActor( ) async {
77
76
await systemUnderTest. enqueueAndWait { [ storedCounter = counter] counter in
78
- XCTAssertTrue ( counter === storedCounter)
77
+ #expect ( counter === storedCounter)
79
78
}
80
79
}
81
80
82
- func test_enqueue_sendsEventsInOrder( ) async {
81
+ @ Test func test_enqueue_sendsEventsInOrder( ) async {
83
82
for iteration in 1 ... 1_000 {
84
83
systemUnderTest. enqueue { counter in
85
84
counter. incrementAndExpectCount ( equals: iteration)
@@ -88,7 +87,7 @@ final class ActorQueueTests: XCTestCase {
88
87
await systemUnderTest. enqueueAndWait { _ in /* Drain the queue */ }
89
88
}
90
89
91
- func test_enqueue_startsExecutionOfNextTaskAfterSuspension( ) async {
90
+ @ Test func test_enqueue_startsExecutionOfNextTaskAfterSuspension( ) async {
92
91
let systemUnderTest = ActorQueue < Semaphore > ( )
93
92
let semaphore = Semaphore ( )
94
93
systemUnderTest. adoptExecutionContext ( of: semaphore)
@@ -105,7 +104,7 @@ final class ActorQueueTests: XCTestCase {
105
104
await systemUnderTest. enqueueAndWait { _ in /* Drain the queue */ }
106
105
}
107
106
108
- func test_enqueueAndWait_allowsReentrancy( ) async {
107
+ @ Test func test_enqueueAndWait_allowsReentrancy( ) async {
109
108
await systemUnderTest. enqueueAndWait { [ systemUnderTest] counter in
110
109
await systemUnderTest. enqueueAndWait { counter in
111
110
counter. incrementAndExpectCount ( equals: 1 )
@@ -114,11 +113,11 @@ final class ActorQueueTests: XCTestCase {
114
113
}
115
114
}
116
115
117
- func test_enqueue_executesEnqueuedTasksAfterReceiverIsDeallocated( ) async {
116
+ @ Test func test_enqueue_executesEnqueuedTasksAfterReceiverIsDeallocated( ) async {
118
117
var systemUnderTest : ActorQueue < Counter > ? = ActorQueue ( )
119
118
systemUnderTest? . adoptExecutionContext ( of: counter)
120
119
121
- let expectation = self . expectation ( description : #function )
120
+ let expectation = Expectation ( )
122
121
let semaphore = Semaphore ( )
123
122
systemUnderTest? . enqueue { counter in
124
123
// Make the task wait.
@@ -129,14 +128,13 @@ final class ActorQueueTests: XCTestCase {
129
128
weak var queue = systemUnderTest
130
129
// Nil out our reference to the queue to show that the enqueued tasks will still complete
131
130
systemUnderTest = nil
132
- XCTAssertNil ( queue)
131
+ #expect ( queue == nil )
133
132
// Signal the semaphore to unlock the enqueued tasks.
134
133
await semaphore. signal ( )
135
-
136
- await fulfillment ( of: [ expectation] , timeout: 1.0 )
134
+ await expectation. fulfillment ( withinSeconds: 10 )
137
135
}
138
136
139
- func test_enqueue_doesNotRetainTaskAfterExecution( ) async {
137
+ @ Test func test_enqueue_doesNotRetainTaskAfterExecution( ) async {
140
138
final class Reference : Sendable { }
141
139
final class ReferenceHolder : @unchecked Sendable {
142
140
init ( ) {
@@ -156,7 +154,7 @@ final class ActorQueueTests: XCTestCase {
156
154
let systemUnderTest = ActorQueue < Semaphore > ( )
157
155
systemUnderTest. adoptExecutionContext ( of: syncSemaphore)
158
156
159
- let expectation = self . expectation ( description : #function )
157
+ let expectation = Expectation ( )
160
158
systemUnderTest. enqueue { [ reference = referenceHolder. reference] syncSemaphore in
161
159
// Now that we've started the task and captured the reference, release the synchronous code.
162
160
syncSemaphore. signal ( )
@@ -173,16 +171,16 @@ final class ActorQueueTests: XCTestCase {
173
171
// Wait for the asynchronous task to start.
174
172
await syncSemaphore. wait ( )
175
173
referenceHolder. clearReference ( )
176
- XCTAssertNotNil ( referenceHolder. weakReference)
174
+ #expect ( referenceHolder. weakReference != nil )
177
175
// Allow the enqueued task to complete.
178
176
await asyncSemaphore. signal ( )
179
177
// Make sure the task has completed.
180
- await fulfillment ( of : [ expectation ] , timeout : 1.0 )
178
+ await expectation . fulfillment ( withinSeconds : 10 )
181
179
182
- XCTAssertNil ( referenceHolder. weakReference)
180
+ #expect ( referenceHolder. weakReference == nil )
183
181
}
184
182
185
- func test_enqueueAndWait_sendsEventsInOrder( ) async {
183
+ @ Test func test_enqueueAndWait_sendsEventsInOrder( ) async {
186
184
for iteration in 1 ... 1_000 {
187
185
systemUnderTest. enqueue { counter in
188
186
counter. incrementAndExpectCount ( equals: iteration)
@@ -194,32 +192,32 @@ final class ActorQueueTests: XCTestCase {
194
192
}
195
193
196
194
await systemUnderTest. enqueueAndWait { counter in
197
- XCTAssertEqual ( counter. count, iteration)
195
+ #expect ( counter. count == iteration)
198
196
}
199
197
}
200
198
await systemUnderTest. enqueueAndWait { _ in /* Drain the queue */ }
201
199
}
202
200
203
- func test_enqueueAndWait_canReturn( ) async {
201
+ @ Test func test_enqueueAndWait_canReturn( ) async {
204
202
let expectedValue = UUID ( )
205
203
let returnedValue = await systemUnderTest. enqueueAndWait { _ in expectedValue }
206
- XCTAssertEqual ( expectedValue, returnedValue)
204
+ #expect ( expectedValue == returnedValue)
207
205
}
208
206
209
- func test_enqueueAndWait_canThrow( ) async {
207
+ @ Test func test_enqueueAndWait_canThrow( ) async {
210
208
struct TestError : Error , Equatable {
211
209
private let identifier = UUID ( )
212
210
}
213
211
let expectedError = TestError ( )
214
212
do {
215
213
try await systemUnderTest. enqueueAndWait { _ in throw expectedError }
216
214
} catch {
217
- XCTAssertEqual ( error as? TestError , expectedError)
215
+ #expect ( error as? TestError == expectedError)
218
216
}
219
217
}
220
218
221
219
// MARK: Private
222
220
223
- private var systemUnderTest = ActorQueue < Counter > ( )
224
- private var counter = Counter ( )
221
+ private let systemUnderTest : ActorQueue < Counter >
222
+ private let counter : Counter
225
223
}
0 commit comments