-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Refactor testcontext APIs #10782
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
Open
stephanos
wants to merge
2
commits into
main
Choose a base branch
from
stephanos/testcontext-api-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor testcontext APIs #10782
Changes from all commits
Commits
Show all changes
2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,24 +30,28 @@ var testContexts = contextStore{ | |
| type config struct { | ||
| timeout time.Duration | ||
| timeoutSet bool | ||
| decorators []contextDecorator | ||
| } | ||
|
|
||
| type contextDecorator struct { | ||
| key any | ||
| decorate func(context.Context) context.Context | ||
| } | ||
|
|
||
| // New returns the test-scoped context for tb. The context is canceled when the | ||
| // test ends or when the configured test timeout expires. | ||
| // DefaultTimeout returns the effective default timeout for test-scoped contexts. | ||
| func DefaultTimeout() time.Duration { | ||
| return effectiveTimeout(0) | ||
| } | ||
|
|
||
| // For returns the test-scoped context for tb. The context is canceled | ||
| // when the test ends or when the configured test timeout expires. | ||
| // | ||
| // The first call creates the per-test context and fixes its timeout. Later calls | ||
| // may add decorators, but an explicit different timeout fails instead of being | ||
| // silently ignored. | ||
| func New(tb testing.TB, opts ...Option) context.Context { | ||
| // return the same context, but an explicit different timeout fails instead of | ||
| // being silently ignored. | ||
| func For(tb testing.TB, opts ...Option) context.Context { | ||
| tb.Helper() | ||
|
|
||
| cfg := config{timeout: effectiveTimeout(0)} | ||
| cfg := config{timeout: DefaultTimeout()} | ||
| for _, opt := range opts { | ||
| opt(&cfg) | ||
| } | ||
|
|
@@ -57,7 +61,7 @@ func New(tb testing.TB, opts ...Option) context.Context { | |
| return st.context() | ||
| } | ||
|
|
||
| // Option configures the test-scoped context returned by [New]. | ||
| // Option configures the test-scoped context returned by [For]. | ||
| type Option func(*config) | ||
|
|
||
| // WithTimeout sets a custom timeout for the test-scoped context. | ||
|
|
@@ -71,15 +75,18 @@ func WithTimeout(timeout time.Duration) Option { | |
| } | ||
| } | ||
|
|
||
| // WithContextDecorator applies decorator to the test-scoped context once for key. | ||
| // Reusing the same key is a no-op. | ||
| func WithContextDecorator[K comparable](key K, decorator func(context.Context) context.Context) Option { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was awkward as it had to be used together with |
||
| return func(cfg *config) { | ||
| cfg.decorators = append(cfg.decorators, contextDecorator{ | ||
| key: key, | ||
| decorate: decorator, | ||
| }) | ||
| } | ||
| // AttachDecorator applies decorator to the test-scoped context once for key. | ||
| // Reusing the same key is a no-op. If the test context does not exist yet, | ||
| // AttachDecorator creates it with the default timeout. Call [For] with [WithTimeout] | ||
| // first when using a custom timeout. | ||
| func AttachDecorator[K comparable](tb testing.TB, key K, decorator func(context.Context) context.Context) { | ||
| tb.Helper() | ||
|
|
||
| st := getContextState(tb, DefaultTimeout()) | ||
| st.attachDecorator(tb, contextDecorator{ | ||
| key: key, | ||
| decorate: decorator, | ||
| }) | ||
| } | ||
|
|
||
| type contextState struct { | ||
|
|
@@ -114,12 +121,13 @@ func getContextState(tb testing.TB, timeout time.Duration) *contextState { | |
| testContexts.byTest[tb] = st | ||
|
|
||
| tb.Cleanup(func() { | ||
| err := st.err() | ||
| st.cancel() | ||
| testContexts.Lock() | ||
| delete(testContexts.byTest, tb) | ||
| testContexts.Unlock() | ||
| if st.err() == context.DeadlineExceeded { | ||
| tb.Errorf("Test exceeded timeout of %v", st.timeout) | ||
| if err == context.DeadlineExceeded { | ||
| tb.Errorf("test exceeded timeout of %v", st.timeout) | ||
| } | ||
| }) | ||
| return st | ||
|
|
@@ -135,21 +143,27 @@ func (s *contextState) configure(tb testing.TB, cfg config) { | |
| tb.Fatalf("testcontext: test context already exists with timeout %v; cannot change it to %v", s.timeout, cfg.timeout) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func (s *contextState) attachDecorator(tb testing.TB, decorator contextDecorator) { | ||
| tb.Helper() | ||
|
|
||
| // Decorators may be registered by independent helpers, so apply each keyed | ||
| // decorator at most once while preserving call order. | ||
| for _, decorator := range cfg.decorators { | ||
| if decorator.key == nil { | ||
| tb.Fatal("testcontext: context decorator key must not be nil") | ||
| } | ||
| if decorator.decorate == nil { | ||
| tb.Fatal("testcontext: context decorator must not be nil") | ||
| } | ||
| if _, ok := s.decorators[decorator.key]; ok { | ||
| continue | ||
| } | ||
| s.ctx = decorator.decorate(s.ctx) | ||
| s.decorators[decorator.key] = struct{}{} | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
|
|
||
| if decorator.key == nil { | ||
| tb.Fatal("testcontext: context decorator key must not be nil") | ||
| } | ||
| if decorator.decorate == nil { | ||
| tb.Fatal("testcontext: context decorator must not be nil") | ||
| } | ||
| if _, ok := s.decorators[decorator.key]; ok { | ||
| return | ||
| } | ||
| s.ctx = decorator.decorate(s.ctx) | ||
| s.decorators[decorator.key] = struct{}{} | ||
| } | ||
|
|
||
| func (s *contextState) context() context.Context { | ||
|
|
@@ -182,6 +196,6 @@ func effectiveTimeout(customTimeout time.Duration) (timeout time.Duration) { | |
| } | ||
| } | ||
|
|
||
| // 3. Default 90 seconds. | ||
| // 3. Default timeout. | ||
| return defaultTimeout | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newwasn't quite right as this doesn't create one if it already exists.