fix: Make TestAgent and TestWorkspaceAgentPTY less flaky (#1562)

This commit is contained in:
David Wahler
2022-05-18 12:06:17 -05:00
committed by GitHub
parent 9f402fa27f
commit a50a6e8638
2 changed files with 39 additions and 13 deletions

View File

@ -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"
@ -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",
@ -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)
}