retry_test.go 685 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package fx
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestRetry(t *testing.T) {
  8. assert.NotNil(t, DoWithRetry(func() error {
  9. return errors.New("any")
  10. }))
  11. var times int
  12. assert.Nil(t, DoWithRetry(func() error {
  13. times++
  14. if times == defaultRetryTimes {
  15. return nil
  16. }
  17. return errors.New("any")
  18. }))
  19. times = 0
  20. assert.NotNil(t, DoWithRetry(func() error {
  21. times++
  22. if times == defaultRetryTimes+1 {
  23. return nil
  24. }
  25. return errors.New("any")
  26. }))
  27. total := 2 * defaultRetryTimes
  28. times = 0
  29. assert.Nil(t, DoWithRetry(func() error {
  30. times++
  31. if times == total {
  32. return nil
  33. }
  34. return errors.New("any")
  35. }, WithRetry(total)))
  36. }