Skip to content

Commit

Permalink
Merge pull request #824 from smallrye/bug/kotlin-flow-cancellation-fl…
Browse files Browse the repository at this point in the history
…akiness

Fix a Kotlin test timing issue due to slow starting coroutines
  • Loading branch information
jponge authored Jan 24, 2022
2 parents 2ba56ec + 75f9166 commit c583ae4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.Awaitility
import org.awaitility.Awaitility.await

class FlowAsMultiTest {

Expand Down Expand Up @@ -75,8 +77,12 @@ class FlowAsMultiTest {
@Test
fun `verify that coroutine cancellation result in failure`() {
testBlocking {
Awaitility.pollInSameThread()
val ready = AtomicBoolean()

// Given
val flow = flow<UUID> {
ready.set(true)
delay(200)
emit(UUID.randomUUID())
}
Expand All @@ -86,9 +92,8 @@ class FlowAsMultiTest {
val job = launch {
flow.asMulti().subscribe().withSubscriber(subscriber)
}
delay(50)
await().untilTrue(ready)
job.cancel(CancellationException("abort"))
Thread.sleep(350)

// Then
subscriber.awaitFailure().assertFailedWith(CancellationException::class.java, "abort")
Expand Down

0 comments on commit c583ae4

Please sign in to comment.