1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 @file:OptIn(DelicateCoroutinesApi::class, ExperimentalCoroutinesApi::class) 18 19 package com.android.test.tracing.coroutines 20 21 import android.platform.test.annotations.EnableFlags 22 import com.android.app.tracing.coroutines.createCoroutineTracingContext 23 import com.android.app.tracing.coroutines.launchTraced 24 import com.android.app.tracing.coroutines.traceCoroutine 25 import com.android.systemui.Flags.FLAG_COROUTINE_TRACING 26 import kotlin.coroutines.CoroutineContext 27 import kotlinx.coroutines.CoroutineScope 28 import kotlinx.coroutines.CoroutineStart 29 import kotlinx.coroutines.DelicateCoroutinesApi 30 import kotlinx.coroutines.ExperimentalCoroutinesApi 31 import kotlinx.coroutines.delay 32 import org.junit.Test 33 34 @EnableFlags(FLAG_COROUTINE_TRACING) 35 class NestedCoroutineTracingTest : TestBase() { 36 <lambda>null37 override val extraContext: CoroutineContext by lazy { createCoroutineTracingContext("main") } 38 39 @Test stressTestContextSwitches_depthnull40 fun stressTestContextSwitches_depth() { 41 fun CoroutineScope.recursivelyLaunch(n: Int) { 42 if (n == 400) return 43 launchTraced("launch#$n", start = CoroutineStart.UNDISPATCHED) { 44 traceCoroutine("a") { 45 if (n == 350) { 46 val expectedBeforeDelay = mutableListOf("main") 47 repeat(n + 1) { 48 expectedBeforeDelay.add("launch#$it") 49 expectedBeforeDelay.add("a") 50 } 51 expect(*expectedBeforeDelay.toTypedArray()) 52 } 53 recursivelyLaunch(n + 1) 54 delay(1) 55 expect("launch#$n", "a") 56 } 57 } 58 } 59 runTest(totalEvents = 401) { recursivelyLaunch(0) } 60 } 61 } 62