~surma/goamz/put-headers

« back to all changes in this revision

Viewing changes to aws/attempt_test.go

  • Committer: Gustavo Niemeyer
  • Date: 2013-02-04 21:32:57 UTC
  • mfrom: (27.1.7 introduce-retrying)
  • Revision ID: gustavo@niemeyer.net-20130204213257-ciqizii3i7fqv7e0
s3: introduce retrying on known errors

R=
CC=
https://codereview.appspot.com/7264043

Show diffs side-by-side

added added

removed removed

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