# Reliably testing code that adopts Swift Concurrency?

**URL:** <https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304>\
**Category:** Using Swift\
**Tags:** concurrency, testing\
**Created:** [May 13, 2022, 5:14pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304 "2022-05-13T17:14:10Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [June 1, 2022, 6:33pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/61 "2022-06-01T18:33:32Z")

</div>

> [@ptomaselli](#):
>
> Genuinely asking, is this actually true? From what I can tell, the “problem” with the original code is that `ViewModel` is not actually “ready” once it has been initialized. There is additional asynchronous preparation required before it can actually start doing its job, and this preparation is potentially racing with the delivery of the events it is supposed to handle. But I might be misreading the code, despite having re-read this thread about 50 times at this point 😂?

It's definitely true that in the span of a few microseconds between the view appearing and the subscription starting a screenshot could be taken and not counted. I think I'm OK if my test ignores that edge case and only focuses on the happy path of screenshots only happen after the subscription is started.

> [@ptomaselli](#):
>
> I don’t completely follow the Combine analogy, but w/r/t serial executors specifically, it seems like being able to “inject” a serial executor here would allow a completely reliable test to be written for code that, in production, produces a bug one out of every thousand times (or whatever). Arguably a worse outcome than what we have already, imo. At least the current flakiness reflects reality?

That's definitely true of a Wild West world of concurrency and non-determinism, but the example posted in this thread, in my opinion, does not fall into that category. And neither does a lot of very common situations one comes across in application development. For example, every case study and demo in TCA (of which there are dozens, many involving long-living and interleaving effects) does not fall into this category either.

However, if I was writing a low level systems library that could have thousands of concurrent users, then I would not use a serial executor and think that it is testing real life. That would be a great use case for property-based testing or fuzz testing.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [June 1, 2022, 6:44pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/62 "2022-06-01T18:44:02Z")

</div>

> [@mbrandonw](#):
>
> Honestly, the `AsyncStream` has solved the biggest problems for us since it buffers its elements until someone subscribes. That fixes the issue of an event occurring before a subscription. We're just not sure if there are other subtleties lurking in the shadows.

That does seem to point to a general problem that's also captured by Stephen's original example—perhaps the way SwiftUI's `task` modifier is designed makes it too easy to entangle the "setup" phase from the "execution" phase for testing purposes. Buffering should ideally not be something you need to rely on for testing either. A developer can factor the task code into two separate async methods—one which sets up the subscriptions and doesn't return until they're ready to go, and another that runs the continuous business logic that consumes the subscriptions—and that would allow for a test to call the setup phase, schedule its own test events, and then execute the business logic, but that requires some forethought on the user's behalf. Maybe a better design would be for there to be a variant of `task` modifier that takes a value conforming to a protocol with distinct `setUp` and `run` methods, to encourage clients to factor their code that way up front.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 1, 2022, 7:21pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/63 "2022-06-01T19:21:41Z")

</div>

> [@Joe\_Groff](#):
>
> Buffering should ideally not be something you need to rely on for testing either

We definitely agree that `AsyncStream` isn't 100% ideal, but it's gotten us far with the least amount of production code invasiveness. Assuming folks know the rough edges and expectations of how mock streams are interacted with in tests, I think it's a fair trade-off, and the buffering seems essential to testing these things without any other serial guarantees (or the ability to "flush" an executor's queue).

> [@Joe\_Groff](#):
>
> Maybe a better design would be for there to be a variant of `task` modifier that takes a value conforming to a protocol with distinct `setUp` and `run` methods, to encourage clients to factor their code that way up front.

Forgive me for putting more work on you to explain, but I'm not quite seeing how this would help. Can you sketch out some code or pseudocode that shows how it could apply to the screenshot example?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [June 1, 2022, 7:46pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/64 "2022-06-01T19:46:59Z")

</div>

In your original example, you could factor `task()` into something like this:

```swift
class ViewModel: ObservableObject {
  @Published var count = 0
  var progress: AsyncChannel<()>?

  @MainActor
  func setUpTask() async -> NotificationCenter.Notifications {
    return NotificationCenter.default.notifications(
      named: UIApplication.userDidTakeScreenshotNotification
    )
  }

  @MainActor
  func runTask(screenshots: NotificationCenter.Notifications) async {
    for await _ in screenshots {
      self.count += 1
      await progress?.send(())
    }
  }

  func task() async {
    runTask(screenshots: await setUpTask())
  }
}

```

which would allow you to write the test setup as:

```swift
@MainActor
func testBasics() async throws {
  let vm = ViewModel()
  let vmProgress = AsyncChannel<()>()
  vm.progress = vmProgress

  // Set up the subscription in the ViewModel.
  let screenshots = await vm.setUpTask()
  XCTAssertEqual(vm.count, 0)

  // Kick off the main loop of the ViewModel task.
  async let task = vm.runTask(screenshots: screenshots)

  // Simulate a screen shot being taken.
  NotificationCenter.default.post(
    name: UIApplication.userDidTakeScreenshotNotification, object: nil
  )

  // Wait for the task to post progress through the loop.
  await vmProgress.next()

  XCTAssertEqual(vm.count, 1)
}

```

Since the `setUp` is a separate async function, your test can call it and let it complete before posting its own notifications, allowing you to avoid the first implicit scheduling dependency. I preserved the `AsyncChannel` for posting progress in the main loop to deal with the second scheduling dependency here, but `runTask` should also at this point be straightforward to express as an `AsyncStream` that takes the notification stream as an input and turns it into a running count, like you might in Combine, which would let you test the stream's behavior in isolation.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 1, 2022, 8:01pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/65 "2022-06-01T20:01:44Z")

</div>

> [@Joe\_Groff](#):
>
> In your original example, you could factor `task()` into something like this: […]

Unfortunately, this doesn't solve the problem. The `post` can still happen before the subscription, at which point the channel will get stuck and hang the test forever, which took 78 iterations for me to hit:

```console
…
Test Case '-[Tests testBasics]' started (Iteration 77 of 10000).
Test Case '-[Tests testBasics]' passed (0.002 seconds).
Test Case '-[Tests testBasics]' started (Iteration 78 of 10000).

```

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [June 1, 2022, 8:06pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/66 "2022-06-01T20:06:38Z")

</div>

That's unfortunate. Like I said before, we should provide an API that lets code wait for the subscription to be ready before proceeding for testing purposes:

> [@Joe\_Groff](#):
>
> If the subscription isn't guaranteed to actually be in effect before `notifications(named:)` returns, then I agree that being able to get that guarantee seems like something Foundation should provide (and it should be documented that the subscription may be delayed in the API docs).

If you were going to factor `runTask` into an `AsyncStream` transformation, though, then you could also make this easier to test by having the input stream a generic `AsyncSequence` instead of requiring it to be an `NSNotification.Notifications` sequence, which would let unit tests provide their own stream under their control.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 1, 2022, 8:14pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/67 "2022-06-01T20:14:22Z")

</div>

> [@Joe\_Groff](#):
>
> That's unfortunate. Like I said before, we should provide an API that lets code wait for the subscription to be ready before proceeding for testing purposes:

I'm not quite sure how that would work under the hood, but if this particular API could be fixed that'd be nice!

> [@Joe\_Groff](#):
>
> If you were going to factor `runTask` into an `AsyncStream` transformation, though, then you could also make this easier to test by having the input stream a generic `AsyncSequence` instead of requiring it to be an `NSNotification.Notifications` sequence, which would let unit tests provide their own stream under their control.

I think this depends on `any AsyncSequence<Element> throws/nothrows` (or `AnyAsyncSequence/AnyThrowingAsyncSequence`) to be viable. Adding a generic per async sequence dependency to each view and view model becomes quickly untenable.

FWIW I've defined a helper that effectively uses `Async(Throwing)Stream` as type-erasers given an async sequence, so converting `Notification.Notifications` is a one-liner at least. It's just not as safe as it could be since the `@rethrows` effect can't be tracked right now.

---

<div class="post-metadata">

**Author:** ![rvsrvs](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rvsrvs/32/5270_2.png) [@rvsrvs](https://forums.swift.org/u/rvsrvs)\
**Post date:** [June 1, 2022, 8:42pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/68 "2022-06-01T20:42:43Z")

</div>

@stephencelis Have you considered passing, say, an `UnsafeContinuation<Void, Never>` into the the Task and resuming it when task start up has completed? This is the only way I've been able to get a Task to to notify on startup and is what I'm using in the CheckedExpectation I posted a link to way up thread. It's the only way I've found to reliably assure notification of task startup completion.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 1, 2022, 8:58pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/69 "2022-06-01T20:58:25Z")

</div>

> [@rvsrvs](#):
>
> @stephencelis Have you considered passing, say, an `UnsafeContinuation<Void, Never>` into the the Task and resuming it when task start up has completed? This is the only way I've been able to get a Task to to notify on startup and is what I'm using in the CheckedExpectation I posted a link to way up thread. It's the only way I've found to reliably assure notification of task startup completion.

I think for the example I posted there's a chicken-egg problem if you intend to pass an optional continuation to the `vm.task()` endpoint. An `AsyncStream.Continuation` or `AsyncChannel` get around the problem, but that's basically back to Joe's solution.

---

<div class="post-metadata">

**Author:** ![rvsrvs](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rvsrvs/32/5270_2.png) [@rvsrvs](https://forums.swift.org/u/rvsrvs)\
**Post date:** [June 1, 2022, 9:41pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/70 "2022-06-01T21:41:46Z")

</div>

True, I was basically only getting around the initial setup and not trying to test the entire stream. Basically, a whole layer of assertions around AsyncChannel is going to be the only way to do this right now.

---

<div class="post-metadata">

**Author:** ![babbage](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/babbage/32/6780_2.png) [@babbage](https://forums.swift.org/u/babbage)\
**Post date:** [June 2, 2022, 4:00am UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/71 "2022-06-02T04:00:00Z")

</div>

> [@ptomaselli](#):
>
> Genuinely asking, is this actually true? From what I can tell, the “problem” with the original code is that `ViewModel` is not actually “ready” once it has been initialized. There is additional asynchronous preparation required before it can actually start doing its job, and this preparation is potentially racing with the delivery of the events it is supposed to handle.

I think this is absolutely key. I suggest what the example and investigations here in the thread have established is that there is a clear flaw in the implementation of AsyncSequence.

It is a reasonable expectation of this type that when you await its initialization, it does not return until it is listening to the events it will handle. What is required is a bug report/evolution proposal to request that AsyncSequence behaves as would be expected in an async-await world. With that, no additional testing tools or infrastructure would be required for the AsyncSequence example.

_Later:_ On reflection, it seems more likely that the implementation fault might lie with NotificationCentre's implementation of `AsyncSequence` in `NotificationCenter.Notifications` rather than in the AsyncSequence protocol per se.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [June 2, 2022, 4:13pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/72 "2022-06-02T16:13:27Z")

</div>

> [@babbage](#):
>
> _Later:_ On reflection, it seems more likely that the implementation fault might lie with NotificationCentre's implementation of `AsyncSequence` in `NotificationCenter.Notifications` rather than in the AsyncSequence protocol per se.

Yeah, I think this is the case. AsyncSequence as a protocol can admit varying implementations, and it appears to be an undocumented quirk of NotificationCenter.Notifications that the underlying subscription isn't established before the sequence value is returned to the caller, making it impossible to order test notifications in relation to the subscription.

---

<div class="post-metadata">

**Author:** ![babbage](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/babbage/32/6780_2.png) [@babbage](https://forums.swift.org/u/babbage)\
**Post date:** [June 12, 2022, 2:26am UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/73 "2022-06-12T02:26:33Z")

</div>

A good story often has an epilogue.

As things seemed to point to `NotificationCentre.Notifications` as the culprit in the hard-to-test code here, I wrote a minimal test to demonstrate the bug, intending to submit a Feedback report to the team. Not difficult to test in a nicely encapsulated test:

```swift
func test_notificationCentreNotifications_doesNotDisplayRaceCondition() async throws {
    // Setup test expectation
    let expectation = self.expectation(description: "Notification received.")
    let fulfillExpectation = { expectation.fulfill() }
    
    // Initiate the NotificationCentre.Notifications AsyncSequence
    let asyncSequenceTask = Task {
        let screenshotSequence = await NotificationCenter.default.notifications(
            named: UIApplication.userDidTakeScreenshotNotification
        )
        for await _ in screenshotSequence {
            fulfillExpectation()
        }
    }
    
    // Post the notification created when a screenshot occurs.
    _ = await MainActor.run {
        Task.detached(priority:.userInitiated, operation: {
            await NotificationCenter.default.post(name: UIApplication.userDidTakeScreenshotNotification, object: nil)
        })
    }

    // Test expectations
    await self.waitForExpectations(timeout: 1)
    asyncSequenceTask.cancel()
}

```

Just one "problem":

> Executed 100000 tests, with 0 failures (0 unexpected) in 138.589 (203.695) seconds

OK. So, our diagnosis above, pointing the finger at the internal initialization of `NotificationCentre.Notifications` seemed plausible, but clearly isn't correct.

So we’re back answering the original topic of the thread, a focus on the testability of async-await, with the original question “So how are we supposed to test code like this?”. Effective problem solving requires clear problem definition. And while it might not be initialization of `NotificationCentre.Notifications` that was the issue, it still seems like an initialization issue was the problem that was making the code untestable.

> [@ptomaselli](#):
>
> From what I can tell, the “problem” with the original code is that `ViewModel` is not actually “ready” once it has been initialized.

This is the insight. While the ViewModel was initialized synchronously, it originally only created its AsyncSequence to monitor for screenshots when the async method `task()` was called. That could be the cause of a race condition. What if we moved the creation of that AsyncSequence to occur as part of the initialization of the ViewModel, and also kick off monitoring during `init()`?

```swift
@MainActor
class ViewModel: ObservableObject {
    @Published var count = 0
    let screenshots = NotificationCenter.default.notifications(named: UIApplication.userDidTakeScreenshotNotification)
    var screenshotMonitor: Task<(), Never>?
    
    init() {
        Task { await monitorForScreenshots() }
    }

    func monitorForScreenshots() async {
        screenshotMonitor = Task {
            for await _ in screenshots {
                self.count += 1
            }
        }
    }
    
    func cancelMonitoring() {
        screenshotMonitor?.cancel()
    }
}

```

The test for this is then pretty straightfoward, directly examines the count property on the ViewModel, and does not use dependency injection:

```swift
func test_selfInitializedViewModel_doesNotDisplayRaceCondition() async throws {
    // Initialize the ViewModel
    let viewModel = await ViewModel()
    
    // Baseline expectations
    await MainActor.run { // MainActor.run required because ViewModel is @MainActor given @Published count
        XCTAssertEqual(viewModel.count, 0, "Expected 0 count, found \(viewModel.count)")
    }
    
    // Post the notification created when a screenshot occurs, then wait till it has been issued.
    _ = await expectation(forNotification: UIApplication.userDidTakeScreenshotNotification, object: nil)
    await NotificationCenter.default.post(name: UIApplication.userDidTakeScreenshotNotification, object: nil)
    await waitForExpectations(timeout: 1)
    
    // Test expectations
    await MainActor.run { // MainActor.run required because ViewModel is @MainActor given @Published count
        XCTAssertEqual(viewModel.count, 1, "Expected 1 count, found \(viewModel.count)")
    }
    
    // Cleanup
    await viewModel.cancelMonitoring()
}

```

Let’s be really sure. Result of running this test one million times:

> Executed 1000000 tests, with 0 failures (0 unexpected) in 2274.146 (3139.190) seconds

What I’ve learned is: to ensure AsyncSequence code is testable, your production code should initialize your AsyncSequence synchronously, as part of its owner’s initiailzation. This also makes the production code slightly more reliable (eliminates the possibility of missing an early screenshot during that async startup), though functionally this probably would be a non-issue.

 ![Screen Shot 2022-06-12 at 12.11.02](https://global.discourse-cdn.com/swift/original/3X/d/1/d1a3d77ec48dcea0fba4b54c77a59e2b0c894f72.jpeg)

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 22, 2022, 9:58pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/74 "2022-06-22T21:58:45Z")

</div>

Not resurrecting this thread to suggest relying on `Task.yield()` is a good idea (though I imagine folks will rely on it till there are better tools to assist testing async code), but I did notice that this behavior has gotten much worse in the Xcode 14 betas. Previously, the single `Task.yield()` would result in a passing test 99% of the time, but now if fails far more often than not. I need to do a "mega" yield for it to consistently pass on my machine:

```swift
await Task.detached(priority: .low) { await Task.yield() }.value
await Task.detached(priority: .low) { await Task.yield() }.value
await Task.detached(priority: .low) { await Task.yield() }.value

```

This is just to let the team know that there may be performance regressions in the latest concurrency runtime.

---

<div class="post-metadata">

**Author:** ![darrarski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darrarski/32/6548_2.png) [@darrarski](https://forums.swift.org/u/darrarski)\
**Post date:** [March 24, 2023, 1:45pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/75 "2023-03-24T13:45:18Z")

</div>

Unfortunately, this became an even bigger problem in Xcode 14.3 RC (14E222a). My tests that previously (Xcode 14.2) passed without the need to use `await Task.yield()` - now fails randomly.

What's worse, the `yield` workaround proposed by @stephencelis does not work anymore for me on Xcode 14.3 RC. Previously I used `yield` workaround + a one-second timeout for some of my tests, which fixed issues on a slow CI server. Now, even with the timeout increased to 10 seconds and thousands of `yields`, my tests are failing from time to time on M1 Max MacBook.

Before swift-concurrency it was hard to test asynchronous code. Now, it's impossible.

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [March 26, 2023, 12:51pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/76 "2023-03-26T12:51:45Z")

</div>

With AsyncSequence you don’t really know when the counter is incremented. And that’s actually a part of the `ViewModel`’s contract. It is not correct to test that ViewModel increments the counter right after the notification, because it’s not what it is doing. Instead what ViewModel can promise is that it will increment the counter some time later, and will let you know when it does.

And it notifies using `objectWillChange`. So, I think test should be waiting for `objectWillChange` to fire. In the end it is not really testing the async stuff, it tests the view model with sync public API.

```swift
class ObjectDidChangeExpectation: XCTestExpectation {
    var cancellable: AnyCancellable?
    var wasScheduled: Bool = false
    
    init<T: ObservableObject>(_ object: T) {
        super.init(description: "ObjectDidChangeExpectation(\(object))")
        self.cancellable = object.objectWillChange.sink(receiveValue: { [weak self] _ in
            guard let self = self else { return }
            if self.wasScheduled { return }
            self.wasScheduled = true
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                self.fulfill()
                self.cancellable = nil
            }
        })
    }
}

final class ViewModelTests: XCTestCase {
    func testBasics() {
        let vm = ViewModel()
        let completion = XCTestExpectation(description: "End of task")
        let task = Task {
            await vm.task()
            completion.fulfill()
        }
        
        XCTAssertEqual(vm.count, 0)
        
        let ex = ObjectDidChangeExpectation(vm)
        
        // Simulate a screen shot being taken.
        NotificationCenter.default.post(
            name: UIApplication.userDidTakeScreenshotNotification, object: nil
        )
        
        self.wait(for: [ex], timeout: 1)
        
        XCTAssertEqual(vm.count, 1)
        
        task.cancel()
        self.wait(for: [completion], timeout: 1)
    }
}

```

But, it does not work. My first suspicion was that `XCTestCase.wait(for:timeout:)` does not drain main loop. So, I reimplemented it using continuations instead of expectations:

```swift
extension ObservableObject {
    @MainActor
    func waitingForChange(_ action: () -> Void) async {
        await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
            var pendingContinuation: Optional<_> = continuation
            var cancellable: AnyCancellable?
            cancellable = self.objectWillChange.sink(receiveValue: { _ in
                if let cont = pendingContinuation {
                    pendingContinuation = nil
                    DispatchQueue.main.async {
                        cancellable?.cancel()
                        cancellable = nil
                        cont.resume()
                    }
                }
            })
            action()
        }
    }
}

final class ViewModelTests: XCTestCase {    
    @MainActor
    func testBasics() async {
        let vm = ViewModel()
        let task = Task { await vm.task() }
        
        XCTAssertEqual(vm.count, 0)
        
        await vm.waitingForChange {
            // Simulate a screen shot being taken.
            NotificationCenter.default.post(
                name: UIApplication.userDidTakeScreenshotNotification, object: nil
            )
        }
        
        XCTAssertEqual(vm.count, 1)
        
        task.cancel()
        await task.value
    }
}

```

But it does not work either. After debugging the code, I've realised that the actual problem is that starting a task to observe notifications is racy. Any notifications posted before `vm.task()` starts to execute are ignored. So both versions of the test above are working fine. They are failing because there is a bug in the SUT. The fix would be to use Combine or raw NotificationCenter API. I don't think this bug can be fixed using async API.

My learning from this case, it to not use `NotificationCenter.Notifications` at all.

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [March 26, 2023, 1:13pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/77 "2023-03-26T13:13:54Z")

</div>

Is this problem even avoidable by a hypothetical better implementation of `NotificationCenter.Notifications` or is it inherent to all "hot" `AsyncSequence`s and the fact that there's no way for an `await`ed `async` function to perform some initial logic synchronously?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [March 26, 2023, 2:01pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/78 "2023-03-26T14:01:41Z")

</div>

Probably similar question, without any acknowledgment that there is an issue, or any suggested technique or workaround: [[Pitch] Observation (Revised) - #43 by gwendal.roue](https://forums.swift.org/t/pitch-observation-revised/63757/43)

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [March 26, 2023, 2:37pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/79 "2023-03-26T14:37:22Z")

</div>

Thanks for cross-referencing that, and yes I think it's the same root issue and a real shortcoming of async/await.

---

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [March 27, 2023, 6:35pm UTC](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304/80 "2023-03-27T18:35:48Z")

</div>

It is difficult to know when observation of an AsyncSequence has started. Typically, even from an async context, you spin up a Task to do the observation. And you don't know when that task starts. We've unfortunately reverted to Combine and publishers for some of this.

[Previous page](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304.md?page=3)

[Next page](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304.md?page=5)
