Skip to content

Commit

Permalink
Fix flaky watch backend test
Browse files Browse the repository at this point in the history
Fixes tektoncd#1178

Signed-off-by: Luiz Carvalho <[email protected]>
  • Loading branch information
lcarva committed Aug 19, 2024
1 parent 6e288c1 commit 99dd499
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions pkg/chains/storage/docdb/docdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,38 +352,44 @@ func TestWatchBackend(t *testing.T) {
return
}

currentEnv := os.Getenv("MONGO_SERVER_URL")
if currentEnv != testEnv {
t.Errorf("expected MONGO_SERVER_URL: %s, but got %s", testEnv, currentEnv)
}
waitForMongoServerURLEnv(t, testEnv)

// Updating file now
if err := os.WriteFile(filepath.Join(tt.cfg.Storage.DocDB.MongoServerURLDir, "MONGO_SERVER_URL"), []byte(tt.expectedMongoEnv), 0644); err != nil {
t.Error(err)
}

// Let's wait for the event to be read by fsnotify
time.Sleep(500 * time.Millisecond)
waitForMongoServerURLEnv(t, tt.expectedMongoEnv)

// Empty the channel now
<-backendChan
currentEnv = os.Getenv("MONGO_SERVER_URL")
if currentEnv != tt.expectedMongoEnv {
t.Errorf("expected MONGO_SERVER_URL: %s, but got %s", tt.expectedMongoEnv, currentEnv)
}

// Let's go back to older env (env rotation) and test again
if err := os.WriteFile(filepath.Join(tt.cfg.Storage.DocDB.MongoServerURLDir, "MONGO_SERVER_URL"), []byte(testEnv), 0644); err != nil {
t.Error(err)
}

// Let's wait for the event to be read by fsnotify
time.Sleep(500 * time.Millisecond)

currentEnv = os.Getenv("MONGO_SERVER_URL")
if currentEnv != testEnv {
t.Errorf("expected MONGO_SERVER_URL: %s, but got %s", testEnv, currentEnv)
}
waitForMongoServerURLEnv(t, testEnv)
})
}
}

func waitForMongoServerURLEnv(t *testing.T, expectedEnv string) {
t.Helper()
attempts := 10
for i := 1; i <= attempts; i++ {
currentEnv := os.Getenv("MONGO_SERVER_URL")
if currentEnv == expectedEnv {
break
}

if i == attempts {
t.Errorf("MONGO_SERVER_URL: want %s, got %s", expectedEnv, currentEnv)
}

t.Logf("MONGO_SERVER_URL: want %s, got %s, attempt: %d", expectedEnv, currentEnv, i)
time.Sleep(500 * time.Millisecond)
}
}

0 comments on commit 99dd499

Please sign in to comment.