Skip to content

Commit

Permalink
Make test less reliant on timing
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Sep 7, 2023
1 parent 4afae51 commit 66475d4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions internal/pkg/agent/install/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package install

import (
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -44,18 +45,18 @@ func TestProgress(t *testing.T) {
t.Run("single_step_delayed_failure", func(t *testing.T) {
w := newTestWriter()
pt := NewProgressTracker(w)
pt.SetTickInterval(40 * time.Millisecond) // to speed up testing
pt.SetTickInterval(10 * time.Millisecond) // to speed up testing
pt.DisableRandomizedTickIntervals()

pt.Start()

pt.StepStart("step 1 starting")
time.Sleep(90 * time.Millisecond) // to simulate work being done
time.Sleep(15 * time.Millisecond) // to simulate work being done
pt.StepFailed()

pt.Stop()

require.Equal(t, "step 1 starting..... FAILED\n", string(w.buf))
require.Regexp(t, regexp.MustCompile(`step 1 starting\.{3}\.+ FAILED\n`), string(w.buf))
})

t.Run("multi_step_immediate_success", func(t *testing.T) {
Expand All @@ -78,34 +79,34 @@ func TestProgress(t *testing.T) {
t.Run("multi_step_delayed_success", func(t *testing.T) {
w := newTestWriter()
pt := NewProgressTracker(w)
pt.SetTickInterval(40 * time.Millisecond) // to speed up testing
pt.SetTickInterval(10 * time.Millisecond) // to speed up testing
pt.DisableRandomizedTickIntervals()

pt.Start()

pt.StepStart("step 1 starting")
time.Sleep(210 * time.Millisecond) // to simulate work being done
time.Sleep(55 * time.Millisecond) // to simulate work being done
pt.StepSucceeded()
pt.StepStart("step 2 starting")
time.Sleep(90 * time.Millisecond) // to simulate work being done
time.Sleep(25 * time.Millisecond) // to simulate work being done
pt.StepSucceeded()

pt.Stop()

require.Equal(t, "step 1 starting........ DONE\nstep 2 starting..... DONE\n", string(w.buf))
require.Regexp(t, regexp.MustCompile(`step 1 starting\.{3}\.+ DONE\nstep 2 starting\.{3}\.+ DONE`), string(w.buf))
})

t.Run("single_step_delay_after_success", func(t *testing.T) {
t.Run("single_step_delay_after_failed", func(t *testing.T) {
w := newTestWriter()
pt := NewProgressTracker(w)
pt.SetTickInterval(40 * time.Millisecond) // to speed up testing
pt.SetTickInterval(10 * time.Millisecond) // to speed up testing
pt.DisableRandomizedTickIntervals()

pt.Start()

pt.StepStart("step 1 starting")
pt.StepFailed()
time.Sleep(90 * time.Millisecond) // to simulate work being done
time.Sleep(15 * time.Millisecond)

pt.Stop()

Expand Down

0 comments on commit 66475d4

Please sign in to comment.