@@ -12,12 +12,13 @@ A library of queues that enable sending ordered tasks from nonisolated to asynch
1212Tasks sent from a nonisolated context to an asynchronous context in Swift Concurrency are inherently unordered. Consider the following test:
1313
1414``` swift
15- func testActorTaskOrdering () async {
15+ @Test
16+ func actorTaskOrdering () async {
1617 actor Counter {
1718 func incrementAndAssertCountEquals (_ expectedCount : Int ) {
1819 count += 1
1920 let incrementedCount = count
20- XCTAssertEqual (incrementedCount, expectedCount) // often fails
21+ #expect (incrementedCount == expectedCount) // often fails
2122 }
2223
2324 private var count = 0
@@ -47,36 +48,35 @@ Use a `FIFOQueue` to execute asynchronous tasks enqueued from a nonisolated cont
4748
4849A ` FIFOQueue ` can easily execute asynchronous tasks from a nonisolated context in FIFO order:
4950``` swift
50- func testFIFOQueueOrdering () async {
51+ @Test
52+ func fIFOQueueOrdering () async {
5153 actor Counter {
5254 nonisolated
53- func incrementAndAssertCountEquals (_ expectedCount : Int ) {
55+ func incrementAndAssertCountEquals (_ expectedCount : Int ) -> Task< Void , Never > {
5456 Task (on : queue) {
5557 await self .increment ()
5658 let incrementedCount = await self .count
57- XCTAssertEqual (incrementedCount, expectedCount) // always succeeds
59+ #expect (incrementedCount == expectedCount) // always succeeds
5860 }
5961 }
6062
61- func flushQueue () async {
62- await Task (on : queue) {}.value
63- }
64-
6563 func increment () {
6664 count += 1
6765 }
6866
69- var count = 0
70-
67+ private var count = 0
7168 private let queue = FIFOQueue ()
7269 }
7370
7471 let counter = Counter ()
72+ var tasks = [Task< Void , Never > ]()
7573 for iteration in 1 ... 100 {
76- counter.incrementAndAssertCountEquals (iteration)
74+ tasks. append ( counter.incrementAndAssertCountEquals (iteration) )
7775 }
7876 // Wait for all enqueued tasks to finish.
79- await counter.flushQueue ()
77+ for task in tasks {
78+ _ = await task.value
79+ }
8080}
8181```
8282
@@ -92,36 +92,36 @@ An instance of an `ActorQueue` is designed to be utilized by a single `actor` in
9292
9393An ` ActorQueue ` can easily enqueue tasks that execute on an actor’s isolated context from a nonisolated context in order:
9494``` swift
95- func testActorQueueOrdering () async {
95+ @Test
96+ func actorQueueOrdering () async {
9697 actor Counter {
9798 init () {
9899 // Adopting the execution context in `init` satisfies requirement #2 above.
99100 queue.adoptExecutionContext (of : self )
100101 }
101102
102103 nonisolated
103- func incrementAndAssertCountEquals (_ expectedCount : Int ) {
104- await Task (on : queue) { myself in
104+ func incrementAndAssertCountEquals (_ expectedCount : Int ) -> Task< Void , Never > {
105+ Task (on : queue) { myself in
105106 myself.count += 1
106- XCTAssertEqual (expectedCount, myself.count ) // always succeeds
107+ #expect (expectedCount == myself.count ) // always succeeds
107108 }
108109 }
109110
110- func flushQueue () async {
111- await Task (on : queue) {}.value
112- }
113-
114111 private var count = 0
115112 // Making the queue a private let constant satisfies requirement #1 above.
116113 private let queue = ActorQueue< Counter> ()
117114 }
118115
119116 let counter = Counter ()
117+ var tasks = [Task< Void , Never > ]()
120118 for iteration in 1 ... 100 {
121- counter.incrementAndAssertCountEquals (iteration)
119+ tasks. append ( counter.incrementAndAssertCountEquals (iteration) )
122120 }
123121 // Wait for all enqueued tasks to finish.
124- await counter.flushQueue ()
122+ for task in tasks {
123+ _ = await task.value
124+ }
125125}
126126```
127127
@@ -132,35 +132,35 @@ Use `MainActor.queue` to send ordered asynchronous tasks to the `@MainActor`’s
132132A ` MainActor.queue ` can easily execute asynchronous tasks from a nonisolated context in FIFO order:
133133``` swift
134134@MainActor
135- func testMainActorQueueOrdering () async {
135+ @Test
136+ func mainActorQueueOrdering () async {
136137 @MainActor
137138 final class Counter {
138139 nonisolated
139- func incrementAndAssertCountEquals (_ expectedCount : Int ) {
140+ func incrementAndAssertCountEquals (_ expectedCount : Int ) -> Task< Void , Never > {
140141 Task (on : MainActor.queue ) {
141142 self .increment ()
142143 let incrementedCount = self .count
143- XCTAssertEqual (incrementedCount, expectedCount) // always succeeds
144+ #expect (incrementedCount == expectedCount) // always succeeds
144145 }
145146 }
146147
147- func flushQueue () async {
148- await Task (on : MainActor.queue ) { }.value
149- }
150-
151148 func increment () {
152149 count += 1
153150 }
154151
155- var count = 0
152+ private var count = 0
156153 }
157154
158155 let counter = Counter ()
156+ var tasks = [Task< Void , Never > ]()
159157 for iteration in 1 ... 100 {
160- counter.incrementAndAssertCountEquals (iteration)
158+ tasks. append ( counter.incrementAndAssertCountEquals (iteration) )
161159 }
162160 // Wait for all enqueued tasks to finish.
163- await counter.flushQueue ()
161+ for task in tasks {
162+ _ = await task.value
163+ }
164164}
165165```
166166
0 commit comments