@@ -127,8 +127,8 @@ func extractTar(t *testing.T, data []byte, directory string) {
127
127
}
128
128
}
129
129
130
- // Start runs the command in a goroutine and cleans it up when
131
- // the test completed.
130
+ // Start runs the command in a goroutine and cleans it up when the test
131
+ // completed.
132
132
func Start (t * testing.T , inv * clibase.Invocation ) {
133
133
t .Helper ()
134
134
@@ -170,7 +170,7 @@ func (w *ErrorWaiter) Wait() error {
170
170
var ok bool
171
171
w .cachedError , ok = <- w .c
172
172
if ! ok {
173
- panic ("unexpoected channel close" )
173
+ panic ("unexpected channel close" )
174
174
}
175
175
})
176
176
return w .cachedError
@@ -196,18 +196,18 @@ func (w *ErrorWaiter) RequireAs(want interface{}) {
196
196
require .ErrorAs (w .t , w .Wait (), want )
197
197
}
198
198
199
- // StartWithWaiter runs the command in a goroutine but returns the error
200
- // instead of asserting it. This is useful for testing error cases.
199
+ // StartWithWaiter runs the command in a goroutine but returns the error instead
200
+ // of asserting it. This is useful for testing error cases.
201
201
func StartWithWaiter (t * testing.T , inv * clibase.Invocation ) * ErrorWaiter {
202
202
t .Helper ()
203
203
204
- errCh := make (chan error , 1 )
205
-
206
- var cleaningUp atomic.Bool
207
-
208
204
var (
209
205
ctx = inv .Context ()
210
206
cancel func ()
207
+
208
+ cleaningUp atomic.Bool
209
+ errCh = make (chan error , 1 )
210
+ doneCh = make (chan struct {})
211
211
)
212
212
if _ , ok := ctx .Deadline (); ! ok {
213
213
ctx , cancel = context .WithDeadline (ctx , time .Now ().Add (testutil .WaitMedium ))
@@ -218,12 +218,13 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
218
218
inv = inv .WithContext (ctx )
219
219
220
220
go func () {
221
+ defer close (doneCh )
221
222
defer close (errCh )
222
223
err := inv .Run ()
223
224
if cleaningUp .Load () && errors .Is (err , context .DeadlineExceeded ) {
224
- // If we're cleaning up, this error is likely related to the
225
- // CLI teardown process. E.g., the server could be slow to shut
226
- // down Postgres.
225
+ // If we're cleaning up, this error is likely related to the CLI
226
+ // teardown process. E.g., the server could be slow to shut down
227
+ // Postgres.
227
228
t .Logf ("command %q timed out during test cleanup" , inv .Command .FullName ())
228
229
}
229
230
// Whether or not this fails the test is left to the caller.
@@ -235,7 +236,7 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
235
236
t .Cleanup (func () {
236
237
cancel ()
237
238
cleaningUp .Store (true )
238
- <- errCh
239
+ <- doneCh
239
240
})
240
241
return & ErrorWaiter {c : errCh , t : t }
241
242
}
0 commit comments