~ubuntu-branches/ubuntu/saucy/juju-core/saucy

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/utils/attempt_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 16:02:16 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20130820160216-5yu1llasa2e2youn
Tags: 1.13.1-0ubuntu1
* New upstream release.
  - Build and install juju metadata plugin.
  - d/NEWS: Add some guidance on upgrading environments from 1.11.x
    to 1.13.x.
* d/NEWS: Add details about lack of upgrade path from juju < 1.11
  and how to interact with older juju environments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package utils_test
 
2
 
 
3
import (
 
4
        . "launchpad.net/gocheck"
 
5
        "launchpad.net/juju-core/utils"
 
6
        "time"
 
7
)
 
8
 
 
9
func doSomething() (int, error) { return 0, nil }
 
10
 
 
11
func shouldRetry(error) bool { return false }
 
12
 
 
13
func doSomethingWith(int) {}
 
14
 
 
15
func ExampleAttempt_HasNext() {
 
16
        // This example shows how Attempt.HasNext can be used to help
 
17
        // structure an attempt loop. If the godoc example code allowed
 
18
        // us to make the example return an error, we would uncomment
 
19
        // the commented return statements.
 
20
        attempts := utils.AttemptStrategy{
 
21
                Total: 1 * time.Second,
 
22
                Delay: 250 * time.Millisecond,
 
23
        }
 
24
        for attempt := attempts.Start(); attempt.Next(); {
 
25
                x, err := doSomething()
 
26
                if shouldRetry(err) && attempt.HasNext() {
 
27
                        continue
 
28
                }
 
29
                if err != nil {
 
30
                        // return err
 
31
                        return
 
32
                }
 
33
                doSomethingWith(x)
 
34
        }
 
35
        // return ErrTimedOut
 
36
        return
 
37
}
 
38
 
 
39
func (utilsSuite) TestAttemptTiming(c *C) {
 
40
        testAttempt := utils.AttemptStrategy{
 
41
                Total: 0.25e9,
 
42
                Delay: 0.1e9,
 
43
        }
 
44
        want := []time.Duration{0, 0.1e9, 0.2e9, 0.2e9}
 
45
        got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing
 
46
        t0 := time.Now()
 
47
        for a := testAttempt.Start(); a.Next(); {
 
48
                got = append(got, time.Now().Sub(t0))
 
49
        }
 
50
        got = append(got, time.Now().Sub(t0))
 
51
        c.Assert(got, HasLen, len(want))
 
52
        const margin = 0.01e9
 
53
        for i, got := range want {
 
54
                lo := want[i] - margin
 
55
                hi := want[i] + margin
 
56
                if got < lo || got > hi {
 
57
                        c.Errorf("attempt %d want %g got %g", i, want[i].Seconds(), got.Seconds())
 
58
                }
 
59
        }
 
60
}
 
61
 
 
62
func (utilsSuite) TestAttemptNextHasNext(c *C) {
 
63
        a := utils.AttemptStrategy{}.Start()
 
64
        c.Assert(a.Next(), Equals, true)
 
65
        c.Assert(a.Next(), Equals, false)
 
66
 
 
67
        a = utils.AttemptStrategy{}.Start()
 
68
        c.Assert(a.Next(), Equals, true)
 
69
        c.Assert(a.HasNext(), Equals, false)
 
70
        c.Assert(a.Next(), Equals, false)
 
71
 
 
72
        a = utils.AttemptStrategy{Total: 2e8}.Start()
 
73
        c.Assert(a.Next(), Equals, true)
 
74
        c.Assert(a.HasNext(), Equals, true)
 
75
        time.Sleep(2e8)
 
76
        c.Assert(a.HasNext(), Equals, true)
 
77
        c.Assert(a.Next(), Equals, true)
 
78
        c.Assert(a.Next(), Equals, false)
 
79
 
 
80
        a = utils.AttemptStrategy{Total: 1e8, Min: 2}.Start()
 
81
        time.Sleep(1e8)
 
82
        c.Assert(a.Next(), Equals, true)
 
83
        c.Assert(a.HasNext(), Equals, true)
 
84
        c.Assert(a.Next(), Equals, true)
 
85
        c.Assert(a.HasNext(), Equals, false)
 
86
        c.Assert(a.Next(), Equals, false)
 
87
}