Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0e8588d4 authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

Trace util for coroutineScope

Test: atest tracinglib-host-test
Flag: com.android.systemui.coroutine_tracing
Bug: 289353932
Change-Id: Ieed70d0f19314ae20a4e95ded5943b9d97298dc6
parent 1d34c94a
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@@ -38,6 +39,19 @@ import kotlinx.coroutines.withContext

@PublishedApi internal const val DEFAULT_TRACK_NAME = "Coroutines"

@OptIn(ExperimentalContracts::class)
suspend inline fun <R> coroutineScope(
    traceName: String,
    crossinline block: suspend CoroutineScope.() -> R
): R {
    contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
    return traceCoroutine(traceName) {
        return@traceCoroutine coroutineScope wrappedCoroutineScope@{
            return@wrappedCoroutineScope block()
        }
    }
}

/**
 * Convenience function for calling [CoroutineScope.launch] with [traceCoroutine] to enable tracing.
 *
+54 −0
Original line number Diff line number Diff line
@@ -93,6 +93,60 @@ class CoroutineTracingTest : TestBase() {
        expect(2)
    }

    @Test
    fun launchInCoroutineScope() = runTestWithTraceContext {
        launch("span-for-launch-0") {
            expect("span-for-launch-0")
            delay(1)
            expect("span-for-launch-0")
        }
        coroutineScope("span-for-coroutineScope-1") {
            launch("span-for-launch-1") {
                expect("span-for-coroutineScope-1", "span-for-launch-1")
                delay(1)
                expect("span-for-coroutineScope-1", "span-for-launch-1")
            }
            launch("span-for-launch-2") {
                expect("span-for-coroutineScope-1", "span-for-launch-2")
                delay(1)
                expect("span-for-coroutineScope-1", "span-for-launch-2")
            }
            coroutineScope("span-for-coroutineScope-2") {
                launch("span-for-launch-3") {
                    expect(
                        "span-for-coroutineScope-1",
                        "span-for-coroutineScope-2",
                        "span-for-launch-3"
                    )
                    delay(1)
                    expect(
                        "span-for-coroutineScope-1",
                        "span-for-coroutineScope-2",
                        "span-for-launch-3"
                    )
                }
                launch("span-for-launch-4") {
                    expect(
                        "span-for-coroutineScope-1",
                        "span-for-coroutineScope-2",
                        "span-for-launch-4"
                    )
                    delay(1)
                    expect(
                        "span-for-coroutineScope-1",
                        "span-for-coroutineScope-2",
                        "span-for-launch-4"
                    )
                }
            }
        }
        launch("span-for-launch-5") {
            expect("span-for-launch-5")
            delay(1)
            expect("span-for-launch-5")
        }
    }

    @Test
    fun nestedUpdateAndRestoreOnSingleThread_unconfinedDispatcher() = runTestWithTraceContext {
        traceCoroutine("parent-span") {