Skip to content

Commit 1eade06

Browse files
committed
Tweak coroutines ANR for a more realistic case
1 parent 8270899 commit 1eade06

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/FatalIssueGenerator.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import kotlinx.coroutines.flow.asFlow
3434
import kotlinx.coroutines.flow.onEach
3535
import kotlinx.coroutines.launch
3636
import kotlinx.coroutines.runBlocking
37+
import java.util.UUID
3738

3839

3940
/**
@@ -92,18 +93,13 @@ internal object FatalIssueGenerator {
9293
}
9394

9495
fun forceCoroutinesAnr() {
95-
CoroutineScope(Dispatchers.Main).launch {
96-
(1..Int.MAX_VALUE).asFlow()
97-
.onEach {
98-
Thread.sleep(1)
99-
}
100-
.collect {
101-
Log.i(TAG_NAME, "Item received: $it")
102-
}
96+
callOnMainThread {
97+
Log.i(TAG_NAME, "forceCoroutinesAnr. Getting device")
98+
val deviceId = DeviceFetcher.getDeviceId()
99+
Log.d(TAG_NAME, "forceCoroutinesAnr. Device ID: $deviceId")
103100
}
104101
}
105102

106-
107103
fun forceCoroutinesCrash(){
108104
CoroutineScope(Dispatchers.IO).launch {
109105
throw RuntimeException("Coroutine background thread crash")
@@ -195,4 +191,22 @@ internal object FatalIssueGenerator {
195191
return "Task ID [$id] completed with a $duration of milliseconds"
196192
}
197193
}
194+
195+
object DeviceFetcher {
196+
197+
private suspend fun fetchDeviceId(): String {
198+
delay(20000)
199+
return "Device ID: ${UUID.randomUUID()}"
200+
}
201+
202+
/**
203+
* This call will block caller thread until fetchDeviceId completes
204+
*/
205+
fun getDeviceId(): String {
206+
return runBlocking {
207+
Log.i("DeviceScopeRegistry", "getDevice")
208+
fetchDeviceId()
209+
}
210+
}
211+
}
198212
}

0 commit comments

Comments
 (0)