Skip to content

Commit 1ecda35

Browse files
authored
async -> enqueue. await -> enqueueAndWait (#12)
1 parent 8b6a898 commit 1ecda35

File tree

5 files changed

+119
-119
lines changed

5 files changed

+119
-119
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func testFIFOQueueOrdering() async {
5353
actor Counter {
5454
nonisolated
5555
func incrementAndAssertCountEquals(_ expectedCount: Int) {
56-
queue.async {
56+
queue.enqueue {
5757
await self.increment()
5858
let incrementedCount = await self.count
5959
XCTAssertEqual(incrementedCount, expectedCount) // always succeeds
@@ -62,7 +62,7 @@ func testFIFOQueueOrdering() async {
6262

6363
nonisolated
6464
func flushQueue() async {
65-
await queue.await { }
65+
await queue.enqueueAndWait { }
6666
}
6767

6868
func increment() {
@@ -104,15 +104,15 @@ func testActorQueueOrdering() async {
104104

105105
nonisolated
106106
func incrementAndAssertCountEquals(_ expectedCount: Int) {
107-
queue.async { myself in
107+
queue.enqueue { myself in
108108
myself.count += 1
109109
XCTAssertEqual(expectedCount, myself.count) // always succeeds
110110
}
111111
}
112112

113113
nonisolated
114114
func flushQueue() async {
115-
await queue.await { _ in }
115+
await queue.enqueueAndWait { _ in }
116116
}
117117

118118
private var count = 0

Sources/AsyncQueue/ActorQueue.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
///
3636
/// nonisolated
3737
/// public func log(_ message: String) {
38-
/// queue.async { myself in
38+
/// queue.enqueue { myself in
3939
/// myself.logs.append(message)
4040
/// }
4141
/// }
4242
///
4343
/// nonisolated
4444
/// public func retrieveLogs() async -> [String] {
45-
/// await queue.await { myself in myself.logs }
45+
/// await queue.enqueueAndWait { myself in myself.logs }
4646
/// }
4747
///
4848
/// private let queue = ActorQueue<LogStore>()
@@ -78,7 +78,7 @@ public final class ActorQueue<ActorType: Actor> {
7878

7979
// MARK: Public
8080

81-
/// Sets the actor context within which each `async` and `await`ed task will execute.
81+
/// Sets the actor context within which each `enqueue` and `enqueueAndWait` task will execute.
8282
/// It is recommended that this method be called in the adopted actor’s `init` method.
8383
/// **Must be called prior to enqueuing any work on the receiver.**
8484
///
@@ -92,15 +92,15 @@ public final class ActorQueue<ActorType: Actor> {
9292
/// Schedules an asynchronous task for execution and immediately returns.
9393
/// The scheduled task will not execute until all prior tasks have completed or suspended.
9494
/// - Parameter task: The task to enqueue. The task's parameter is a reference to the actor whose execution context has been adopted.
95-
public func async(_ task: @escaping @Sendable (isolated ActorType) async -> Void) {
95+
public func enqueue(_ task: @escaping @Sendable (isolated ActorType) async -> Void) {
9696
taskStreamContinuation.yield(ActorTask(executionContext: executionContext, task: task))
9797
}
9898

9999
/// Schedules an asynchronous task and returns after the task is complete.
100100
/// The scheduled task will not execute until all prior tasks have completed or suspended.
101101
/// - Parameter task: The task to enqueue. The task's parameter is a reference to the actor whose execution context has been adopted.
102102
/// - Returns: The value returned from the enqueued task.
103-
public func await<T>(_ task: @escaping @Sendable (isolated ActorType) async -> T) async -> T {
103+
public func enqueueAndWait<T>(_ task: @escaping @Sendable (isolated ActorType) async -> T) async -> T {
104104
let executionContext = self.executionContext // Capture/retain the executionContext before suspending.
105105
return await withUnsafeContinuation { continuation in
106106
taskStreamContinuation.yield(ActorTask(executionContext: executionContext) { executionContext in
@@ -113,7 +113,7 @@ public final class ActorQueue<ActorType: Actor> {
113113
/// The scheduled task will not execute until all prior tasks have completed or suspended.
114114
/// - Parameter task: The task to enqueue. The task's parameter is a reference to the actor whose execution context has been adopted.
115115
/// - Returns: The value returned from the enqueued task.
116-
public func await<T>(_ task: @escaping @Sendable (isolated ActorType) async throws -> T) async throws -> T {
116+
public func enqueueAndWait<T>(_ task: @escaping @Sendable (isolated ActorType) async throws -> T) async throws -> T {
117117
let executionContext = self.executionContext // Capture/retain the executionContext before suspending.
118118
return try await withUnsafeThrowingContinuation { continuation in
119119
taskStreamContinuation.yield(ActorTask(executionContext: executionContext) { executionContext in

Sources/AsyncQueue/FIFOQueue.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class FIFOQueue: Sendable {
5454
/// Schedules an asynchronous task for execution and immediately returns.
5555
/// The scheduled task will not execute until all prior tasks – including suspended tasks – have completed.
5656
/// - Parameter task: The task to enqueue.
57-
public func async(_ task: @escaping @Sendable () async -> Void) {
57+
public func enqueue(_ task: @escaping @Sendable () async -> Void) {
5858
taskStreamContinuation.yield(task)
5959
}
6060

@@ -63,15 +63,15 @@ public final class FIFOQueue: Sendable {
6363
/// - Parameters:
6464
/// - isolatedActor: The actor within which the task is isolated.
6565
/// - task: The task to enqueue.
66-
public func async<ActorType: Actor>(on isolatedActor: ActorType, _ task: @escaping @Sendable (isolated ActorType) async -> Void) {
66+
public func enqueue<ActorType: Actor>(on isolatedActor: ActorType, _ task: @escaping @Sendable (isolated ActorType) async -> Void) {
6767
taskStreamContinuation.yield { await task(isolatedActor) }
6868
}
6969

7070
/// Schedules an asynchronous task and returns after the task is complete.
7171
/// The scheduled task will not execute until all prior tasks – including suspended tasks – have completed.
7272
/// - Parameter task: The task to enqueue.
7373
/// - Returns: The value returned from the enqueued task.
74-
public func await<T>(_ task: @escaping @Sendable () async -> T) async -> T {
74+
public func enqueueAndWait<T>(_ task: @escaping @Sendable () async -> T) async -> T {
7575
await withUnsafeContinuation { continuation in
7676
taskStreamContinuation.yield {
7777
continuation.resume(returning: await task())
@@ -85,7 +85,7 @@ public final class FIFOQueue: Sendable {
8585
/// - isolatedActor: The actor within which the task is isolated.
8686
/// - task: The task to enqueue.
8787
/// - Returns: The value returned from the enqueued task.
88-
public func await<ActorType: Actor, T>(on isolatedActor: isolated ActorType, _ task: @escaping @Sendable (isolated ActorType) async -> T) async -> T {
88+
public func enqueueAndWait<ActorType: Actor, T>(on isolatedActor: isolated ActorType, _ task: @escaping @Sendable (isolated ActorType) async -> T) async -> T {
8989
await withUnsafeContinuation { continuation in
9090
taskStreamContinuation.yield {
9191
continuation.resume(returning: await task(isolatedActor))
@@ -97,7 +97,7 @@ public final class FIFOQueue: Sendable {
9797
/// The scheduled task will not execute until all prior tasks – including suspended tasks – have completed.
9898
/// - Parameter task: The task to enqueue.
9999
/// - Returns: The value returned from the enqueued task.
100-
public func await<T>(_ task: @escaping @Sendable () async throws -> T) async throws -> T {
100+
public func enqueueAndWait<T>(_ task: @escaping @Sendable () async throws -> T) async throws -> T {
101101
try await withUnsafeThrowingContinuation { continuation in
102102
taskStreamContinuation.yield {
103103
do {
@@ -115,7 +115,7 @@ public final class FIFOQueue: Sendable {
115115
/// - isolatedActor: The actor within which the task is isolated.
116116
/// - task: The task to enqueue.
117117
/// - Returns: The value returned from the enqueued task.
118-
public func await<ActorType: Actor, T>(on isolatedActor: isolated ActorType, _ task: @escaping @Sendable (isolated ActorType) async throws -> T) async throws -> T {
118+
public func enqueueAndWait<ActorType: Actor, T>(on isolatedActor: isolated ActorType, _ task: @escaping @Sendable (isolated ActorType) async throws -> T) async throws -> T {
119119
try await withUnsafeThrowingContinuation { continuation in
120120
taskStreamContinuation.yield {
121121
do {

Tests/AsyncQueueTests/ActorQueueTests.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ final class ActorQueueTests: XCTestCase {
4747
XCTAssertNil(weakCounter)
4848
}
4949

50-
func test_async_retainsAdoptedActorUntilEnqueuedTasksComplete() async {
50+
func test_enqueue_retainsAdoptedActorUntilEnqueuedTasksComplete() async {
5151
let systemUnderTest = ActorQueue<Counter>()
5252
var counter: Counter? = Counter()
5353
weak var weakCounter = counter
5454
systemUnderTest.adoptExecutionContext(of: counter!)
5555

5656
let semaphore = Semaphore()
57-
systemUnderTest.async { counter in
57+
systemUnderTest.enqueue { counter in
5858
await semaphore.wait()
5959
}
6060

@@ -63,64 +63,64 @@ final class ActorQueueTests: XCTestCase {
6363
await semaphore.signal()
6464
}
6565

66-
func test_async_taskParameterIsAdoptedActor() async {
66+
func test_enqueue_taskParameterIsAdoptedActor() async {
6767
let semaphore = Semaphore()
68-
systemUnderTest.async { counter in
68+
systemUnderTest.enqueue { counter in
6969
XCTAssertTrue(counter === self.counter)
7070
await semaphore.signal()
7171
}
7272

7373
await semaphore.wait()
7474
}
7575

76-
func test_await_taskParameterIsAdoptedActor() async {
77-
await systemUnderTest.await { counter in
76+
func test_enqueueAndWait_taskParameterIsAdoptedActor() async {
77+
await systemUnderTest.enqueueAndWait { counter in
7878
XCTAssertTrue(counter === self.counter)
7979
}
8080
}
8181

82-
func test_async_sendsEventsInOrder() async {
82+
func test_enqueue_sendsEventsInOrder() async {
8383
for iteration in 1...1_000 {
84-
systemUnderTest.async { counter in
84+
systemUnderTest.enqueue { counter in
8585
counter.incrementAndExpectCount(equals: iteration)
8686
}
8787
}
88-
await systemUnderTest.await { _ in /* Drain the queue */ }
88+
await systemUnderTest.enqueueAndWait { _ in /* Drain the queue */ }
8989
}
9090

91-
func test_async_startsExecutionOfNextTaskAfterSuspension() async {
91+
func test_enqueue_startsExecutionOfNextTaskAfterSuspension() async {
9292
let systemUnderTest = ActorQueue<Semaphore>()
9393
let semaphore = Semaphore()
9494
systemUnderTest.adoptExecutionContext(of: semaphore)
9595

96-
systemUnderTest.async { semaphore in
96+
systemUnderTest.enqueue { semaphore in
9797
await semaphore.wait()
9898
}
99-
systemUnderTest.async { semaphore in
99+
systemUnderTest.enqueue { semaphore in
100100
// Signal the semaphore from the actor queue.
101101
// If the actor queue were FIFO, this test would hang since this code would never execute:
102102
// we'd still be waiting for the prior `wait()` tasks to finish.
103103
semaphore.signal()
104104
}
105-
await systemUnderTest.await { _ in /* Drain the queue */ }
105+
await systemUnderTest.enqueueAndWait { _ in /* Drain the queue */ }
106106
}
107107

108-
func test_await_allowsReentrancy() async {
109-
await systemUnderTest.await { [systemUnderTest] counter in
110-
await systemUnderTest.await { counter in
108+
func test_enqueueAndWait_allowsReentrancy() async {
109+
await systemUnderTest.enqueueAndWait { [systemUnderTest] counter in
110+
await systemUnderTest.enqueueAndWait { counter in
111111
counter.incrementAndExpectCount(equals: 1)
112112
}
113113
counter.incrementAndExpectCount(equals: 2)
114114
}
115115
}
116116

117-
func test_async_executesEnqueuedTasksAfterReceiverIsDeallocated() async {
117+
func test_enqueue_executesEnqueuedTasksAfterReceiverIsDeallocated() async {
118118
var systemUnderTest: ActorQueue<Counter>? = ActorQueue()
119119
systemUnderTest?.adoptExecutionContext(of: counter)
120120

121121
let expectation = self.expectation(description: #function)
122122
let semaphore = Semaphore()
123-
systemUnderTest?.async { counter in
123+
systemUnderTest?.enqueue { counter in
124124
// Make the task wait.
125125
await semaphore.wait()
126126
counter.incrementAndExpectCount(equals: 1)
@@ -136,7 +136,7 @@ final class ActorQueueTests: XCTestCase {
136136
await waitForExpectations(timeout: 1.0)
137137
}
138138

139-
func test_async_doesNotRetainTaskAfterExecution() async {
139+
func test_enqueue_doesNotRetainTaskAfterExecution() async {
140140
final class Reference: Sendable {}
141141
final class ReferenceHolder: @unchecked Sendable {
142142
init() {
@@ -157,14 +157,14 @@ final class ActorQueueTests: XCTestCase {
157157
systemUnderTest.adoptExecutionContext(of: syncSemaphore)
158158

159159
let expectation = self.expectation(description: #function)
160-
systemUnderTest.async { [reference = referenceHolder.reference] syncSemaphore in
160+
systemUnderTest.enqueue { [reference = referenceHolder.reference] syncSemaphore in
161161
// Now that we've started the task and captured the reference, release the synchronous code.
162162
syncSemaphore.signal()
163163
// Wait for the synchronous setup to complete and the reference to be nil'd out.
164164
await asyncSemaphore.wait()
165165
// Retain the unsafe counter until the task is completed.
166166
_ = reference
167-
systemUnderTest.async { _ in
167+
systemUnderTest.enqueue { _ in
168168
// Signal that this task has cleaned up.
169169
// This closure will not execute until the prior closure completes.
170170
expectation.fulfill()
@@ -182,9 +182,9 @@ final class ActorQueueTests: XCTestCase {
182182
XCTAssertNil(referenceHolder.weakReference)
183183
}
184184

185-
func test_await_sendsEventsInOrder() async {
185+
func test_enqueueAndWait_sendsEventsInOrder() async {
186186
for iteration in 1...1_000 {
187-
systemUnderTest.async { counter in
187+
systemUnderTest.enqueue { counter in
188188
counter.incrementAndExpectCount(equals: iteration)
189189
}
190190

@@ -193,26 +193,26 @@ final class ActorQueueTests: XCTestCase {
193193
continue
194194
}
195195

196-
await systemUnderTest.await { counter in
196+
await systemUnderTest.enqueueAndWait { counter in
197197
XCTAssertEqual(counter.count, iteration)
198198
}
199199
}
200-
await systemUnderTest.await { _ in /* Drain the queue */ }
200+
await systemUnderTest.enqueueAndWait { _ in /* Drain the queue */ }
201201
}
202202

203-
func test_await_canReturn() async {
203+
func test_enqueueAndWait_canReturn() async {
204204
let expectedValue = UUID()
205-
let returnedValue = await systemUnderTest.await { _ in expectedValue }
205+
let returnedValue = await systemUnderTest.enqueueAndWait { _ in expectedValue }
206206
XCTAssertEqual(expectedValue, returnedValue)
207207
}
208208

209-
func test_await_canThrow() async {
209+
func test_enqueueAndWait_canThrow() async {
210210
struct TestError: Error, Equatable {
211211
private let identifier = UUID()
212212
}
213213
let expectedError = TestError()
214214
do {
215-
try await systemUnderTest.await { _ in throw expectedError }
215+
try await systemUnderTest.enqueueAndWait { _ in throw expectedError }
216216
} catch {
217217
XCTAssertEqual(error as? TestError, expectedError)
218218
}

0 commit comments

Comments
 (0)