~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/attempt_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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