@@ -352,38 +352,44 @@ func TestWatchBackend(t *testing.T) {
352352 return
353353 }
354354
355- currentEnv := os .Getenv ("MONGO_SERVER_URL" )
356- if currentEnv != testEnv {
357- t .Errorf ("expected MONGO_SERVER_URL: %s, but got %s" , testEnv , currentEnv )
358- }
355+ waitForMongoServerURLEnv (t , testEnv )
359356
360357 // Updating file now
361358 if err := os .WriteFile (filepath .Join (tt .cfg .Storage .DocDB .MongoServerURLDir , "MONGO_SERVER_URL" ), []byte (tt .expectedMongoEnv ), 0644 ); err != nil {
362359 t .Error (err )
363360 }
364361
365362 // Let's wait for the event to be read by fsnotify
366- time . Sleep ( 500 * time . Millisecond )
363+ waitForMongoServerURLEnv ( t , tt . expectedMongoEnv )
367364
368365 // Empty the channel now
369366 <- backendChan
370- currentEnv = os .Getenv ("MONGO_SERVER_URL" )
371- if currentEnv != tt .expectedMongoEnv {
372- t .Errorf ("expected MONGO_SERVER_URL: %s, but got %s" , tt .expectedMongoEnv , currentEnv )
373- }
374367
375368 // Let's go back to older env (env rotation) and test again
376369 if err := os .WriteFile (filepath .Join (tt .cfg .Storage .DocDB .MongoServerURLDir , "MONGO_SERVER_URL" ), []byte (testEnv ), 0644 ); err != nil {
377370 t .Error (err )
378371 }
379372
380373 // Let's wait for the event to be read by fsnotify
381- time .Sleep (500 * time .Millisecond )
382-
383- currentEnv = os .Getenv ("MONGO_SERVER_URL" )
384- if currentEnv != testEnv {
385- t .Errorf ("expected MONGO_SERVER_URL: %s, but got %s" , testEnv , currentEnv )
386- }
374+ waitForMongoServerURLEnv (t , testEnv )
387375 })
388376 }
389377}
378+
379+ func waitForMongoServerURLEnv (t * testing.T , expectedEnv string ) {
380+ t .Helper ()
381+ attempts := 10
382+ for i := 1 ; i <= attempts ; i ++ {
383+ currentEnv := os .Getenv ("MONGO_SERVER_URL" )
384+ if currentEnv == expectedEnv {
385+ break
386+ }
387+
388+ if i == attempts {
389+ t .Errorf ("MONGO_SERVER_URL: want %s, got %s" , expectedEnv , currentEnv )
390+ }
391+
392+ t .Logf ("MONGO_SERVER_URL: want %s, got %s, attempt: %d" , expectedEnv , currentEnv , i )
393+ time .Sleep (500 * time .Millisecond )
394+ }
395+ }
0 commit comments