Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace most runBlocking calls with runTest in paging-common #555

Conversation

veyndan
Copy link
Contributor

@veyndan veyndan commented Jun 1, 2023

The below test is the only remaining instance of runBlocking in paging-common. The rest of the test uses a lot of Java specific stuff, and in Multiplatform Paging, I hadn't attempted to multiplatformize it yet.

@OptIn(DelicateCoroutinesApi::class)
@Test
fun ensureIsolation_whenCancelationIsIgnoredByThePreviousBlock() {
// make sure we wait for previous one if it ignores cancellation
val singleRunner = SingleRunner()
val output = Collections.synchronizedList(mutableListOf<Int>())
// using a latch instead of a mutex to avoid suspension
val firstStarted = CountDownLatch(1)
GlobalScope.launch {
singleRunner.runInIsolation {
// this code uses latches and thread sleeps instead of Mutex and delay to mimic
// a code path which ignores coroutine cancellation
firstStarted.countDown()
repeat(10) {
@Suppress("BlockingMethodInNonBlockingContext")
Thread.sleep(100)
output.add(it)
}
}
}
val job2 = GlobalScope.launch {
@Suppress("BlockingMethodInNonBlockingContext")
firstStarted.await()
singleRunner.runInIsolation {
repeat(10) {
output.add(it + 10)
}
}
}
runBlocking {
withTimeout(10.seconds) {
job2.join()
}
}
assertThat(output).isEqualTo(
// if cancellation is ignored, make sure we wait for it to finish.
(0 until 20).toList()
)
}

Test: ./gradlew test connectedCheck
Bug: 270612487

@claraf3 claraf3 self-requested a review June 1, 2023 22:29
@veyndan veyndan force-pushed the 270612487/replace-runBlocking-with-runTest branch from 297bcb9 to 4749aac Compare June 2, 2023 21:50
@copybara-service copybara-service bot closed this in 143a6f5 Jun 2, 2023
@veyndan veyndan deleted the 270612487/replace-runBlocking-with-runTest branch June 3, 2023 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants