Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Make TestAgent and TestWorkspaceAgentPTY less flaky #1562

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 3 commits into from
May 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix TestWorkspaceAgentPTY
  • Loading branch information
dwahler committed May 18, 2022
commit 0696b4029416f28c553bda4a3e63f20f485a4890
25 changes: 19 additions & 6 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package coderd_test

import (
"bufio"
"context"
"encoding/json"
"runtime"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/pion/webrtc/v3"
Expand Down Expand Up @@ -230,6 +232,11 @@ func TestWorkspaceAgentPTY(t *testing.T) {
require.NoError(t, err)
_, err = conn.Write(data)
require.NoError(t, err)
bufRead := bufio.NewReader(conn)

// Brief pause to reduce the likelihood that we send keystrokes while
// the shell is simultaneously sending a prompt.
time.Sleep(100 * time.Millisecond)

data, err = json.Marshal(agent.ReconnectingPTYRequest{
Data: "echo test\r\n",
Expand All @@ -238,16 +245,22 @@ func TestWorkspaceAgentPTY(t *testing.T) {
_, err = conn.Write(data)
require.NoError(t, err)

findEcho := func() {
expectLine := func(matcher func(string) bool) {
for {
read, err := conn.Read(data)
line, err := bufRead.ReadString('\n')
require.NoError(t, err)
if strings.Contains(string(data[:read]), "test") {
return
if matcher(line) {
break
}
}
}
matchEchoCommand := func(line string) bool {
return strings.Contains(line, "echo test")
}
matchEchoOutput := func(line string) bool {
return strings.Contains(line, "test") && !strings.Contains(line, "echo")
}

findEcho()
findEcho()
expectLine(matchEchoCommand)
expectLine(matchEchoOutput)
}