Loading tracinglib/core/src/TraceUtils.kt +4 −3 Original line number Diff line number Diff line Loading @@ -111,13 +111,14 @@ public inline fun <T> traceSection(tag: String, block: () -> T): T { * strings when not needed. */ @OptIn(ExperimentalContracts::class) public inline fun <T> traceSection(tag: () -> String, block: () -> T): T { public inline fun <T> traceSection(tag: () -> String?, block: () -> T): T { contract { callsInPlace(tag, InvocationKind.AT_MOST_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE) } val tracingEnabled = Trace.isEnabled() if (tracingEnabled) beginSlice(tag()) val sliceName = if (Trace.isEnabled()) tag() else null val tracingEnabled = sliceName != null if (tracingEnabled) beginSlice(sliceName!!) return try { block() } finally { Loading tracinglib/core/src/coroutines/CoroutineTracing.kt +4 −4 Original line number Diff line number Diff line Loading @@ -161,10 +161,10 @@ public suspend inline fun <T> withContextTraced( } /** @see kotlinx.coroutines.runBlocking */ public inline fun <T> runBlockingTraced( crossinline spanName: () -> String, context: CoroutineContext, noinline block: suspend CoroutineScope.() -> T, public fun <T> runBlockingTraced( spanName: () -> String? = { null }, context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T, ): T { contract { callsInPlace(spanName, InvocationKind.AT_MOST_ONCE) Loading tracinglib/robolectric/src/RunBlockingTracedTest.kt 0 → 100644 +113 −0 Original line number Diff line number Diff line /* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ @file:OptIn(ExperimentalStdlibApi::class, ExperimentalCoroutinesApi::class) package com.android.test.tracing.coroutines import com.android.app.tracing.coroutines.runBlockingTraced import com.android.app.tracing.traceSection import com.android.test.tracing.coroutines.util.FakeTraceState import com.android.test.tracing.coroutines.util.ShadowTrace import kotlin.coroutines.EmptyCoroutineContext import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import org.junit.Assert.assertTrue import org.junit.Test import org.robolectric.annotation.Config @Config(shadows = [ShadowTrace::class]) class RunBlockingTracedTest : TestBase() { @Test fun runBlockingTracedWithSpanNameLambda() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced({ "hello" }) { delay(1) expect(2, "1^main", "hello") true } assertTrue(result) } @Test fun runBlockingTracedWithSpanNameString() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced(spanName = "hello", context = EmptyCoroutineContext) { delay(1) expect(2, "1^main", "hello") true } assertTrue(result) } @Test fun runBlockingTracedWithDefaultSpanNameAndContext() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced { delay(1) expect( 2, "1^main", "RunBlockingTracedTest\$runBlockingTracedWithDefaultSpanNameAndContext\$1\$invokeSuspend\$\$inlined\$runBlockingTraced\$default\$1", ) true } assertTrue(result) } @Test fun runBlockingTracedNestedTraceSections() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced(spanName = { "OuterSpan" }) { traceSection("InnerSpan") { delay(1) expect(2, "1^main", "OuterSpan", "InnerSpan") true } } assertTrue(result) } @Test fun runBlockingTracedWhenTracingDisabled() = runTest(totalEvents = 2) { FakeTraceState.isTracingEnabled = false expect(1, "1^main") val result = runBlockingTraced(spanName = { "NoTraceSpan" }) { delay(1) expect(2, "1^main") true } assertTrue(result) } } Loading
tracinglib/core/src/TraceUtils.kt +4 −3 Original line number Diff line number Diff line Loading @@ -111,13 +111,14 @@ public inline fun <T> traceSection(tag: String, block: () -> T): T { * strings when not needed. */ @OptIn(ExperimentalContracts::class) public inline fun <T> traceSection(tag: () -> String, block: () -> T): T { public inline fun <T> traceSection(tag: () -> String?, block: () -> T): T { contract { callsInPlace(tag, InvocationKind.AT_MOST_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE) } val tracingEnabled = Trace.isEnabled() if (tracingEnabled) beginSlice(tag()) val sliceName = if (Trace.isEnabled()) tag() else null val tracingEnabled = sliceName != null if (tracingEnabled) beginSlice(sliceName!!) return try { block() } finally { Loading
tracinglib/core/src/coroutines/CoroutineTracing.kt +4 −4 Original line number Diff line number Diff line Loading @@ -161,10 +161,10 @@ public suspend inline fun <T> withContextTraced( } /** @see kotlinx.coroutines.runBlocking */ public inline fun <T> runBlockingTraced( crossinline spanName: () -> String, context: CoroutineContext, noinline block: suspend CoroutineScope.() -> T, public fun <T> runBlockingTraced( spanName: () -> String? = { null }, context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T, ): T { contract { callsInPlace(spanName, InvocationKind.AT_MOST_ONCE) Loading
tracinglib/robolectric/src/RunBlockingTracedTest.kt 0 → 100644 +113 −0 Original line number Diff line number Diff line /* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ @file:OptIn(ExperimentalStdlibApi::class, ExperimentalCoroutinesApi::class) package com.android.test.tracing.coroutines import com.android.app.tracing.coroutines.runBlockingTraced import com.android.app.tracing.traceSection import com.android.test.tracing.coroutines.util.FakeTraceState import com.android.test.tracing.coroutines.util.ShadowTrace import kotlin.coroutines.EmptyCoroutineContext import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import org.junit.Assert.assertTrue import org.junit.Test import org.robolectric.annotation.Config @Config(shadows = [ShadowTrace::class]) class RunBlockingTracedTest : TestBase() { @Test fun runBlockingTracedWithSpanNameLambda() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced({ "hello" }) { delay(1) expect(2, "1^main", "hello") true } assertTrue(result) } @Test fun runBlockingTracedWithSpanNameString() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced(spanName = "hello", context = EmptyCoroutineContext) { delay(1) expect(2, "1^main", "hello") true } assertTrue(result) } @Test fun runBlockingTracedWithDefaultSpanNameAndContext() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced { delay(1) expect( 2, "1^main", "RunBlockingTracedTest\$runBlockingTracedWithDefaultSpanNameAndContext\$1\$invokeSuspend\$\$inlined\$runBlockingTraced\$default\$1", ) true } assertTrue(result) } @Test fun runBlockingTracedNestedTraceSections() = runTest(totalEvents = 2) { expect(1, "1^main") val result = runBlockingTraced(spanName = { "OuterSpan" }) { traceSection("InnerSpan") { delay(1) expect(2, "1^main", "OuterSpan", "InnerSpan") true } } assertTrue(result) } @Test fun runBlockingTracedWhenTracingDisabled() = runTest(totalEvents = 2) { FakeTraceState.isTracingEnabled = false expect(1, "1^main") val result = runBlockingTraced(spanName = { "NoTraceSpan" }) { delay(1) expect(2, "1^main") true } assertTrue(result) } }