mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
fix: Make TestAgent and TestWorkspaceAgentPTY less flaky (#1562)
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user