From b22a84eb42354d43c793a78e7fd19fe797a5b990 Mon Sep 17 00:00:00 2001 From: bidetofevil Date: Fri, 26 Jul 2024 16:19:44 -0700 Subject: [PATCH] Verify via test what spans can and can't be parent --- .../testcases/EmbraceInternalInterfaceTest.kt | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt b/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt index 2112106ca0..c408965330 100644 --- a/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt +++ b/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt @@ -4,30 +4,32 @@ package io.embrace.android.embracesdk.testcases import android.os.Build import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.embrace.android.embracesdk.internal.EventType import io.embrace.android.embracesdk.IntegrationTestRule import io.embrace.android.embracesdk.LogType -import io.embrace.android.embracesdk.internal.arch.schema.EmbType import io.embrace.android.embracesdk.findEventOfType import io.embrace.android.embracesdk.findSessionSpan +import io.embrace.android.embracesdk.findSpansByName import io.embrace.android.embracesdk.internal.ApkToolsConfig +import io.embrace.android.embracesdk.internal.EventType +import io.embrace.android.embracesdk.internal.arch.schema.EmbType import io.embrace.android.embracesdk.internal.payload.Span import io.embrace.android.embracesdk.internal.spans.findAttributeValue import io.embrace.android.embracesdk.network.EmbraceNetworkRequest import io.embrace.android.embracesdk.network.http.HttpMethod import io.embrace.android.embracesdk.recordSession import io.embrace.android.embracesdk.spans.ErrorCode -import java.net.SocketException import io.opentelemetry.semconv.HttpAttributes import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.annotation.Config +import java.net.SocketException /** * Validation of the internal API @@ -280,6 +282,57 @@ internal class EmbraceInternalInterfaceTest { } } + @Test + fun `span logging across sessions`() { + with(testRule) { + startSdk(context = harness.overriddenCoreModule.context) + val internalInterface = checkNotNull(embrace.internalInterface) + var stoppedParentId = "" + var activeParentId = "" + val s1 = checkNotNull(harness.recordSession { + stoppedParentId = checkNotNull(internalInterface.startSpan("parent")) + activeParentId = checkNotNull(internalInterface.startSpan("active-parent")) + assertTrue( + internalInterface.stopSpan( + checkNotNull( + internalInterface.startSpan( + name = "child", + parentSpanId = stoppedParentId + ) + ) + ) + ) + assertTrue(internalInterface.stopSpan(stoppedParentId)) + }) + + val s2 = checkNotNull(harness.recordSession { + assertNull(internalInterface.startSpan(name = "stopped-parent-child", parentSpanId = stoppedParentId)) + assertTrue( + internalInterface.stopSpan( + checkNotNull( + internalInterface.startSpan( + name = "active-parent-child", + parentSpanId = activeParentId + ) + ) + ) + ) + assertTrue(internalInterface.stopSpan(activeParentId)) + }) + + assertEquals(1, s1.findSpansByName("parent").size) + assertEquals(1, s1.findSpansByName("child").size) + assertEquals(0, s1.findSpansByName("active-parent").size) + + // spans stopped in a previous session cannot be a valid parent + assertEquals(0, s2.findSpansByName("stopped-parent-child").size) + + // active spans started in a previous session is a valid parent + assertEquals(1, s2.findSpansByName("active-parent-child").size) + assertEquals(1, s2.findSpansByName("active-parent").size) + } + } + companion object { private const val URL = "https://embrace.io" private const val START_TIME = 1692201601000L