Skip to content

Commit 4367e65

Browse files
committed
Ensuring tests don't run indefinitely
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent b08e633 commit 4367e65

File tree

1 file changed

+58
-31
lines changed

1 file changed

+58
-31
lines changed

cli/cliui/provisionerjob_test.go

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/coder/coder/v2/testutil"
1415
"github.com/stretchr/testify/assert"
1516

1617
"github.com/coder/coder/v2/cli/cliui"
@@ -27,7 +28,11 @@ func TestProvisionerJob(t *testing.T) {
2728
t.Parallel()
2829

2930
test := newProvisionerJob(t)
30-
go func() {
31+
32+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
33+
defer cancel()
34+
35+
testutil.Go(t, func() {
3136
<-test.Next
3237
test.JobMutex.Lock()
3338
test.Job.Status = codersdk.ProvisionerJobRunning
@@ -41,20 +46,26 @@ func TestProvisionerJob(t *testing.T) {
4146
test.Job.CompletedAt = &now
4247
close(test.Logs)
4348
test.JobMutex.Unlock()
44-
}()
45-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
46-
test.Next <- struct{}{}
47-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
48-
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
49-
test.Next <- struct{}{}
50-
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
49+
})
50+
testutil.Eventually(ctx, t, func(ctx context.Context) (done bool) {
51+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
52+
test.Next <- struct{}{}
53+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
54+
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
55+
test.Next <- struct{}{}
56+
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
57+
return true
58+
}, testutil.IntervalFast)
5159
})
5260

5361
t.Run("Stages", func(t *testing.T) {
5462
t.Parallel()
5563

5664
test := newProvisionerJob(t)
57-
go func() {
65+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
66+
defer cancel()
67+
68+
testutil.Go(t, func() {
5869
<-test.Next
5970
test.JobMutex.Lock()
6071
test.Job.Status = codersdk.ProvisionerJobRunning
@@ -72,13 +83,16 @@ func TestProvisionerJob(t *testing.T) {
7283
test.Job.CompletedAt = &now
7384
close(test.Logs)
7485
test.JobMutex.Unlock()
75-
}()
76-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
77-
test.Next <- struct{}{}
78-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
79-
test.PTY.ExpectMatch("Something")
80-
test.Next <- struct{}{}
81-
test.PTY.ExpectMatch("Something")
86+
})
87+
testutil.Eventually(ctx, t, func(ctx context.Context) (done bool) {
88+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
89+
test.Next <- struct{}{}
90+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
91+
test.PTY.ExpectMatch("Something")
92+
test.Next <- struct{}{}
93+
test.PTY.ExpectMatch("Something")
94+
return true
95+
}, testutil.IntervalFast)
8296
})
8397

8498
t.Run("Queue Position", func(t *testing.T) {
@@ -120,7 +134,10 @@ func TestProvisionerJob(t *testing.T) {
120134
test.Job.QueueSize = tc.queuePos
121135
test.JobMutex.Unlock()
122136

123-
go func() {
137+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
138+
defer cancel()
139+
140+
testutil.Go(t, func() {
124141
<-test.Next
125142
test.JobMutex.Lock()
126143
test.Job.Status = codersdk.ProvisionerJobRunning
@@ -134,13 +151,16 @@ func TestProvisionerJob(t *testing.T) {
134151
test.Job.CompletedAt = &now
135152
close(test.Logs)
136153
test.JobMutex.Unlock()
137-
}()
138-
test.PTY.ExpectRegexMatch(tc.expected)
139-
test.Next <- struct{}{}
140-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued) // step completed
141-
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
142-
test.Next <- struct{}{}
143-
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
154+
})
155+
testutil.Eventually(ctx, t, func(ctx context.Context) (done bool) {
156+
test.PTY.ExpectRegexMatch(tc.expected)
157+
test.Next <- struct{}{}
158+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued) // step completed
159+
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
160+
test.Next <- struct{}{}
161+
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
162+
return true
163+
}, testutil.IntervalFast)
144164
})
145165
}
146166
})
@@ -156,7 +176,11 @@ func TestProvisionerJob(t *testing.T) {
156176
}
157177

158178
test := newProvisionerJob(t)
159-
go func() {
179+
180+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
181+
defer cancel()
182+
183+
testutil.Go(t, func() {
160184
<-test.Next
161185
currentProcess, err := os.FindProcess(os.Getpid())
162186
assert.NoError(t, err)
@@ -169,12 +193,15 @@ func TestProvisionerJob(t *testing.T) {
169193
test.Job.CompletedAt = &now
170194
close(test.Logs)
171195
test.JobMutex.Unlock()
172-
}()
173-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
174-
test.Next <- struct{}{}
175-
test.PTY.ExpectMatch("Gracefully canceling")
176-
test.Next <- struct{}{}
177-
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
196+
})
197+
testutil.Eventually(ctx, t, func(ctx context.Context) (done bool) {
198+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
199+
test.Next <- struct{}{}
200+
test.PTY.ExpectMatch("Gracefully canceling")
201+
test.Next <- struct{}{}
202+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued)
203+
return true
204+
}, testutil.IntervalFast)
178205
})
179206
}
180207

0 commit comments

Comments
 (0)