Skip to content

Commit

Permalink
Make breaker tests run faster
Browse files Browse the repository at this point in the history
Timing tests are finicky, but 10ms increments seems to still be plenty
of margin of error in practice, and means these tests don't have to take
14s any sleep() anymore.
  • Loading branch information
eapache committed Jan 13, 2024
1 parent b938ac8 commit c642ff9
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions breaker/breaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ func returnsSuccess() error {
}

func TestBreakerErrorExpiry(t *testing.T) {
breaker := New(2, 1, 1*time.Second)
breaker := New(2, 1, 10*time.Millisecond)

for i := 0; i < 3; i++ {
if err := breaker.Run(returnsError); err != errSomeError {
t.Error(err)
}
time.Sleep(1 * time.Second)
time.Sleep(10 * time.Millisecond)
}

for i := 0; i < 3; i++ {
if err := breaker.Go(returnsError); err != nil {
t.Error(err)
}
time.Sleep(1 * time.Second)
time.Sleep(10 * time.Millisecond)
}
}

Expand Down Expand Up @@ -66,7 +66,7 @@ func TestBreakerPanicsCountAsErrors(t *testing.T) {
}

func TestBreakerStateTransitions(t *testing.T) {
breaker := New(3, 2, 1*time.Second)
breaker := New(3, 2, 10*time.Millisecond)

// three errors opens the breaker
for i := 0; i < 3; i++ {
Expand All @@ -83,7 +83,7 @@ func TestBreakerStateTransitions(t *testing.T) {
}

// wait for it to half-close
time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
// one success works, but is not enough to fully close
if err := breaker.Run(returnsSuccess); err != nil {
t.Error(err)
Expand All @@ -98,7 +98,7 @@ func TestBreakerStateTransitions(t *testing.T) {
}

// wait for it to half-close
time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
// two successes is enough to close it for good
for i := 0; i < 2; i++ {
if err := breaker.Run(returnsSuccess); err != nil {
Expand All @@ -116,7 +116,7 @@ func TestBreakerStateTransitions(t *testing.T) {
}

func TestBreakerAsyncStateTransitions(t *testing.T) {
breaker := New(3, 2, 1*time.Second)
breaker := New(3, 2, 10*time.Millisecond)

// three errors opens the breaker
for i := 0; i < 3; i++ {
Expand All @@ -136,7 +136,7 @@ func TestBreakerAsyncStateTransitions(t *testing.T) {
}

// wait for it to half-close
time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
// one success works, but is not enough to fully close
if err := breaker.Go(returnsSuccess); err != nil {
t.Error(err)
Expand All @@ -153,7 +153,7 @@ func TestBreakerAsyncStateTransitions(t *testing.T) {
}

// wait for it to half-close
time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
// two successes is enough to close it for good
for i := 0; i < 2; i++ {
if err := breaker.Go(returnsSuccess); err != nil {
Expand Down

0 comments on commit c642ff9

Please sign in to comment.