Debian Codegraph @

server/session/tracker_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 session
18
19 import (
20 "fmt"
21 "net"
22
23 . "gopkg.in/check.v1"
24
25 "gitlab.com/ubports/development/core/lomiri-push-service/server/broker"
26 "gitlab.com/ubports/development/core/lomiri-push-service/server/broker/testing"
27 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
28 )
29
30 type trackerSuite struct {
31 testlog *helpers.TestLogger
32 }
33
34 func (s *trackerSuite) SetUpTest(c *C) {
35 s.testlog = helpers.NewTestLogger(c, "debug")
36 }
37
38 var _ = Suite(&trackerSuite{})
39
40 type testRemoteAddrable struct{}
41
42 func (tra *testRemoteAddrable) RemoteAddr() net.Addr {
43 return &net.TCPAddr{net.IPv4(127, 0, 0, 1), 9999, ""}
44 }
45
46 func (s *trackerSuite) TestSessionTrackStart(c *C) {
47 track := NewTracker(s.testlog)
48 track.Start(&testRemoteAddrable{})
49 c.Check(track.SessionId(), Not(Equals), "")
50 regExpected := fmt.Sprintf(`DEBUG session\(%s\) connected 127\.0\.0\.1:9999\n`, track.SessionId())
51 c.Check(s.testlog.Captured(), Matches, regExpected)
52 }
53
54 func (s *trackerSuite) TestSessionTrackRegistered(c *C) {
55 track := NewTracker(s.testlog)
56 track.Start(&testRemoteAddrable{})
57 track.Registered(&testing.TestBrokerSession{DeviceId: "DEV-ID"})
58 regExpected := fmt.Sprintf(`.*connected.*\nINFO session\(%s\) registered DEV-ID\n`, track.SessionId())
59 c.Check(s.testlog.Captured(), Matches, regExpected)
60 }
61
62 func (s *trackerSuite) TestSessionTrackEnd(c *C) {
63 track := NewTracker(s.testlog)
64 track.Start(&testRemoteAddrable{})
65 track.End(&broker.ErrAbort{})
66 regExpected := fmt.Sprintf(`.*connected.*\nDEBUG session\(%s\) ended with: session aborted \(\)\n`, track.SessionId())
67 c.Check(s.testlog.Captured(), Matches, regExpected)
68 }