Skip to content

flatMapLatest + retryWhen lead to emission loss when collected inside TestScope or inside runBlocking scope #3118

@AlexWoodblock

Description

@AlexWoodblock

Kotlin version: 1.6.10
Coroutines version:
Platform: JVM

val list = flow {
  emit(1)
}
.flatMapLatest {
  flow {
    emit("First item for $it")
    throw RuntimeException()
  }
}
.retryWhen { _, _ ->
  emit("Error!")
  true
 }
.take(6)
.toList()

println(list)

Running this code prints [Error!, Error!, Error!, Error!, Error!, Error!] inside runTest or runBlocking, despite having emit("First item for $it") before the exception throwing.

However, collecting the flow in another dispatcher (for example, Dispatchers.Default, or Dispatchers.IO) will make it work as expected, printing [First item for 1, Error!, First item for 1, Error!, First item for 1, Error!].

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions