retry_test.go 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package retry
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. "github.com/v2ray/v2ray-core/testing/unit"
  7. )
  8. var (
  9. TestError = errors.New("This is a fake error.")
  10. )
  11. func TestNoRetry(t *testing.T) {
  12. assert := unit.Assert(t)
  13. startTime := time.Now().Unix()
  14. Timed(10, 100000).On(func() error {
  15. return nil
  16. })
  17. endTime := time.Now().Unix()
  18. assert.Int64(endTime - startTime).AtLeast(0)
  19. }
  20. func TestRetryOnce(t *testing.T) {
  21. assert := unit.Assert(t)
  22. startTime := time.Now()
  23. called := 0
  24. Timed(10, 1000).On(func() error {
  25. if called == 0 {
  26. called++
  27. return TestError
  28. }
  29. return nil
  30. })
  31. duration := time.Since(startTime)
  32. assert.Int64(int64(duration / time.Millisecond)).AtLeast(900)
  33. }
  34. func TestRetryMultiple(t *testing.T) {
  35. assert := unit.Assert(t)
  36. startTime := time.Now()
  37. called := 0
  38. Timed(10, 1000).On(func() error {
  39. if called < 5 {
  40. called++
  41. return TestError
  42. }
  43. return nil
  44. })
  45. duration := time.Since(startTime)
  46. assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900)
  47. }