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

Skip to content

Commit c82c724

Browse files
dwahlerkylecarbs
authored andcommitted
fix: Make TestAgent and TestWorkspaceAgentPTY less flaky (#1562)
1 parent f4a2d28 commit c82c724

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

agent/agent_test.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package agent_test
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -204,6 +205,11 @@ func TestAgent(t *testing.T) {
204205
id := uuid.NewString()
205206
netConn, err := conn.ReconnectingPTY(id, 100, 100)
206207
require.NoError(t, err)
208+
bufRead := bufio.NewReader(netConn)
209+
210+
// Brief pause to reduce the likelihood that we send keystrokes while
211+
// the shell is simultaneously sending a prompt.
212+
time.Sleep(100 * time.Millisecond)
207213

208214
data, err := json.Marshal(agent.ReconnectingPTYRequest{
209215
Data: "echo test\r\n",
@@ -212,28 +218,35 @@ func TestAgent(t *testing.T) {
212218
_, err = netConn.Write(data)
213219
require.NoError(t, err)
214220

215-
findEcho := func() {
221+
expectLine := func(matcher func(string) bool) {
216222
for {
217-
read, err := netConn.Read(data)
223+
line, err := bufRead.ReadString('\n')
218224
require.NoError(t, err)
219-
if strings.Contains(string(data[:read]), "test") {
225+
if matcher(line) {
220226
break
221227
}
222228
}
223229
}
230+
matchEchoCommand := func(line string) bool {
231+
return strings.Contains(line, "echo test")
232+
}
233+
matchEchoOutput := func(line string) bool {
234+
return strings.Contains(line, "test") && !strings.Contains(line, "echo")
235+
}
224236

225237
// Once for typing the command...
226-
findEcho()
238+
expectLine(matchEchoCommand)
227239
// And another time for the actual output.
228-
findEcho()
240+
expectLine(matchEchoOutput)
229241

230242
_ = netConn.Close()
231243
netConn, err = conn.ReconnectingPTY(id, 100, 100)
232244
require.NoError(t, err)
245+
bufRead = bufio.NewReader(netConn)
233246

234247
// Same output again!
235-
findEcho()
236-
findEcho()
248+
expectLine(matchEchoCommand)
249+
expectLine(matchEchoOutput)
237250
})
238251

239252
t.Run("Dial", func(t *testing.T) {

coderd/workspaceagents_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package coderd_test
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
67
"runtime"
78
"strings"
89
"testing"
10+
"time"
911

1012
"github.com/google/uuid"
1113
"github.com/pion/webrtc/v3"
@@ -230,6 +232,11 @@ func TestWorkspaceAgentPTY(t *testing.T) {
230232
require.NoError(t, err)
231233
_, err = conn.Write(data)
232234
require.NoError(t, err)
235+
bufRead := bufio.NewReader(conn)
236+
237+
// Brief pause to reduce the likelihood that we send keystrokes while
238+
// the shell is simultaneously sending a prompt.
239+
time.Sleep(100 * time.Millisecond)
233240

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

241-
findEcho := func() {
248+
expectLine := func(matcher func(string) bool) {
242249
for {
243-
read, err := conn.Read(data)
250+
line, err := bufRead.ReadString('\n')
244251
require.NoError(t, err)
245-
if strings.Contains(string(data[:read]), "test") {
246-
return
252+
if matcher(line) {
253+
break
247254
}
248255
}
249256
}
257+
matchEchoCommand := func(line string) bool {
258+
return strings.Contains(line, "echo test")
259+
}
260+
matchEchoOutput := func(line string) bool {
261+
return strings.Contains(line, "test") && !strings.Contains(line, "echo")
262+
}
250263

251-
findEcho()
252-
findEcho()
264+
expectLine(matchEchoCommand)
265+
expectLine(matchEchoOutput)
253266
}

0 commit comments

Comments
 (0)