@@ -1198,24 +1198,30 @@ func x11EchoSession(ctx context.Context, t *testing.T, clt *tracessh.Client) x11
1198
1198
os .Remove (tmpFile .Name ())
1199
1199
})
1200
1200
1201
- // type 'printenv DISPLAY > /path/to/tmp/file' into the session (dumping the value of DISPLAY into the temp file)
1202
- _ , err = keyboard .Write ([]byte (fmt .Sprintf ("printenv %v >> %s\n \r " , x11 .DisplayEnv , tmpFile .Name ())))
1203
- require .NoError (t , err )
1204
-
1205
- // wait for the output
1206
- var display string
1207
- require .Eventually (t , func () bool {
1208
- output , err := os .ReadFile (tmpFile .Name ())
1209
- if err == nil && len (output ) != 0 {
1210
- display = strings .TrimSpace (string (output ))
1211
- return true
1212
- }
1213
- return false
1214
- }, 5 * time .Second , 10 * time .Millisecond , "failed to read display" )
1201
+ // Reading the display may fail if the session is not fully initialized
1202
+ // and the write to stdin is swallowed.
1203
+ display := make (chan string , 1 )
1204
+ require .EventuallyWithT (t , func (t * assert.CollectT ) {
1205
+ // enter 'printenv DISPLAY > /path/to/tmp/file' into the session (dumping the value of DISPLAY into the temp file)
1206
+ _ , err = keyboard .Write ([]byte (fmt .Sprintf ("printenv %v > %s\n \r " , x11 .DisplayEnv , tmpFile .Name ())))
1207
+ assert .NoError (t , err )
1208
+
1209
+ assert .Eventually (t , func () bool {
1210
+ output , err := os .ReadFile (tmpFile .Name ())
1211
+ if err == nil && len (output ) != 0 {
1212
+ select {
1213
+ case display <- strings .TrimSpace (string (output )):
1214
+ default :
1215
+ }
1216
+ return true
1217
+ }
1218
+ return false
1219
+ }, time .Second , 100 * time .Millisecond , "failed to read display" )
1220
+ }, 10 * time .Second , 1 * time .Second )
1215
1221
1216
1222
// Make a new connection to the XServer proxy, the client
1217
1223
// XServer should echo back anything written on it.
1218
- serverDisplay , err := x11 .ParseDisplay (display )
1224
+ serverDisplay , err := x11 .ParseDisplay (<- display )
1219
1225
require .NoError (t , err )
1220
1226
1221
1227
return serverDisplay
0 commit comments