Skip to content

Commit 06fade9

Browse files
feat(kotlin): Add failing tests (#8238)
1 parent b04a38c commit 06fade9

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.3/src/test/kotlin/KotlinCoroutineTests.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class KotlinCoroutineTests(dispatcher: CoroutineDispatcher) : CoreKotlinCoroutin
6363
return super.tracedWithLazyStarting()
6464
}
6565

66+
@Trace
67+
override fun traceAfterTimeout(): Int {
68+
return super.traceAfterTimeout()
69+
}
70+
6671
@Trace
6772
override fun tracedChild(opName: String) {
6873
super.tracedChild(opName)

dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/groovy/KotlinCoroutineInstrumentationTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datadog.trace.core.DDSpan
22
import datadog.trace.instrumentation.kotlin.coroutines.AbstractKotlinCoroutineInstrumentationTest
33
import kotlinx.coroutines.CoroutineDispatcher
4+
import spock.lang.Ignore
45

56
class KotlinCoroutineInstrumentationTest extends AbstractKotlinCoroutineInstrumentationTest<KotlinCoroutineTests> {
67

@@ -42,4 +43,33 @@ class KotlinCoroutineInstrumentationTest extends AbstractKotlinCoroutineInstrume
4243
where:
4344
[dispatcherName, dispatcher] << dispatchersToTest
4445
}
46+
47+
@Ignore("Not working: disconnected trace")
48+
def "kotlin trace consistent after flow"() {
49+
setup:
50+
KotlinCoroutineTests kotlinTest = new KotlinCoroutineTests(dispatcher)
51+
int expectedNumberOfSpans = kotlinTest.traceAfterFlow()
52+
TEST_WRITER.waitForTraces(1)
53+
54+
expect:
55+
assertTraces(1) {
56+
trace(expectedNumberOfSpans, true) {
57+
span(2) {
58+
operationName "trace.annotation"
59+
parent()
60+
}
61+
span(1) {
62+
operationName "outside-flow"
63+
childOf span(2)
64+
}
65+
span(0) {
66+
operationName "inside-flow"
67+
childOf span(2)
68+
}
69+
}
70+
}
71+
72+
where:
73+
[dispatcherName, dispatcher] << dispatchersToTest
74+
}
4575
}

dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/kotlin/KotlinCoroutineTests.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import datadog.trace.api.Trace
22
import datadog.trace.instrumentation.kotlin.coroutines.CoreKotlinCoroutineTests
33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
44
import kotlinx.coroutines.CoroutineDispatcher
5+
import kotlinx.coroutines.Dispatchers
56
import kotlinx.coroutines.flow.collect
67
import kotlinx.coroutines.flow.flow
78
import kotlinx.coroutines.flow.flowOn
9+
import kotlinx.coroutines.flow.single
810
import kotlinx.coroutines.launch
911
import kotlinx.coroutines.withTimeout
1012

@@ -35,6 +37,23 @@ class KotlinCoroutineTests(dispatcher: CoroutineDispatcher) : CoreKotlinCoroutin
3537
7
3638
}
3739

40+
@Trace
41+
fun traceAfterFlow(): Int = runTest {
42+
val f = flow {
43+
childSpan("inside-flow").activateAndUse {
44+
println("insideFlowSpan")
45+
}
46+
emit(1)
47+
}.flowOn(Dispatchers.IO)
48+
val ff = f.single()
49+
// FIXME: This span is detached
50+
childSpan("outside-flow").activateAndUse {
51+
println("hello $ff")
52+
}
53+
54+
3
55+
}
56+
3857
@Trace
3958
override fun tracePreventedByCancellation(): Int {
4059
return super.tracePreventedByCancellation()
@@ -65,6 +84,11 @@ class KotlinCoroutineTests(dispatcher: CoroutineDispatcher) : CoreKotlinCoroutin
6584
return super.tracedWithLazyStarting()
6685
}
6786

87+
@Trace
88+
override fun traceAfterTimeout(): Int {
89+
return super.traceAfterTimeout()
90+
}
91+
6892
@Trace
6993
override fun tracedChild(opName: String) {
7094
super.tracedChild(opName)

dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/groovy/datadog/trace/instrumentation/kotlin/coroutines/AbstractKotlinCoroutineInstrumentationTest.groovy

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import datadog.trace.core.DDSpan
55
import kotlinx.coroutines.CoroutineDispatcher
66
import kotlinx.coroutines.Dispatchers
77
import kotlinx.coroutines.ThreadPoolDispatcherKt
8+
import spock.lang.Ignore
89
import spock.lang.Shared
910

1011
abstract class AbstractKotlinCoroutineInstrumentationTest<T extends CoreKotlinCoroutineTests> extends AgentTestRunner {
@@ -365,6 +366,46 @@ abstract class AbstractKotlinCoroutineInstrumentationTest<T extends CoreKotlinCo
365366
[dispatcherName, dispatcher] << dispatchersToTest
366367
}
367368

369+
@Ignore("Not working: disconnected trace")
370+
def "kotlin trace consistent with timeout"() {
371+
setup:
372+
CoreKotlinCoroutineTests kotlinTest = getCoreKotlinCoroutineTestsInstance(dispatcher)
373+
int expectedNumberOfSpans = kotlinTest.traceAfterTimeout()
374+
375+
expect:
376+
assertTraces(1) {
377+
trace(expectedNumberOfSpans, true) {
378+
span(5) {
379+
operationName "top-level"
380+
parent()
381+
}
382+
span(0) {
383+
operationName "1-before-timeout"
384+
childOf span(5)
385+
}
386+
span(1) {
387+
operationName "2-inside-timeout"
388+
childOf span(5)
389+
}
390+
span(2) {
391+
operationName "3-after-timeout"
392+
childOf span(5)
393+
}
394+
span(3) {
395+
operationName "4-after-timeout-2"
396+
childOf span(5)
397+
}
398+
span(4) {
399+
operationName "5-after-timeout-3"
400+
childOf span(5)
401+
}
402+
}
403+
}
404+
405+
where:
406+
[dispatcherName, dispatcher] << dispatchersToTest
407+
}
408+
368409
protected static DDSpan findSpan(List<DDSpan> trace, String opName) {
369410
for (DDSpan span : trace) {
370411
if (span.getOperationName() == opName) {

dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/kotlin/datadog/trace/instrumentation/kotlin/coroutines/CoreKotlinCoroutineTests.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,30 @@ abstract class CoreKotlinCoroutineTests(private val dispatcher: CoroutineDispatc
311311
1
312312
}
313313

314+
@Trace
315+
open fun traceAfterTimeout(): Int = runTest {
316+
childSpan("1-before-timeout").activateAndUse {
317+
delay(10)
318+
}
319+
withTimeout(50) {
320+
childSpan("2-inside-timeout").activateAndUse {
321+
delay(10)
322+
}
323+
}
324+
// FIXME: This span is detached
325+
childSpan("3-after-timeout").activateAndUse {
326+
delay(10)
327+
}
328+
childSpan("4-after-timeout-2").activateAndUse {
329+
delay(10)
330+
}
331+
childSpan("5-after-timeout-3").activateAndUse {
332+
delay(10)
333+
}
334+
335+
6
336+
}
337+
314338
@Trace
315339
protected open fun tracedChild(opName: String) {
316340
activeSpan().setSpanName(opName)

0 commit comments

Comments
 (0)