Skip to content

Commit

Permalink
Fix flaky unit tests
Browse files Browse the repository at this point in the history
Addresses issues causing failures in TestCache_Backoff, TestTeleportProcess_reconnectToAuth
and TestResourceWatcher_Backoff. By utilizing FakeClock.BlockUntil tests ensure that the clock
will not be advanced until retry.After has been called.
  • Loading branch information
rosstimothy committed Jan 4, 2022
1 parent 02ee527 commit a2c3efe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2193,8 +2193,11 @@ func TestCache_Backoff(t *testing.T) {
require.GreaterOrEqual(t, duration, stepMin)
require.LessOrEqual(t, duration, stepMax)

// wait for cache to get to retry.After
clock.BlockUntil(1)

// add some extra to the duration to ensure the retry occurs
clock.Advance(duration * 3)
clock.Advance(p.cache.MaxRetryPeriod)
case <-time.After(time.Minute):
t.Fatalf("timeout waiting for event")
}
Expand Down
7 changes: 6 additions & 1 deletion lib/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ func TestTeleportProcess_reconnectToAuth(t *testing.T) {
cfg.Auth.Enabled = false
cfg.Proxy.Enabled = false
cfg.SSH.Enabled = true
cfg.MaxRetryPeriod = defaults.MaxWatcherBackoff
process, err := NewTeleport(cfg)
require.NoError(t, err)

Expand All @@ -567,8 +568,12 @@ func TestTeleportProcess_reconnectToAuth(t *testing.T) {

require.GreaterOrEqual(t, duration, stepMin)
require.LessOrEqual(t, duration, stepMax)

// wait for connection to get to retry.After
clock.BlockUntil(1)

// add some extra to the duration to ensure the retry occurs
clock.Advance(duration * 3)
clock.Advance(cfg.MaxRetryPeriod)
case <-time.After(time.Minute):
t.Fatalf("timeout waiting for failure")
}
Expand Down
6 changes: 5 additions & 1 deletion lib/services/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ func TestResourceWatcher_Backoff(t *testing.T) {

require.GreaterOrEqual(t, duration, stepMin)
require.LessOrEqual(t, duration, stepMax)

// wait for watcher to get to retry.After
clock.BlockUntil(1)

// add some extra to the duration to ensure the retry occurs
clock.Advance(duration * 3)
clock.Advance(w.MaxRetryPeriod)
case <-time.After(time.Minute):
t.Fatalf("timeout waiting for reset")
}
Expand Down

0 comments on commit a2c3efe

Please sign in to comment.