-
Notifications
You must be signed in to change notification settings - Fork 580
Improve abort tests across all SDKs; add Go unsubscribe tests #48
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
8 commits
Select commit
Hold shift + click to select a range
c5f549e
Fix session event handler unsubscription and add tests
SteveSandersonMS 3a8c2a5
Fix abort test to use non-blocking send()
SteveSandersonMS 40ae753
Clean up "should abort a session"
SteveSandersonMS 70817fa
Add equivalent abort test improvements to Go, Python, and .NET
SteveSandersonMS 2017338
Formatting
SteveSandersonMS 1744bb1
More lint/format
SteveSandersonMS f9dbbfd
Update test_session.py
SteveSandersonMS c657c9b
Fix risk of flakiness
SteveSandersonMS 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
4 changes: 2 additions & 2 deletions
4
go/generated/session_events.go → go/generated_session_events.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,121 @@ | ||
| package copilot | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSession_On(t *testing.T) { | ||
| t.Run("multiple handlers all receive events", func(t *testing.T) { | ||
| session := &Session{ | ||
| handlers: make([]sessionHandler, 0), | ||
| } | ||
|
|
||
| var received1, received2, received3 bool | ||
| session.On(func(event SessionEvent) { received1 = true }) | ||
| session.On(func(event SessionEvent) { received2 = true }) | ||
| session.On(func(event SessionEvent) { received3 = true }) | ||
|
|
||
| session.dispatchEvent(SessionEvent{Type: "test"}) | ||
|
|
||
| if !received1 || !received2 || !received3 { | ||
| t.Errorf("Expected all handlers to receive event, got received1=%v, received2=%v, received3=%v", | ||
| received1, received2, received3) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("unsubscribing one handler does not affect others", func(t *testing.T) { | ||
| session := &Session{ | ||
| handlers: make([]sessionHandler, 0), | ||
| } | ||
|
|
||
| var count1, count2, count3 int | ||
| session.On(func(event SessionEvent) { count1++ }) | ||
| unsub2 := session.On(func(event SessionEvent) { count2++ }) | ||
| session.On(func(event SessionEvent) { count3++ }) | ||
|
|
||
| // First event - all handlers receive it | ||
| session.dispatchEvent(SessionEvent{Type: "test"}) | ||
|
|
||
| // Unsubscribe handler 2 | ||
| unsub2() | ||
|
|
||
| // Second event - only handlers 1 and 3 should receive it | ||
| session.dispatchEvent(SessionEvent{Type: "test"}) | ||
|
|
||
| if count1 != 2 { | ||
| t.Errorf("Expected handler 1 to receive 2 events, got %d", count1) | ||
| } | ||
| if count2 != 1 { | ||
| t.Errorf("Expected handler 2 to receive 1 event (before unsubscribe), got %d", count2) | ||
| } | ||
| if count3 != 2 { | ||
| t.Errorf("Expected handler 3 to receive 2 events, got %d", count3) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("calling unsubscribe multiple times is safe", func(t *testing.T) { | ||
| session := &Session{ | ||
| handlers: make([]sessionHandler, 0), | ||
| } | ||
|
|
||
| var count int | ||
| unsub := session.On(func(event SessionEvent) { count++ }) | ||
|
|
||
| session.dispatchEvent(SessionEvent{Type: "test"}) | ||
|
|
||
| // Call unsubscribe multiple times - should not panic | ||
| unsub() | ||
| unsub() | ||
| unsub() | ||
|
|
||
| session.dispatchEvent(SessionEvent{Type: "test"}) | ||
|
|
||
| if count != 1 { | ||
| t.Errorf("Expected handler to receive 1 event, got %d", count) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("handlers are called in registration order", func(t *testing.T) { | ||
| session := &Session{ | ||
| handlers: make([]sessionHandler, 0), | ||
| } | ||
|
|
||
| var order []int | ||
| session.On(func(event SessionEvent) { order = append(order, 1) }) | ||
| session.On(func(event SessionEvent) { order = append(order, 2) }) | ||
| session.On(func(event SessionEvent) { order = append(order, 3) }) | ||
|
|
||
| session.dispatchEvent(SessionEvent{Type: "test"}) | ||
|
|
||
| if len(order) != 3 || order[0] != 1 || order[1] != 2 || order[2] != 3 { | ||
| t.Errorf("Expected handlers to be called in order [1,2,3], got %v", order) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("concurrent subscribe and unsubscribe is safe", func(t *testing.T) { | ||
| session := &Session{ | ||
| handlers: make([]sessionHandler, 0), | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < 100; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| unsub := session.On(func(event SessionEvent) {}) | ||
| unsub() | ||
| }() | ||
| } | ||
| wg.Wait() | ||
|
|
||
| // Should not panic and handlers should be empty | ||
| session.handlerMutex.RLock() | ||
| count := len(session.handlers) | ||
| session.handlerMutex.RUnlock() | ||
|
|
||
| if count != 0 { | ||
| t.Errorf("Expected 0 handlers after all unsubscribes, got %d", count) | ||
| } | ||
| }) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.