@@ -352,38 +352,44 @@ func TestWatchBackend(t *testing.T) {
352
352
return
353
353
}
354
354
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 )
359
356
360
357
// Updating file now
361
358
if err := os .WriteFile (filepath .Join (tt .cfg .Storage .DocDB .MongoServerURLDir , "MONGO_SERVER_URL" ), []byte (tt .expectedMongoEnv ), 0644 ); err != nil {
362
359
t .Error (err )
363
360
}
364
361
365
362
// Let's wait for the event to be read by fsnotify
366
- time . Sleep ( 500 * time . Millisecond )
363
+ waitForMongoServerURLEnv ( t , tt . expectedMongoEnv )
367
364
368
365
// Empty the channel now
369
366
<- 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
- }
374
367
375
368
// Let's go back to older env (env rotation) and test again
376
369
if err := os .WriteFile (filepath .Join (tt .cfg .Storage .DocDB .MongoServerURLDir , "MONGO_SERVER_URL" ), []byte (testEnv ), 0644 ); err != nil {
377
370
t .Error (err )
378
371
}
379
372
380
373
// 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 )
387
375
})
388
376
}
389
377
}
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