backoff_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package frankenphp
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. "time"
  6. )
  7. func TestExponentialBackoff_Reset(t *testing.T) {
  8. e := &exponentialBackoff{
  9. maxBackoff: 5 * time.Second,
  10. minBackoff: 500 * time.Millisecond,
  11. maxConsecutiveFailures: 3,
  12. }
  13. assert.False(t, e.recordFailure())
  14. assert.False(t, e.recordFailure())
  15. e.recordSuccess()
  16. e.mu.RLock()
  17. defer e.mu.RUnlock()
  18. assert.Equal(t, 0, e.failureCount, "expected failureCount to be reset to 0")
  19. assert.Equal(t, e.backoff, e.minBackoff, "expected backoff to be reset to minBackoff")
  20. }
  21. func TestExponentialBackoff_Trigger(t *testing.T) {
  22. e := &exponentialBackoff{
  23. maxBackoff: 500 * 3 * time.Millisecond,
  24. minBackoff: 500 * time.Millisecond,
  25. maxConsecutiveFailures: 3,
  26. }
  27. assert.False(t, e.recordFailure())
  28. assert.False(t, e.recordFailure())
  29. assert.True(t, e.recordFailure())
  30. e.mu.RLock()
  31. defer e.mu.RUnlock()
  32. assert.Equal(t, e.failureCount, e.maxConsecutiveFailures, "expected failureCount to be maxConsecutiveFailures")
  33. assert.Equal(t, e.backoff, e.maxBackoff, "expected backoff to be maxBackoff")
  34. }