Debian Codegraph @

bus/polld/polld_test.go

raw
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
17 package polld
18
19 import (
20 "testing"
21 "time"
22
23 . "gopkg.in/check.v1"
24
25 testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing"
26 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
27 "gitlab.com/ubports/development/core/lomiri-push-service/testing/condition"
28 )
29
30 // hook up gocheck
31 func TestPolld(t *testing.T) { TestingT(t) }
32
33 type PdSuite struct {
34 log *helpers.TestLogger
35 }
36
37 var _ = Suite(&PdSuite{})
38
39 func (s *PdSuite) SetUpTest(c *C) {
40 s.log = helpers.NewTestLogger(c, "debug")
41 }
42
43 func (s *PdSuite) TestPollWorks(c *C) {
44 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
45 pd := New(endp, s.log)
46 err := pd.Poll()
47 c.Assert(err, IsNil)
48 args := testibus.GetCallArgs(endp)
49 c.Assert(args, HasLen, 1)
50 c.Check(args[0].Member, Equals, "Poll")
51 c.Check(args[0].Args, IsNil)
52 }
53
54 func (s *PdSuite) TestPollUnconfigured(c *C) {
55 c.Check(new(polld).Poll(), Equals, ErrUnconfigured)
56 }
57
58 func (s *PdSuite) TestPollFails(c *C) {
59 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
60 pd := New(endp, s.log)
61 c.Check(pd.Poll(), NotNil)
62 }
63
64 func (s *PdSuite) TestWatchDonesWorks(c *C) {
65 endp := testibus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{})
66 pd := New(endp, s.log)
67 ch, err := pd.WatchDones()
68 c.Assert(err, IsNil)
69 select {
70 case b := <-ch:
71 c.Check(b, Equals, true)
72 case <-time.After(100 * time.Millisecond):
73 c.Error("timeout waiting for bool")
74 }
75 select {
76 case b := <-ch:
77 c.Check(b, Equals, false)
78 case <-time.After(100 * time.Millisecond):
79 c.Error("timeout waiting for close")
80 }
81 }
82
83 func (s *PdSuite) TestWatchDonesUnconfigured(c *C) {
84 _, err := new(polld).WatchDones()
85 c.Check(err, Equals, ErrUnconfigured)
86 }