Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion components/context/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
apply(from = "$rootDir/gradle/java.gradle")

extra["excludedClassesInstructionCoverage"] = listOf("datadog.context.ContextProviders") // covered by forked test
extra["excludedClassesInstructionCoverage"] =
listOf("datadog.context.ContextProviders") // covered by forked test

// excluded from the default 90% rule so the relaxed 80% rule below can apply instead
// (couple of branches involve a nanosecond CAS race that can't be reliably reproduced)
extra["excludedClassesBranchCoverage"] =
listOf("datadog.context.ThreadLocalContextManager.ContextContinuationImpl")

tasks.named<org.gradle.testing.jacoco.tasks.JacocoCoverageVerification>("jacocoTestCoverageVerification") {
violationRules {
rule {
element = "CLASS"
includes = listOf("datadog.context.ThreadLocalContextManager.ContextContinuationImpl")
limit {
counter = "BRANCH"
minimum = "0.8".toBigDecimal()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import static datadog.context.Context.root;
import static datadog.context.ContextTest.STRING_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static datadog.context.ContextTestBase.trackingListener;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import org.junit.jupiter.api.Test;

Expand All @@ -15,13 +18,15 @@ void testCustomBinder() {
assertTrue(ContextBinder.allowTesting());

Context context = root().with(STRING_KEY, "value");
assertNotEquals(root(), context);

Object carrier = new Object();

// should delegate to the default binder
context.attachTo(carrier);
assertNotEquals(root(), Context.from(carrier));
assertEquals(context, Context.detachFrom(carrier));
assertEquals(root(), Context.from(carrier));
assertSame(context, Context.from(carrier));
assertSame(context, Context.detachFrom(carrier));
assertSame(root(), Context.from(carrier));

// now register a NOOP context binder
ContextBinder.register(
Expand All @@ -44,24 +49,32 @@ public Context detachFrom(@Nonnull Object carrier) {

// NOOP binder, context will always be root
context.attachTo(carrier);
assertEquals(root(), Context.from(carrier));
assertEquals(root(), Context.detachFrom(carrier));
assertSame(root(), Context.from(carrier));
assertSame(root(), Context.detachFrom(carrier));
assertSame(root(), Context.from(carrier));
}

@Test
void testCustomManager() {
assertTrue(ContextManager.allowTesting());

Context context = root().with(STRING_KEY, "value");
assertNotEquals(root(), context);

// should delegate to the default manager
try (ContextScope ignored = context.attach()) {
assertNotEquals(root(), Context.current());
try (ContextScope scope = context.attach()) {
assertSame(context, scope.context());
assertSame(context, Context.current());
ContextContinuation cont = context.capture();
assertSame(context, cont.context());
cont.release();
}

Context swapped = context.swap();
assertNotEquals(root(), Context.current());
swapped.swap();
assertSame(root(), swapped);
assertSame(context, Context.current());
assertSame(context, swapped.swap());
assertSame(root(), Context.current());

// now register a NOOP context manager
ContextManager.register(
Expand Down Expand Up @@ -90,14 +103,26 @@ public ContextContinuation capture(Context context) {
public void addListener(ContextListener listener) {}
});

List<String> events = new ArrayList<>();
ContextManager.register(trackingListener(events));

// NOOP manager, context will always be root
try (ContextScope ignored = context.attach()) {
assertEquals(root(), Context.current());
try (ContextScope scope = context.attach()) {
assertSame(root(), scope.context());
assertSame(root(), Context.current());
ContextContinuation cont = context.capture();
assertSame(root(), cont.context());
cont.release();
}

// NOOP manager, context will always be root
swapped = context.swap();
assertEquals(root(), Context.current());
swapped.swap();
assertSame(root(), swapped);
assertSame(root(), Context.current());
assertSame(root(), swapped.swap());
assertSame(root(), Context.current());

// NOOP manager, no events emitted
assertTrue(events.isEmpty());
}
}