Debian Codegraph @

protocol/messages_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 protocol
18
19 import (
20 "encoding/json"
21 "fmt"
22 "strings"
23
24 . "gopkg.in/check.v1"
25 )
26
27 type messagesSuite struct{}
28
29 var _ = Suite(&messagesSuite{})
30
31 func (s *messagesSuite) TestSplitBroadcastMsgNop(c *C) {
32 b := &BroadcastMsg{
33 Type: "broadcast",
34 AppId: "APP",
35 ChanId: "0",
36 TopLevel: 2,
37 Payloads: []json.RawMessage{json.RawMessage(`{b:1}`), json.RawMessage(`{b:2}`)},
38 }
39 done := b.Split()
40 c.Check(done, Equals, true)
41 c.Check(b.TopLevel, Equals, int64(2))
42 c.Check(cap(b.Payloads), Equals, 2)
43 c.Check(len(b.Payloads), Equals, 2)
44 }
45
46 var payloadFmt = fmt.Sprintf(`{"b":%%d,"bloat":"%s"}`, strings.Repeat("x", 1024*2))
47
48 func manyParts(c int) []json.RawMessage {
49 payloads := make([]json.RawMessage, 0, 1)
50 for i := 0; i < c; i++ {
51 payloads = append(payloads, json.RawMessage(fmt.Sprintf(payloadFmt, i)))
52 }
53 return payloads
54 }
55
56 func (s *messagesSuite) TestSplitBroadcastMsgManyParts(c *C) {
57 payloads := manyParts(33)
58 n := len(payloads)
59 // more interesting this way
60 c.Assert(cap(payloads), Not(Equals), n)
61 b := &BroadcastMsg{
62 Type: "broadcast",
63 AppId: "APP",
64 ChanId: "0",
65 TopLevel: 500,
66 Payloads: payloads,
67 }
68 done := b.Split()
69 c.Assert(done, Equals, false)
70 n1 := len(b.Payloads)
71 c.Check(b.TopLevel, Equals, int64(500-n+n1))
72 buf, err := json.Marshal(b)
73 c.Assert(err, IsNil)
74 c.Assert(len(buf) <= 65535, Equals, true)
75 c.Check(len(buf)+len(payloads[n1]) > maxPayloadSize, Equals, true)
76 done = b.Split()
77 c.Assert(done, Equals, true)
78 n2 := len(b.Payloads)
79 c.Check(b.TopLevel, Equals, int64(500))
80 c.Check(n1+n2, Equals, n)
81
82 payloads = manyParts(61)
83 n = len(payloads)
84 b = &BroadcastMsg{
85 Type: "broadcast",
86 AppId: "APP",
87 ChanId: "0",
88 TopLevel: int64(n),
89 Payloads: payloads,
90 }
91 done = b.Split()
92 c.Assert(done, Equals, false)
93 n1 = len(b.Payloads)
94 done = b.Split()
95 c.Assert(done, Equals, false)
96 n2 = len(b.Payloads)
97 done = b.Split()
98 c.Assert(done, Equals, true)
99 n3 := len(b.Payloads)
100 c.Check(b.TopLevel, Equals, int64(n))
101 c.Check(n1+n2+n3, Equals, n)
102 // reset
103 b.Type = ""
104 b.Reset()
105 c.Check(b.Type, Equals, "broadcast")
106 c.Check(b.splitting, Equals, 0)
107 }
108
109 func (s *messagesSuite) TestConnBrokenMsg(c *C) {
110 m := &ConnBrokenMsg{}
111 c.Check(m.Split(), Equals, true)
112 c.Check(m.OnewayContinue(), Equals, false)
113 }
114
115 func (s *messagesSuite) TestConnWarnMsg(c *C) {
116 m := &ConnWarnMsg{}
117 c.Check(m.Split(), Equals, true)
118 c.Check(m.OnewayContinue(), Equals, true)
119 }
120
121 func (s *messagesSuite) TestSetParamsMsg(c *C) {
122 m := &SetParamsMsg{}
123 c.Check(m.Split(), Equals, true)
124 c.Check(m.OnewayContinue(), Equals, true)
125 }
126
127 func (s *messagesSuite) TestExtractPayloads(c *C) {
128 c.Check(ExtractPayloads(nil), IsNil)
129 p1 := json.RawMessage(`{"a":1}`)
130 p2 := json.RawMessage(`{"b":2}`)
131 ns := []Notification{Notification{Payload: p1}, Notification{Payload: p2}}
132 c.Check(ExtractPayloads(ns), DeepEquals, []json.RawMessage{p1, p2})
133 }
134
135 func (s *messagesSuite) TestSplitNotificationsMsgNop(c *C) {
136 n := &NotificationsMsg{
137 Type: "notifications",
138 Notifications: []Notification{
139 Notification{"app1", "msg1", json.RawMessage(`{m:1}`)},
140 Notification{"app1", "msg1", json.RawMessage(`{m:2}`)},
141 },
142 }
143 done := n.Split()
144 c.Check(done, Equals, true)
145 c.Check(cap(n.Notifications), Equals, 2)
146 c.Check(len(n.Notifications), Equals, 2)
147 }
148
149 var payloadFmt2 = fmt.Sprintf(`{"b":%%d,"bloat":"%s"}`, strings.Repeat("x", 1024*2-notificationOverhead-4-6)) // 4 = app1 6 = msg%03d
150
151 func manyNotifications(c int) []Notification {
152 notifs := make([]Notification, 0, 1)
153 for i := 0; i < c; i++ {
154 notifs = append(notifs, Notification{
155 "app1",
156 fmt.Sprintf("msg%03d", i),
157 json.RawMessage(fmt.Sprintf(payloadFmt2, i)),
158 })
159 }
160 return notifs
161 }
162
163 func (s *messagesSuite) TestSplitNotificationsMsgMany(c *C) {
164 notifs := manyNotifications(33)
165 n := len(notifs)
166 // more interesting this way
167 c.Assert(cap(notifs), Not(Equals), n)
168 nm := &NotificationsMsg{
169 Type: "notifications",
170 Notifications: notifs,
171 }
172 done := nm.Split()
173 c.Assert(done, Equals, false)
174 n1 := len(nm.Notifications)
175 buf, err := json.Marshal(nm)
176 c.Assert(err, IsNil)
177 c.Assert(len(buf) <= 65535, Equals, true)
178 c.Check(len(buf)+len(notifs[n1].Payload) > maxPayloadSize, Equals, true)
179 done = nm.Split()
180 c.Assert(done, Equals, true)
181 n2 := len(nm.Notifications)
182 c.Check(n1+n2, Equals, n)
183
184 notifs = manyNotifications(61)
185 n = len(notifs)
186 nm = &NotificationsMsg{
187 Type: "notifications",
188 Notifications: notifs,
189 }
190 done = nm.Split()
191 c.Assert(done, Equals, false)
192 n1 = len(nm.Notifications)
193 done = nm.Split()
194 c.Assert(done, Equals, false)
195 n2 = len(nm.Notifications)
196 done = nm.Split()
197 c.Assert(done, Equals, true)
198 n3 := len(nm.Notifications)
199 c.Check(n1+n2+n3, Equals, n)
200 // reset
201 nm.Type = ""
202 nm.Reset()
203 c.Check(nm.Type, Equals, "notifications")
204 c.Check(nm.splitting, Equals, 0)
205 }