@@ -303,18 +303,14 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
303
303
require .NoError (t ,err )
304
304
305
305
command := "sh"
306
- echoEnv := func (t * testing.T ,w io.Writer ,r io. Reader , env string )string {
306
+ echoEnv := func (t * testing.T ,w io.Writer ,env string ) {
307
307
if runtime .GOOS == "windows" {
308
308
_ ,err := fmt .Fprintf (w ,"echo %%%s%%\r \n " ,env )
309
309
require .NoError (t ,err )
310
310
}else {
311
311
_ ,err := fmt .Fprintf (w ,"echo $%s\n " ,env )
312
312
require .NoError (t ,err )
313
313
}
314
- scanner := bufio .NewScanner (r )
315
- require .True (t ,scanner .Scan ())
316
- t .Logf ("%s=%s" ,env ,scanner .Text ())
317
- return scanner .Text ()
318
314
}
319
315
if runtime .GOOS == "windows" {
320
316
command = "cmd.exe"
@@ -328,6 +324,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
328
324
err = session .Start (command )
329
325
require .NoError (t ,err )
330
326
327
+ ctx := testutil .Context (t ,testutil .WaitLong )
328
+
329
+ s := bufio .NewScanner (stdout )
330
+ out := make (chan string )
331
+ testutil .Go (t ,func () {
332
+ for s .Scan () {
333
+ select {
334
+ case out <- s .Text ():
335
+ case <- ctx .Done ():
336
+ return
337
+ }
338
+ }
339
+ })
340
+
341
+ // Until we have gotten the first result, the shell may spit out some data.
342
+ first := true
331
343
//nolint:paralleltest // These tests need to run sequentially.
332
344
for k ,partialV := range map [string ]string {
333
345
"CODER" :"true" ,// From the agent.
@@ -337,8 +349,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
337
349
"MY_SESSION" :"true" ,// From the session.
338
350
} {
339
351
t .Run (k ,func (t * testing.T ) {
340
- out := echoEnv (t ,stdin ,stdout ,k )
341
- require .Contains (t ,strings .TrimSpace (out ),partialV )
352
+ echoEnv (t ,stdin ,k )
353
+ if first {
354
+ for {
355
+ s := testutil .RequireRecvCtx (ctx ,t ,out )
356
+ t .Logf ("%s=%s" ,k ,s )
357
+ s = strings .TrimSpace (s )
358
+ if strings .Contains (s ,partialV ) {
359
+ first = false
360
+ return
361
+ }
362
+ }
363
+ }
364
+ s := testutil .RequireRecvCtx (ctx ,t ,out )
365
+ t .Logf ("%s=%s" ,k ,s )
366
+ s = strings .TrimSpace (s )
367
+ require .Contains (t ,s ,partialV )
342
368
})
343
369
}
344
370
}