Skip to content

Commit

Permalink
Verify via test what spans can and can't be parent
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Jul 29, 2024
1 parent bd5d2a5 commit 7e68517
Showing 1 changed file with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.LogType
import io.embrace.android.embracesdk.findEventOfType
import io.embrace.android.embracesdk.findSessionSpan
import io.embrace.android.embracesdk.findSpansByName
import io.embrace.android.embracesdk.internal.EventType
import io.embrace.android.embracesdk.internal.arch.schema.EmbType
import io.embrace.android.embracesdk.internal.payload.Span
Expand All @@ -17,15 +18,16 @@ import io.embrace.android.embracesdk.network.http.HttpMethod
import io.embrace.android.embracesdk.recordSession
import io.embrace.android.embracesdk.spans.ErrorCode
import io.opentelemetry.semconv.HttpAttributes
import java.net.SocketException
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.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import java.net.SocketException

/**
* Validation of the internal API
Expand Down Expand Up @@ -272,6 +274,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
Expand Down

0 comments on commit 7e68517

Please sign in to comment.