-
Notifications
You must be signed in to change notification settings - Fork 4.7k
stats/otel: a79 scaffolding to register an async gauge metric and api to record it- part 3 #8780
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
Changes from all commits
4a6f222
2431d5d
02961a3
51561dd
6d78be5
e1c3b2e
6ab145e
b368faf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
arjan-bal marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,3 +132,58 @@ func TestTrackTimers(t *testing.T) { | |
| defer cancel() | ||
| CheckTimers(ctx, t) | ||
| } | ||
|
|
||
| func TestLeakChecker_DetectsLeak(t *testing.T) { | ||
| // 1. Setup the tracker (swaps the delegate in internal). | ||
|
Contributor
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. nit: Please avoid putting numbers in the comments. The step numbers may go out of sync with the code when it's changed.
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. Will be doing this in follow up PR. |
||
| TrackAsyncReporters() | ||
|
|
||
| // Safety defer: ensure we restore the default delegate even if the test crashes | ||
| // before CheckAsyncReporters is called. | ||
| defer func() { | ||
| internal.AsyncReporterCleanupDelegate = func(f func()) func() { return f } | ||
| }() | ||
|
|
||
| // 2. Simulate a registration using the swapped delegate. | ||
| // We utilize the internal delegate directly to simulate stats.RegisterAsyncReporter behavior. | ||
| noOpCleanup := func() {} | ||
| wrappedCleanup := internal.AsyncReporterCleanupDelegate(noOpCleanup) | ||
|
|
||
| // 3. Create a leak: We discard 'wrappedCleanup' without calling it. | ||
| _ = wrappedCleanup | ||
|
|
||
| // 4. Check for leaks. | ||
| tl := &testLogger{} | ||
| CheckAsyncReporters(tl) | ||
|
|
||
| // 5. Assertions. | ||
| if tl.errorCount == 0 { | ||
| t.Error("Expected leak checker to report a leak, but it succeeded silently.") | ||
| } | ||
| if asyncReporterTracker != nil { | ||
| t.Error("Expected CheckAsyncReporters to cleanup global tracker, but it was not nil.") | ||
| } | ||
| } | ||
|
|
||
| func TestLeakChecker_PassesOnCleanup(t *testing.T) { | ||
| // 1. Setup. | ||
| TrackAsyncReporters() | ||
| defer func() { | ||
| internal.AsyncReporterCleanupDelegate = func(f func()) func() { return f } | ||
| }() | ||
|
|
||
| // 2. Simulate registration. | ||
| noOpCleanup := func() {} | ||
| wrappedCleanup := internal.AsyncReporterCleanupDelegate(noOpCleanup) | ||
|
|
||
| // 3. Behave correctly: Call the cleanup. | ||
| wrappedCleanup() | ||
|
|
||
| // 4. Check for leaks. | ||
| tl := &testLogger{} | ||
| CheckAsyncReporters(tl) | ||
|
|
||
| // 5. Assertions. | ||
| if tl.errorCount > 0 { | ||
| t.Errorf("Expected no leaks, but got errors: %v", tl.errors) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.