-
Notifications
You must be signed in to change notification settings - Fork 312
Support re-registration of different ContextManagers/Binders during testing #8793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mcculls
merged 3 commits into
master
from
mcculls/support-context-provider-reregistration-during-tests
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
components/context/src/main/java/datadog/context/TestContextBinder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package datadog.context; | ||
|
||
/** Test class that always delegates to the latest registered {@link ContextBinder}. */ | ||
final class TestContextBinder implements ContextBinder { | ||
private static final ContextBinder TEST_INSTANCE = new TestContextBinder(); | ||
|
||
private TestContextBinder() {} | ||
|
||
static boolean register() { | ||
// attempt to register before binder choice is locked, then check if we succeeded | ||
ContextProviders.customBinder = TEST_INSTANCE; | ||
return ContextProviders.binder() == TEST_INSTANCE; | ||
} | ||
|
||
@Override | ||
public Context from(Object carrier) { | ||
return delegate().from(carrier); | ||
} | ||
|
||
@Override | ||
public void attachTo(Object carrier, Context context) { | ||
delegate().attachTo(carrier, context); | ||
} | ||
|
||
@Override | ||
public Context detachFrom(Object carrier) { | ||
return delegate().detachFrom(carrier); | ||
} | ||
|
||
private static ContextBinder delegate() { | ||
ContextBinder delegate = ContextProviders.customBinder; | ||
if (delegate == TEST_INSTANCE) { | ||
// fall back to default context binder | ||
return WeakMapContextBinder.INSTANCE; | ||
} else { | ||
return delegate; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
components/context/src/main/java/datadog/context/TestContextManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package datadog.context; | ||
|
||
/** Test class that always delegates to the latest registered {@link ContextManager}. */ | ||
final class TestContextManager implements ContextManager { | ||
private static final ContextManager TEST_INSTANCE = new TestContextManager(); | ||
|
||
private TestContextManager() {} | ||
|
||
static boolean register() { | ||
// attempt to register before manager choice is locked, then check if we succeeded | ||
ContextProviders.customManager = TEST_INSTANCE; | ||
return ContextProviders.manager() == TEST_INSTANCE; | ||
} | ||
|
||
@Override | ||
public Context current() { | ||
return delegate().current(); | ||
} | ||
|
||
@Override | ||
public ContextScope attach(Context context) { | ||
return delegate().attach(context); | ||
} | ||
|
||
@Override | ||
public Context swap(Context context) { | ||
return delegate().swap(context); | ||
} | ||
|
||
private static ContextManager delegate() { | ||
ContextManager delegate = ContextProviders.customManager; | ||
if (delegate == TEST_INSTANCE) { | ||
// fall back to default context manager | ||
return ThreadLocalContextManager.INSTANCE; | ||
} else { | ||
return delegate; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
components/context/src/test/java/datadog/context/ContextProvidersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package datadog.context; | ||
|
||
import static datadog.context.Context.root; | ||
import static datadog.context.ContextTest.STRING_KEY; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ContextProvidersTest { | ||
@Test | ||
void testCannotChangeBinderAfterUse() { | ||
Context context = root().with(STRING_KEY, "value"); | ||
Object carrier = new Object(); | ||
|
||
context.attachTo(carrier); | ||
Context.detachFrom(carrier); | ||
|
||
// cannot change binder at this late stage | ||
assertFalse(ContextBinder.allowTesting()); | ||
} | ||
|
||
@Test | ||
void testCannotChangeManagerAfterUse() { | ||
Context context = root().with(STRING_KEY, "value"); | ||
|
||
try (ContextScope ignored = context.attach()) { | ||
assertNotEquals(root(), Context.current()); | ||
} | ||
|
||
// cannot change manager at this late stage | ||
assertFalse(ContextManager.allowTesting()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.