Skip to content

Commit 259dee2

Browse files
authored
fix: move contexts to appropriate locations (#21121)
Closes coder/internal#1173, coder/internal#1174 Currently these two tests are flaky because the contexts were created before a potentially long-running process. By the time the context was actually used, it may have timed out - leading to confusion. Additionally, the `ExpectMatch` calls were not using the test context - but rather a background context. I've marked that func as deprecated because we should always tie these to the test context. Special thanks to @mafredri for the brain probe 🧠 --------- Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 8e0516a commit 259dee2

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

cli/exp_rpty_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ func TestExpRpty(t *testing.T) {
9090
wantLabel := "coder.devcontainers.TestExpRpty.Container"
9191

9292
client, workspace, agentToken := setupWorkspaceForAgent(t)
93-
ctx := testutil.Context(t, testutil.WaitLong)
9493
pool, err := dockertest.NewPool("")
9594
require.NoError(t, err, "Could not connect to docker")
9695
ct, err := pool.RunWithOptions(&dockertest.RunOptions{
@@ -128,14 +127,15 @@ func TestExpRpty(t *testing.T) {
128127
clitest.SetupConfig(t, client, root)
129128
pty := ptytest.New(t).Attach(inv)
130129

130+
ctx := testutil.Context(t, testutil.WaitLong)
131131
cmdDone := tGo(t, func() {
132132
err := inv.WithContext(ctx).Run()
133133
assert.NoError(t, err)
134134
})
135135

136-
pty.ExpectMatch(" #")
136+
pty.ExpectMatchContext(ctx, " #")
137137
pty.WriteLine("hostname")
138-
pty.ExpectMatch(ct.Container.Config.Hostname)
138+
pty.ExpectMatchContext(ctx, ct.Container.Config.Hostname)
139139
pty.WriteLine("exit")
140140
<-cmdDone
141141
})

cli/ssh_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,6 @@ func TestSSH_Container(t *testing.T) {
20522052
t.Parallel()
20532053

20542054
client, workspace, agentToken := setupWorkspaceForAgent(t)
2055-
ctx := testutil.Context(t, testutil.WaitLong)
20562055
pool, err := dockertest.NewPool("")
20572056
require.NoError(t, err, "Could not connect to docker")
20582057
ct, err := pool.RunWithOptions(&dockertest.RunOptions{
@@ -2087,14 +2086,15 @@ func TestSSH_Container(t *testing.T) {
20872086
clitest.SetupConfig(t, client, root)
20882087
ptty := ptytest.New(t).Attach(inv)
20892088

2089+
ctx := testutil.Context(t, testutil.WaitLong)
20902090
cmdDone := tGo(t, func() {
20912091
err := inv.WithContext(ctx).Run()
20922092
assert.NoError(t, err)
20932093
})
20942094

2095-
ptty.ExpectMatch(" #")
2095+
ptty.ExpectMatchContext(ctx, " #")
20962096
ptty.WriteLine("hostname")
2097-
ptty.ExpectMatch(ct.Container.Config.Hostname)
2097+
ptty.ExpectMatchContext(ctx, ct.Container.Config.Hostname)
20982098
ptty.WriteLine("exit")
20992099
<-cmdDone
21002100
})

pty/ptytest/ptytest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ type outExpecter struct {
145145
runeReader *bufio.Reader
146146
}
147147

148+
// Deprecated: use ExpectMatchContext instead.
149+
// This uses a background context, so will not respect the test's context.
148150
func (e *outExpecter) ExpectMatch(str string) string {
149151
return e.expectMatchContextFunc(str, e.ExpectMatchContext)
150152
}

0 commit comments

Comments
 (0)