You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
1.5 KiB

  1. package aws_test
  2. import (
  3. "time"
  4. "github.com/goamz/goamz/aws"
  5. . "gopkg.in/check.v1"
  6. )
  7. func (S) TestAttemptTiming(c *C) {
  8. testAttempt := aws.AttemptStrategy{
  9. Total: 0.25e9,
  10. Delay: 0.1e9,
  11. }
  12. want := []time.Duration{0, 0.1e9, 0.2e9, 0.2e9}
  13. got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing
  14. t0 := time.Now()
  15. for a := testAttempt.Start(); a.Next(); {
  16. got = append(got, time.Now().Sub(t0))
  17. }
  18. got = append(got, time.Now().Sub(t0))
  19. c.Assert(got, HasLen, len(want))
  20. const margin = 0.01e9
  21. for i, got := range want {
  22. lo := want[i] - margin
  23. hi := want[i] + margin
  24. if got < lo || got > hi {
  25. c.Errorf("attempt %d want %g got %g", i, want[i].Seconds(), got.Seconds())
  26. }
  27. }
  28. }
  29. func (S) TestAttemptNextHasNext(c *C) {
  30. a := aws.AttemptStrategy{}.Start()
  31. c.Assert(a.Next(), Equals, true)
  32. c.Assert(a.Next(), Equals, false)
  33. a = aws.AttemptStrategy{}.Start()
  34. c.Assert(a.Next(), Equals, true)
  35. c.Assert(a.HasNext(), Equals, false)
  36. c.Assert(a.Next(), Equals, false)
  37. a = aws.AttemptStrategy{Total: 2e8}.Start()
  38. c.Assert(a.Next(), Equals, true)
  39. c.Assert(a.HasNext(), Equals, true)
  40. time.Sleep(2e8)
  41. c.Assert(a.HasNext(), Equals, true)
  42. c.Assert(a.Next(), Equals, true)
  43. c.Assert(a.Next(), Equals, false)
  44. a = aws.AttemptStrategy{Total: 1e8, Min: 2}.Start()
  45. time.Sleep(1e8)
  46. c.Assert(a.Next(), Equals, true)
  47. c.Assert(a.HasNext(), Equals, true)
  48. c.Assert(a.Next(), Equals, true)
  49. c.Assert(a.HasNext(), Equals, false)
  50. c.Assert(a.Next(), Equals, false)
  51. }