Debian Codegraph @

util/redialer_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package util
18
19 import (
20 . "gopkg.in/check.v1"
21 "gitlab.com/ubports/development/core/lomiri-push-service/bus"
22 testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing"
23 "gitlab.com/ubports/development/core/lomiri-push-service/testing/condition"
24 "testing"
25 "time"
26 )
27
28 // hook up gocheck
29 func TestRedialer(t *testing.T) { TestingT(t) }
30
31 type RedialerSuite struct {
32 timeouts []time.Duration
33 }
34
35 var _ = Suite(&RedialerSuite{})
36
37 func (s *RedialerSuite) SetUpSuite(c *C) {
38 s.timeouts = SwapTimeouts([]time.Duration{0, 0})
39 }
40
41 func (s *RedialerSuite) TearDownSuite(c *C) {
42 SwapTimeouts(s.timeouts)
43 s.timeouts = nil
44 }
45
46 // Redial() tests
47
48 func (s *RedialerSuite) TestWorks(c *C) {
49 endp := testibus.NewTestingEndpoint(condition.Fail2Work(3), nil)
50 ar := NewAutoRedialer(endp)
51 // c.Check(ar.(*autoRedialer).stop, NotNil)
52 c.Check(ar.Redial(), Equals, uint32(4))
53 // and on success, the stopper goes away
54 // c.Check(ar.(*autoRedialer).stop, IsNil)
55 }
56
57 func (s *RedialerSuite) TestRetryNil(c *C) {
58 var ar *autoRedialer
59 c.Check(ar.Redial, Not(PanicMatches), ".* nil pointer dereference")
60 }
61
62 func (s *RedialerSuite) TestRetryTwice(c *C) {
63 endp := testibus.NewTestingEndpoint(condition.Work(true), nil)
64 ar := NewAutoRedialer(endp)
65 c.Check(ar.Redial(), Equals, uint32(1))
66 c.Check(ar.Redial(), Equals, uint32(0))
67 }
68
69 type JitteringEndpoint struct {
70 bus.Endpoint
71 jittered int
72 }
73
74 func (j *JitteringEndpoint) Jitter(time.Duration) time.Duration {
75 j.jittered++
76 return 0
77 }
78
79 func (s *RedialerSuite) TestJitterWorks(c *C) {
80 endp := &JitteringEndpoint{
81 testibus.NewTestingEndpoint(condition.Fail2Work(3), nil),
82 0,
83 }
84 ar := NewAutoRedialer(endp)
85 c.Check(ar.Redial(), Equals, uint32(4))
86 c.Check(endp.jittered, Equals, 3)
87 }
88
89 // Stop() tests
90
91 func (s *RedialerSuite) TestStopWorksOnNil(c *C) {
92 // as a convenience, Stop() should succeed on nil
93 // (a nil retrier certainly isn't retrying!)
94 var ar *autoRedialer
95 c.Check(ar, IsNil)
96 ar.Stop() // nothing happens
97 }
98
99 func (s *RedialerSuite) TestStopStops(c *C) {
100 endp := testibus.NewTestingEndpoint(condition.Work(false), nil)
101 countCh := make(chan uint32)
102 ar := NewAutoRedialer(endp)
103 go func() { countCh <- ar.Redial() }()
104 ar.Stop()
105 select {
106 case <-countCh:
107 // pass
108 case <-time.After(20 * time.Millisecond):
109 c.Fatal("timed out waiting for redial")
110 }
111 // on Stop(), the redialer is Stopped
112 c.Check(ar.(*autoRedialer).state(), Equals, Stopped)
113 // and the next Stop() doesn't panic nor block
114 ar.Stop()
115 }