https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Martin Devillers

10/25/2018, 4:52 PM
Is this expected? This test fails:
Copy code
@Test
fun test() = runBlocking {
    assertTrue(actor<Unit> { for (it in this) {  } }.offer(Unit))
}
But this test passes:
Copy code
@Test
fun test() = runBlocking {
    val actor = GlobalScope.actor<Unit> { for (it in this) {  } }
    delay(100)
    assertTrue(actor.offer(Unit))
}
It’s like the actor’s queue needs some time to be set up. It’s not ready right away after creation.
e

elizarov

10/25/2018, 5:15 PM
It is expected. It is not about setting up queue. This is not how or why you’d use offer.
z

Zach Klippenstein (he/him) [MOD]

10/25/2018, 7:16 PM
The first test fails because your test coroutine never gives the actor coroutine a chance to run. Your second test does this by calling
delay
, but I think you should get the same result if you just used
yield
.
offer
will only succeed if there is a coroutine that is currently suspended waiting for something from the channel. Since the actor never has a chance to run, it never receives from its channel, so the offer fails.
4 Views