Skip to content

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

Closed
veyndan wants to merge 1 commit into
androidx:androidx-mainfrom
veyndan:270612487/replace-runBlocking-with-runTest
Closed

veyndan wants to merge 1 commit into
androidx:androidx-mainfrom
veyndan:270612487/replace-runBlocking-with-runTest

Conversation

@veyndan

@veyndan veyndan commented Jun 1, 2023

Copy link
Copy Markdown
Contributor

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
harryzz pushed a commit to harryzz/compose-multiplatform-core that referenced this pull request Jul 28, 2026
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.

https://github.com/androidx/androidx/blob/9e6ed6f6b1068cb366a6e51c2796ae5fe607e22d/paging/paging-common/src/test/kotlin/androidx/paging/SingleRunnerTest.kt#L151-L190

Test: ./gradlew test connectedCheck
Bug: 270612487

This is an imported pull request from androidx#555.

Resolves JetBrains#555
Github-Pr-Head-Sha: 4749aac
GitOrigin-RevId: 3a59fb4
Change-Id: Ic8275ee6a26e870d71cbf4bc57275db84b14b031
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants