@@ -11,6 +11,7 @@ import (
11
11
"testing"
12
12
"time"
13
13
14
+ "github.com/coder/coder/v2/testutil"
14
15
"github.com/stretchr/testify/assert"
15
16
16
17
"github.com/coder/coder/v2/cli/cliui"
@@ -27,7 +28,11 @@ func TestProvisionerJob(t *testing.T) {
27
28
t .Parallel ()
28
29
29
30
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 () {
31
36
<- test .Next
32
37
test .JobMutex .Lock ()
33
38
test .Job .Status = codersdk .ProvisionerJobRunning
@@ -41,20 +46,26 @@ func TestProvisionerJob(t *testing.T) {
41
46
test .Job .CompletedAt = & now
42
47
close (test .Logs )
43
48
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 )
51
59
})
52
60
53
61
t .Run ("Stages" , func (t * testing.T ) {
54
62
t .Parallel ()
55
63
56
64
test := newProvisionerJob (t )
57
- go func () {
65
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
66
+ defer cancel ()
67
+
68
+ testutil .Go (t , func () {
58
69
<- test .Next
59
70
test .JobMutex .Lock ()
60
71
test .Job .Status = codersdk .ProvisionerJobRunning
@@ -72,13 +83,16 @@ func TestProvisionerJob(t *testing.T) {
72
83
test .Job .CompletedAt = & now
73
84
close (test .Logs )
74
85
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 )
82
96
})
83
97
84
98
t .Run ("Queue Position" , func (t * testing.T ) {
@@ -120,7 +134,10 @@ func TestProvisionerJob(t *testing.T) {
120
134
test .Job .QueueSize = tc .queuePos
121
135
test .JobMutex .Unlock ()
122
136
123
- go func () {
137
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
138
+ defer cancel ()
139
+
140
+ testutil .Go (t , func () {
124
141
<- test .Next
125
142
test .JobMutex .Lock ()
126
143
test .Job .Status = codersdk .ProvisionerJobRunning
@@ -134,13 +151,16 @@ func TestProvisionerJob(t *testing.T) {
134
151
test .Job .CompletedAt = & now
135
152
close (test .Logs )
136
153
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 )
144
164
})
145
165
}
146
166
})
@@ -156,7 +176,11 @@ func TestProvisionerJob(t *testing.T) {
156
176
}
157
177
158
178
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 () {
160
184
<- test .Next
161
185
currentProcess , err := os .FindProcess (os .Getpid ())
162
186
assert .NoError (t , err )
@@ -169,12 +193,15 @@ func TestProvisionerJob(t *testing.T) {
169
193
test .Job .CompletedAt = & now
170
194
close (test .Logs )
171
195
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 )
178
205
})
179
206
}
180
207
0 commit comments