-
Notifications
You must be signed in to change notification settings - Fork 2
fix: receive loop could break if re-auth was called more than once at… #206
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
+162
−14
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/rs/zerolog" | ||
| "maunium.net/go/mautrix/bridgev2" | ||
|
|
||
| "github.com/highesttt/matrix-line-messenger/pkg/line" | ||
| ) | ||
|
|
||
| func TestPollLoopRebuildsSSEClientAfterReconnect(t *testing.T) { | ||
| oldGetLastOpRevision := getLastOpRevisionWithClient | ||
| oldListenSSE := listenSSEWithClient | ||
| oldReconnectDelay := sseReconnectDelay | ||
| t.Cleanup(func() { | ||
| getLastOpRevisionWithClient = oldGetLastOpRevision | ||
| listenSSEWithClient = oldListenSSE | ||
| sseReconnectDelay = oldReconnectDelay | ||
| }) | ||
|
|
||
| getLastOpRevisionWithClient = func(*line.Client) (int64, error) { | ||
| return 1234, nil | ||
| } | ||
| sseReconnectDelay = time.Millisecond | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| lc := &LineClient{ | ||
| AccessToken: "old", | ||
| UserLogin: &bridgev2.UserLogin{ | ||
| Bridge: &bridgev2.Bridge{Log: zerolog.New(io.Discard)}, | ||
| }, | ||
| } | ||
|
|
||
| var tokens []string | ||
| listenSSEWithClient = func(client *line.Client, ctx context.Context, localRev int64, handler func(eventType, data string)) error { | ||
| if localRev != 1234 { | ||
| t.Fatalf("localRev = %d, want 1234", localRev) | ||
| } | ||
| tokens = append(tokens, client.AccessToken) | ||
| if len(tokens) == 1 { | ||
| lc.setTokens("new", "") | ||
| return io.EOF | ||
| } | ||
| cancel() | ||
| return context.Canceled | ||
| } | ||
|
|
||
| lc.wg.Add(1) | ||
| lc.pollLoop(ctx) | ||
|
|
||
| if len(tokens) != 2 { | ||
| t.Fatalf("SSE attempts = %d, want 2", len(tokens)) | ||
| } | ||
| if tokens[0] != "old" || tokens[1] != "new" { | ||
| t.Fatalf("SSE tokens = %v, want [old new]", tokens) | ||
| } | ||
| } |
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,82 @@ | ||
| package line | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestChannelCreateWithWASMOnlyKeyDoesNotWriteStdout(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("skipping runner integration test in short mode") | ||
| } | ||
|
|
||
| r, err := GetRunner() | ||
| if err != nil { | ||
| t.Fatalf("GetRunner failed: %v", err) | ||
| } | ||
|
|
||
| keyID, err := r.KeyGenerate() | ||
| if err != nil { | ||
| t.Fatalf("KeyGenerate failed: %v", err) | ||
| } | ||
| peerKeyID, err := r.KeyGenerate() | ||
| if err != nil { | ||
| t.Fatalf("peer KeyGenerate failed: %v", err) | ||
| } | ||
| peerPub, err := r.KeyGetPublic(peerKeyID) | ||
| if err != nil { | ||
| t.Fatalf("KeyGetPublic failed: %v", err) | ||
| } | ||
|
|
||
| var channelID int | ||
| stdout := captureStdout(t, func() { | ||
| channelID, err = r.ChannelCreate(keyID, peerPub) | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("ChannelCreate failed: %v", err) | ||
| } | ||
| if channelID == 0 { | ||
| t.Fatal("ChannelCreate returned empty channel ID") | ||
| } | ||
| if stdout != "" { | ||
| t.Fatalf("ChannelCreate wrote to stdout: %q", stdout) | ||
| } | ||
| } | ||
|
|
||
| func captureStdout(t *testing.T, fn func()) string { | ||
| t.Helper() | ||
|
|
||
| oldStdout := os.Stdout | ||
| readPipe, writePipe, err := os.Pipe() | ||
| if err != nil { | ||
| t.Fatalf("os.Pipe failed: %v", err) | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| done := make(chan error, 1) | ||
| go func() { | ||
| _, copyErr := io.Copy(&buf, readPipe) | ||
| done <- copyErr | ||
| }() | ||
|
|
||
| os.Stdout = writePipe | ||
| defer func() { | ||
| os.Stdout = oldStdout | ||
| }() | ||
| fn() | ||
| os.Stdout = oldStdout | ||
|
|
||
| if err := writePipe.Close(); err != nil { | ||
| t.Fatalf("closing stdout pipe writer failed: %v", err) | ||
| } | ||
| if err := <-done; err != nil { | ||
| t.Fatalf("reading captured stdout failed: %v", err) | ||
| } | ||
| if err := readPipe.Close(); err != nil { | ||
| t.Fatalf("closing stdout pipe reader failed: %v", err) | ||
| } | ||
|
|
||
| return buf.String() | ||
| } |
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.
Nit — ctx should come first: Go convention is to pass
context.Contextas the first parameter of a function. The underlyingclient.ListenSSE(ctx, localRev, handler)already follows that convention, so this wrapper reads a bit awkward. Consider reordering tofunc(ctx context.Context, client *line.Client, localRev int64, handler func(eventType, data string)) errorfor consistency.