-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add consistent examples to README #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,21 @@ Tasks sent from a synchronous context to an asynchronous context in Swift Concur | |
|
|
||
| ```swift | ||
| @MainActor | ||
| func test_mainActor_taskOrdering() async { | ||
| var counter = 0 | ||
| func testMainActorTaskOrdering() async { | ||
| actor Counter { | ||
| func increment() -> Int { | ||
| count += 1 | ||
| return count | ||
| } | ||
| var count = 0 | ||
| } | ||
|
|
||
| let counter = Counter() | ||
| var tasks = [Task<Void, Never>]() | ||
| for iteration in 1...100 { | ||
| tasks.append(Task { | ||
| counter += 1 | ||
| XCTAssertEqual(counter, iteration) // often fails | ||
| let incrementedCount = await counter.increment() | ||
| XCTAssertEqual(incrementedCount, iteration) // often fails | ||
| }) | ||
| } | ||
| for task in tasks { | ||
|
|
@@ -51,14 +59,35 @@ queue.async { | |
| This task begins execution once the above one-second sleep completes. | ||
| */ | ||
| } | ||
| Task { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense to me. The reader can infer from the |
||
| await queue.await { | ||
| /* | ||
| `async` context that can return a value or throw an error. | ||
| Executes after all other enqueued work is completed. | ||
| Work enqueued after this task will wait for this task to complete. | ||
| */ | ||
| await queue.await { | ||
| /* | ||
| `async` context that can return a value or throw an error. | ||
| Executes after all other enqueued work is completed. | ||
| Work enqueued after this task will wait for this task to complete. | ||
| */ | ||
| } | ||
| ``` | ||
|
|
||
| With a `FIFOQueue` you can easily execute asynchronous tasks from a nonisolated context in FIFO order: | ||
| ```swift | ||
| func testFIFOQueueOrdering() async { | ||
| actor Counter { | ||
| func increment() -> Int { | ||
| count += 1 | ||
| return count | ||
| } | ||
| var count = 0 | ||
| } | ||
|
|
||
| let counter = Counter() | ||
| let queue = FIFOQueue() | ||
| for iteration in 1...100 { | ||
| queue.async { | ||
| let incrementedCount = await counter.increment() | ||
| XCTAssertEqual(incrementedCount, iteration) // always succeeds | ||
| } | ||
| } | ||
| await queue.await { } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe clarify with a comment that we're ensuring all tasks complete before we end the test?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this in #9 which builds on top of this change.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| ``` | ||
|
|
||
|
|
@@ -80,14 +109,35 @@ queue.async { | |
| This task begins execution once the above task suspends due to the one-second sleep. | ||
| */ | ||
| } | ||
| Task { | ||
| await queue.await { | ||
| /* | ||
| `async` context that can return a value or throw an error. | ||
| Executes after all other enqueued work has begun executing. | ||
| Work enqueued after this task will wait for this task to complete or suspend. | ||
| */ | ||
| await queue.await { | ||
| /* | ||
| `async` context that can return a value or throw an error. | ||
| Executes after all other enqueued work has begun executing. | ||
| Work enqueued after this task will wait for this task to complete or suspend. | ||
| */ | ||
| } | ||
| ``` | ||
|
|
||
| With an `ActorQueue` you can easily begin execution of asynchronous tasks from a nonisolated context in order: | ||
| ```swift | ||
| func testActorQueueOrdering() async { | ||
| actor Counter { | ||
| func increment() -> Int { | ||
| count += 1 | ||
| return count | ||
| } | ||
| var count = 0 | ||
| } | ||
|
|
||
| let counter = Counter() | ||
| let queue = ActorQueue() | ||
| for iteration in 1...100 { | ||
| queue.async { | ||
| let incrementedCount = await counter.increment() | ||
| XCTAssertEqual(incrementedCount, iteration) // always succeeds | ||
| } | ||
| } | ||
| await queue.await { } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we also clarify the purpose of this line.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this in #9 which builds on top of this change. |
||
| } | ||
| ``` | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we'd explain better how the |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need a
Countertype in this example but the below examples do need it. For the sake of consistency with the below additions I made this example test a dash more complicated.