Skip to content

Commit c0d7879

Browse files
committed
Fix nested activation overflow test
1 parent a4c98fd commit c0d7879

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

apm-agent-core/src/test/java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import co.elastic.apm.agent.common.util.WildcardMatcher;
2323
import co.elastic.apm.agent.configuration.AutoDetectedServiceInfo;
2424
import co.elastic.apm.agent.configuration.CoreConfiguration;
25+
import co.elastic.apm.agent.impl.baggage.BaggageContext;
2526
import co.elastic.apm.agent.tracer.service.ServiceInfo;
2627
import co.elastic.apm.agent.configuration.SpyConfiguration;
2728
import co.elastic.apm.agent.configuration.source.ConfigSources;
@@ -374,48 +375,61 @@ void testActivationStackOverflow() {
374375
.withObjectPoolFactory(objectPoolFactory)
375376
.buildAndStart();
376377

377-
Transaction transaction = tracer.startRootTransaction(getClass().getClassLoader());
378-
assertThat(tracer.getActive()).isNull();
379-
try (Scope scope = transaction.activateInScope()) {
380-
assertThat(tracer.getActive()).isEqualTo(transaction);
381-
Span child1 = transaction.createSpan();
382-
try (Scope childScope = child1.activateInScope()) {
383-
assertThat(tracer.getActive()).isEqualTo(child1);
384-
Span grandchild1 = child1.createSpan();
385-
try (Scope grandchildScope = grandchild1.activateInScope()) {
386-
// latter activation should not be applied due to activation stack overflow
378+
doWithNestedBaggageActivations(() -> {
379+
Transaction transaction = tracer.startRootTransaction(getClass().getClassLoader());
380+
assertThat(tracer.getActive()).isNull();
381+
try (Scope scope = transaction.activateInScope()) {
382+
assertThat(tracer.getActive()).isEqualTo(transaction);
383+
Span child1 = transaction.createSpan();
384+
try (Scope childScope = child1.activateInScope()) {
387385
assertThat(tracer.getActive()).isEqualTo(child1);
388-
Span ggc = grandchild1.createSpan();
389-
try (Scope ggcScope = ggc.activateInScope()) {
386+
Span grandchild1 = child1.createSpan();
387+
try (Scope grandchildScope = grandchild1.activateInScope()) {
388+
// latter activation should not be applied due to activation stack overflow
390389
assertThat(tracer.getActive()).isEqualTo(child1);
391-
ggc.end();
390+
Span ggc = grandchild1.createSpan();
391+
try (Scope ggcScope = ggc.activateInScope()) {
392+
assertThat(tracer.getActive()).isEqualTo(child1);
393+
ggc.end();
394+
}
395+
grandchild1.end();
392396
}
393-
grandchild1.end();
397+
assertThat(tracer.getActive()).isEqualTo(child1);
398+
child1.end();
394399
}
395-
assertThat(tracer.getActive()).isEqualTo(child1);
396-
child1.end();
397-
}
398-
assertThat(tracer.getActive()).isEqualTo(transaction);
399-
Span child2 = transaction.createSpan();
400-
try (Scope childScope = child2.activateInScope()) {
401-
assertThat(tracer.getActive()).isEqualTo(child2);
402-
Span grandchild2 = child2.createSpan();
403-
try (Scope grandchildScope = grandchild2.activateInScope()) {
404-
// latter activation should not be applied due to activation stack overflow
400+
assertThat(tracer.getActive()).isEqualTo(transaction);
401+
Span child2 = transaction.createSpan();
402+
try (Scope childScope = child2.activateInScope()) {
403+
assertThat(tracer.getActive()).isEqualTo(child2);
404+
Span grandchild2 = child2.createSpan();
405+
try (Scope grandchildScope = grandchild2.activateInScope()) {
406+
// latter activation should not be applied due to activation stack overflow
407+
assertThat(tracer.getActive()).isEqualTo(child2);
408+
grandchild2.end();
409+
}
405410
assertThat(tracer.getActive()).isEqualTo(child2);
406-
grandchild2.end();
411+
child2.end();
407412
}
408-
assertThat(tracer.getActive()).isEqualTo(child2);
409-
child2.end();
413+
assertThat(tracer.getActive()).isEqualTo(transaction);
414+
transaction.end();
410415
}
411-
assertThat(tracer.getActive()).isEqualTo(transaction);
412-
transaction.end();
413-
}
416+
}, tracer, 16);
414417
assertThat(tracer.getActive()).isNull();
415418
assertThat(reporter.getTransactions()).hasSize(1);
416419
assertThat(reporter.getSpans()).hasSize(2);
417420
}
418421

422+
private void doWithNestedBaggageActivations(Runnable r, Tracer tracer, int nestedCount) {
423+
if (nestedCount == 0) {
424+
r.run();
425+
return;
426+
}
427+
BaggageContext baggageContext = tracer.currentContext().withUpdatedBaggage().buildContext();
428+
try (Scope scope = baggageContext.activateInScope()) {
429+
doWithNestedBaggageActivations(r, tracer, nestedCount - 1);
430+
}
431+
}
432+
419433
@Test
420434
void testPause() {
421435
tracerImpl.pause();

0 commit comments

Comments
 (0)