~jonas-drange/ubuntu-push/lp1577723-skiptest

« back to all changes in this revision

Viewing changes to poller/poller_test.go

  • Committer: John R. Lenton
  • Date: 2014-08-21 10:47:12 UTC
  • mto: This revision was merged to the branch mainline in revision 315.
  • Revision ID: jlenton@gmail.com-20140821104712-7i03q9eqotfdadzu
poller

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright 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
package poller
 
17
 
 
18
import (
 
19
        "testing"
 
20
        "time"
 
21
 
 
22
        . "launchpad.net/gocheck"
 
23
 
 
24
        "launchpad.net/ubuntu-push/client/session"
 
25
        helpers "launchpad.net/ubuntu-push/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
        return m.reqWakeCookie, m.reqWakeErr
 
67
}
 
68
func (m *myD) RequestWakelock(name string) (string, error) {
 
69
        m.reqLockName = name
 
70
        return m.reqLockCookie, m.reqLockErr
 
71
}
 
72
func (m *myD) ClearWakelock(cookie string) error {
 
73
        m.clearLockCookie = cookie
 
74
        return m.clearLockErr
 
75
}
 
76
func (m *myD) ClearWakeup(cookie string) error    { panic("clearwakeup called??") }
 
77
func (m *myD) WatchWakeups() (<-chan bool, error) { return m.watchWakeCh, m.watchWakeErr }
 
78
func (m *myD) Poll() error                        { return m.pollErr }
 
79
func (m *myD) WatchDones() (<-chan bool, error)   { return m.watchDonesCh, m.watchDonesErr }
 
80
func (m *myD) State() session.ClientSessionState  { return m.stateState }
 
81
 
 
82
func (s *PrSuite) SetUpTest(c *C) {
 
83
        s.log = helpers.NewTestLogger(c, "debug")
 
84
        s.myd = &myD{}
 
85
}
 
86
 
 
87
func (s *PrSuite) TestStep(c *C) {
 
88
        p := &poller{
 
89
                times:        Times{},
 
90
                log:          s.log,
 
91
                powerd:       s.myd,
 
92
                polld:        s.myd,
 
93
                sessionState: s.myd,
 
94
        }
 
95
        s.myd.reqLockCookie = "wakelock cookie"
 
96
        s.myd.stateState = session.Running
 
97
        // we'll get the wakeup right away
 
98
        wakeupCh := make(chan bool, 1)
 
99
        wakeupCh <- true
 
100
        // we won't get the "done" signal in time ;)
 
101
        doneCh := make(chan bool)
 
102
        // and a channel to get the return value from a goroutine
 
103
        ch := make(chan string)
 
104
        // now, run
 
105
        go func() { ch <- p.step(wakeupCh, doneCh, "old cookie") }()
 
106
        select {
 
107
        case s := <-ch:
 
108
                c.Check(s, Equals, "wakelock cookie")
 
109
        case <-time.After(time.Second):
 
110
                c.Fatal("timeout waiting for step")
 
111
        }
 
112
}