/
network_test.go
102 lines (87 loc) · 2.29 KB
/
network_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"time"
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/testing"
)
type networkSuite struct {
testing.BaseSuite
}
var _ = gc.Suite(&networkSuite{})
func (s *networkSuite) TestOpSuccess(c *gc.C) {
isCalled := false
f := func() error {
isCalled = true
return nil
}
err := networkOperationWitDefaultRetries(f, "do it")()
c.Assert(err, jc.ErrorIsNil)
c.Assert(isCalled, jc.IsTrue)
}
func (s *networkSuite) TestOpFailureNoRetry(c *gc.C) {
s.PatchValue(&defaultNetworkOperationRetryDelay, 1*time.Millisecond)
netErr := &netError{false}
callCount := 0
f := func() error {
callCount++
return netErr
}
err := networkOperationWitDefaultRetries(f, "do it")()
c.Assert(errors.Cause(err), gc.Equals, netErr)
c.Assert(callCount, gc.Equals, 1)
}
func (s *networkSuite) TestOpFailureRetries(c *gc.C) {
s.PatchValue(&defaultNetworkOperationRetryDelay, 1*time.Millisecond)
netErr := &netError{true}
callCount := 0
f := func() error {
callCount++
return netErr
}
err := networkOperationWitDefaultRetries(f, "do it")()
c.Assert(errors.Cause(err), gc.Equals, netErr)
c.Assert(callCount, gc.Equals, 10)
}
func (s *networkSuite) TestOpNestedFailureRetries(c *gc.C) {
s.PatchValue(&defaultNetworkOperationRetryDelay, 1*time.Millisecond)
netErr := &netError{true}
callCount := 0
f := func() error {
callCount++
return errors.Annotate(errors.Trace(netErr), "create a wrapped error")
}
err := networkOperationWitDefaultRetries(f, "do it")()
c.Assert(errors.Cause(err), gc.Equals, netErr)
c.Assert(callCount, gc.Equals, 10)
}
func (s *networkSuite) TestOpSucceedsAfterRetries(c *gc.C) {
s.PatchValue(&defaultNetworkOperationRetryDelay, 1*time.Millisecond)
netErr := &netError{true}
callCount := 0
f := func() error {
callCount++
if callCount == 5 {
return nil
}
return netErr
}
err := networkOperationWitDefaultRetries(f, "do it")()
c.Assert(err, jc.ErrorIsNil)
c.Assert(callCount, gc.Equals, 5)
}
type netError struct {
temporary bool
}
func (e *netError) Error() string {
return "network error"
}
func (e *netError) Temporary() bool {
return e.temporary
}
func (e *netError) Timeout() bool {
return false
}