How to reliably await a change to an Observable type in tests

The topic of using Task.immediate to set up observations deterministically was discussed in the review thread of SE-0472 and in the review thread of SE-0475.

My understanding is that Task.immediate can't always be used to set up observation and ensure the loop is already "listening" for values before continuing, even though it (empirically) works most of the time. Nothing in the AsyncIteratorProtocol guarantees this to work.

That said, I thought this would work for Observations unless an actor hop was required due to the observation and the iterator being in different isolation regions. I can't really tell from the source if there are any additional suspension points in the process of calling next() before the continuation is registered, though.

+1 on this, it's the only truly reliable solution I've found when facing this problem. Sadly it's not always feasible to do so. Sometimes you do want to test whether a fire-and-forget function will eventually trigger a new value in an async sequence.

2 Likes