-
Notifications
You must be signed in to change notification settings - Fork 0
fix: race-safe asyncSem, safe type assertion, atomic shouldNotify #21
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70a375d
fix: race-safe asyncSem, safe type assertion in GetTraceId, atomic sh…
ankurs 17b4eea
fix: address PR review comments
ankurs 31fc081
fix: rewrite bounded concurrency test to be deterministic
ankurs eaa9ebf
fix: escape $ in Makefile bench target for correct regex
ankurs 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package notifier | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/go-coldbrew/errors" | ||
| "github.com/go-coldbrew/options" | ||
| ) | ||
|
|
||
| func TestGetTraceId_NonStringValue(t *testing.T) { | ||
| // Regression test: GetTraceId must not panic when the tracerID | ||
| // option holds a non-string value. | ||
| ctx := options.AddToOptions(context.Background(), tracerID, 12345) | ||
|
|
||
| // Before the fix this panicked with "interface conversion: interface {} is int, not string". | ||
| got := GetTraceId(ctx) | ||
| if got != "" { | ||
| t.Errorf("expected empty string for non-string tracerID, got %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestGetTraceId_StringValue(t *testing.T) { | ||
| ctx := options.AddToOptions(context.Background(), tracerID, "abc-123") | ||
|
|
||
| got := GetTraceId(ctx) | ||
| if got != "abc-123" { | ||
| t.Errorf("expected 'abc-123', got %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestNotifyAsync_BoundedConcurrency(t *testing.T) { | ||
| // Use a 1-slot semaphore and pre-fill it to simulate a full pool. | ||
| ch := make(chan struct{}, 1) | ||
| ch <- struct{}{} // pre-fill: pool is now full | ||
| asyncSem.Store(&ch) | ||
| t.Cleanup(func() { | ||
| // Restore default. Drain first so cleanup is safe. | ||
| for len(ch) > 0 { | ||
| <-ch | ||
| } | ||
| def := make(chan struct{}, 20) | ||
| asyncSem.Store(&def) | ||
| }) | ||
|
|
||
| // With the semaphore full, NotifyAsync must drop (hit default branch). | ||
| // It should not block and should not spawn a goroutine. | ||
| NotifyAsync(errors.New("should-drop")) | ||
|
|
||
| // Verify the semaphore is still exactly full (1 token, capacity 1). | ||
| // If NotifyAsync had somehow acquired a slot, len would be < cap. | ||
| if len(ch) != cap(ch) { | ||
| t.Errorf("expected semaphore to remain full (len=%d, cap=%d); NotifyAsync should have dropped", len(ch), cap(ch)) | ||
| } | ||
| } | ||
|
|
||
| func TestSetMaxAsyncNotifications_ConcurrentAccess(t *testing.T) { | ||
| // Regression test: SetMaxAsyncNotifications and NotifyAsync must not | ||
| // race on the asyncSem variable. Run with -race to verify. | ||
| var wg sync.WaitGroup | ||
|
|
||
| wg.Add(2) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < 100; i++ { | ||
| SetMaxAsyncNotifications(50) | ||
| } | ||
| }() | ||
|
ankurs marked this conversation as resolved.
|
||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < 20; i++ { | ||
| NotifyAsync(errors.New("race test")) | ||
| } | ||
| }() | ||
| wg.Wait() | ||
| } | ||
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.