Debian Codegraph @

poller/poller_test.go

raw
1 /*
2 Copyright 2014-2015 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 package poller
17
18 import (
19 "testing"
20 "time"
21
22 . "gopkg.in/check.v1"
23
24 "gitlab.com/ubports/development/core/lomiri-push-service/client/session"
25 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
26 )
27
28 // hook up gocheck
29 func TestPoller(t *testing.T) { TestingT(t) }
30
31 type PrSuite struct {
32 log *helpers.TestLogger
33 myd *myD
34 }
35
36 var _ = Suite(&PrSuite{})
37
38 type myD struct {
39 // in/out for RequestWakeup
40 reqWakeName string
41 reqWakeTime time.Time
42 reqWakeCookie string
43 reqWakeErr error
44 // WatchWakeups
45 watchWakeCh chan bool
46 watchWakeErr error
47 // RequestWakelock
48 reqLockName string
49 reqLockCookie string
50 reqLockErr error
51 // ClearWakelock
52 clearLockCookie string
53 clearLockErr error
54 // Poll
55 pollErr error
56 // WatchDones
57 watchDonesCh <-chan bool
58 watchDonesErr error
59 // State
60 stateState session.ClientSessionState
61 }
62
63 func (m *myD) RequestWakeup(name string, wakeupTime time.Time) (string, error) {
64 m.reqWakeName = name
65 m.reqWakeTime = wakeupTime
66 time.AfterFunc(100*time.Millisecond, func() {
67 m.watchWakeCh <- true
68 })
69 return m.reqWakeCookie, m.reqWakeErr
70 }
71 func (m *myD) RequestWakelock(name string) (string, error) {
72 m.reqLockName = name
73 return m.reqLockCookie, m.reqLockErr
74 }
75 func (m *myD) ClearWakelock(cookie string) error {
76 m.clearLockCookie = cookie
77 return m.clearLockErr
78 }
79 func (m *myD) ClearWakeup(cookie string) error {
80 m.watchWakeCh <- false
81 return nil
82 }
83 func (m *myD) WatchWakeups() (<-chan bool, error) { return m.watchWakeCh, m.watchWakeErr }
84 func (m *myD) Poll() error { return m.pollErr }
85 func (m *myD) WatchDones() (<-chan bool, error) { return m.watchDonesCh, m.watchDonesErr }
86 func (m *myD) State() session.ClientSessionState { return m.stateState }
87
88 func (s *PrSuite) SetUpTest(c *C) {
89 s.log = helpers.NewTestLogger(c, "debug")
90 s.myd = &myD{}
91 }
92
93 func (s *PrSuite) TestStep(c *C) {
94 p := &poller{
95 times: Times{},
96 log: s.log,
97 powerd: s.myd,
98 polld: s.myd,
99 sessionState: s.myd,
100 requestWakeupCh: make(chan struct{}),
101 requestedWakeupErrCh: make(chan error),
102 holdsWakeLockCh: make(chan bool),
103 connCh: make(chan bool),
104 }
105 s.myd.reqLockCookie = "wakelock cookie"
106 s.myd.stateState = session.Running
107 wakeupCh := make(chan bool, 1)
108 s.myd.watchWakeCh = wakeupCh
109 // we won't get the "done" signal in time ;)
110 doneCh := make(chan bool)
111 // and a channel to get the return value from a goroutine
112 ch := make(chan string)
113 // now, run
114 filteredWakeUpCh := make(chan bool)
115 go p.control(wakeupCh, filteredWakeUpCh)
116 go func() { ch <- p.step(filteredWakeUpCh, doneCh, "old cookie") }()
117 select {
118 case s := <-ch:
119 c.Check(s, Equals, "wakelock cookie")
120 case <-time.After(time.Second):
121 c.Fatal("timeout waiting for step")
122 }
123 // check we cleared the old cookie
124 c.Check(s.myd.clearLockCookie, Equals, "old cookie")
125 }
126
127 func (s *PrSuite) TestControl(c *C) {
128 p := &poller{
129 times: Times{},
130 log: s.log,
131 powerd: s.myd,
132 polld: s.myd,
133 sessionState: s.myd,
134 requestWakeupCh: make(chan struct{}),
135 requestedWakeupErrCh: make(chan error),
136 holdsWakeLockCh: make(chan bool),
137 connCh: make(chan bool),
138 }
139 wakeUpCh := make(chan bool)
140 filteredWakeUpCh := make(chan bool)
141 s.myd.watchWakeCh = make(chan bool, 1)
142 go p.control(wakeUpCh, filteredWakeUpCh)
143
144 // works
145 p.HasConnectivity(true)
146 err := p.requestWakeup()
147 c.Assert(err, IsNil)
148 c.Check(<-s.myd.watchWakeCh, Equals, true)
149
150 // there's a wakeup already
151 err = p.requestWakeup()
152 c.Assert(err, IsNil)
153 c.Check(s.myd.watchWakeCh, HasLen, 0)
154
155 // wakeup happens
156 wakeUpCh <- true
157 <-filteredWakeUpCh
158
159 p.HasConnectivity(false)
160 err = p.requestWakeup()
161 c.Assert(err, IsNil)
162 c.Check(s.myd.watchWakeCh, HasLen, 0)
163
164 // connected
165 p.HasConnectivity(true)
166 c.Check(<-s.myd.watchWakeCh, Equals, true)
167
168 // disconnected
169 p.HasConnectivity(false)
170 // pending wakeup was cleared
171 c.Check(<-s.myd.watchWakeCh, Equals, false)
172
173 }