Merge lp:~chipaca/ubuntu-push/protocol-testing into lp:ubuntu-push

Proposed by John Lenton
Status: Rejected
Rejected by: John Lenton
Proposed branch: lp:~chipaca/ubuntu-push/protocol-testing
Merge into: lp:ubuntu-push
Prerequisite: lp:~chipaca/ubuntu-push/bushtest
Diff against target: 361 lines (+151/-129)
2 files modified
protocol/protocol_test.go (+49/-129)
protocol/testing/testing.go (+102/-0)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/protocol-testing
Reviewer Review Type Date Requested Status
Samuele Pedroni Needs Information
Review via email: mp+202970@code.launchpad.net

Commit message

split the testing facilities present in protocol_test out into protocol/testing

Description of the change

split the testing facilities present in protocol_test out into protocol/testing

To post a comment you must log in.
27. By John Lenton

oops, testMsg too

28. By John Lenton

and if I remember to bzr add testing, everybody is happy

Revision history for this message
Samuele Pedroni (pedronis) wrote :

I'm a bit wondering why we would need to reuse these instead of testProtocol and friends in session_test.go

review: Needs Fixing
Revision history for this message
Samuele Pedroni (pedronis) wrote :

sorry, I wanted to pick need information

review: Needs Information
Revision history for this message
John Lenton (chipaca) wrote :

Ignorance, alone. Will tinker with testProtocol, hopefully it does what I need -- testConn is quite slow going!

Unmerged revisions

28. By John Lenton

and if I remember to bzr add testing, everybody is happy

27. By John Lenton

oops, testMsg too

26. By John Lenton

split the testing facilities present in protocol_test out into protocol/testing

25. By John Lenton

Merged redialer into bushtest.

24. By John Lenton

unit tests, schmnit tests. Covered a bit of bus with an integration test.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'protocol/protocol_test.go'
2--- protocol/protocol_test.go 2014-01-14 15:35:20 +0000
3+++ protocol/protocol_test.go 2014-01-24 00:49:53 +0000
4@@ -22,7 +22,7 @@
5 // "fmt"
6 "io"
7 . "launchpad.net/gocheck"
8- "net"
9+ teepot "launchpad.net/ubuntu-push/protocol/testing"
10 "testing"
11 "time"
12 )
13@@ -33,112 +33,32 @@
14
15 var _ = Suite(&protocolSuite{})
16
17-type deadline struct {
18- kind string
19- deadAfter time.Duration
20-}
21-
22-func (d *deadline) setDeadAfter(t time.Time) {
23- deadAfter := t.Sub(time.Now())
24- d.deadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond
25-}
26-
27-type rw struct {
28- buf []byte
29- n int
30- err error
31-}
32-
33-type testConn struct {
34- deadlines []*deadline
35- reads []rw
36- writes []*rw
37-}
38-
39-func (tc *testConn) LocalAddr() net.Addr {
40- return nil
41-}
42-
43-func (tc *testConn) RemoteAddr() net.Addr {
44- return nil
45-}
46-
47-func (tc *testConn) Close() error {
48- return nil
49-}
50-
51-func (tc *testConn) SetDeadline(t time.Time) error {
52- deadline := tc.deadlines[0]
53- deadline.kind = "both"
54- deadline.setDeadAfter(t)
55- tc.deadlines = tc.deadlines[1:]
56- return nil
57-}
58-
59-func (tc *testConn) SetReadDeadline(t time.Time) error {
60- deadline := tc.deadlines[0]
61- deadline.kind = "read"
62- deadline.setDeadAfter(t)
63- tc.deadlines = tc.deadlines[1:]
64- return nil
65-}
66-
67-func (tc *testConn) SetWriteDeadline(t time.Time) error {
68- deadline := tc.deadlines[0]
69- deadline.kind = "write"
70- deadline.setDeadAfter(t)
71- tc.deadlines = tc.deadlines[1:]
72- return nil
73-}
74-
75-func (tc *testConn) Read(buf []byte) (n int, err error) {
76- read := tc.reads[0]
77- copy(buf, read.buf)
78- tc.reads = tc.reads[1:]
79- return read.n, read.err
80-}
81-
82-func (tc *testConn) Write(buf []byte) (n int, err error) {
83- write := tc.writes[0]
84- n = copy(write.buf, buf)
85- write.buf = write.buf[:n]
86- write.n = n
87- err = write.err
88- tc.writes = tc.writes[1:]
89- return
90-}
91-
92 func (s *protocolSuite) TestReadWireFormatVersion(c *C) {
93- deadl := deadline{}
94- read1 := rw{buf: []byte{42}, n: 1}
95- tc := &testConn{reads: []rw{read1}, deadlines: []*deadline{&deadl}}
96+ deadl := teepot.Deadline{}
97+ read1 := teepot.RW{Buf: []byte{42}, N: 1}
98+ tc := &teepot.TestConn{Reads: []teepot.RW{read1}, Deadlines: []*teepot.Deadline{&deadl}}
99 ver, err := ReadWireFormatVersion(tc, time.Minute)
100 c.Check(err, IsNil)
101 c.Check(ver, Equals, 42)
102- c.Check(deadl.kind, Equals, "read")
103- c.Check(deadl.deadAfter, Equals, time.Minute)
104+ c.Check(deadl.Kind, Equals, "read")
105+ c.Check(deadl.DeadAfter, Equals, time.Minute)
106 }
107
108 func (s *protocolSuite) TestReadWireFormatVersionError(c *C) {
109- deadl := deadline{}
110- read1 := rw{err: io.EOF}
111- tc := &testConn{reads: []rw{read1}, deadlines: []*deadline{&deadl}}
112+ deadl := teepot.Deadline{}
113+ read1 := teepot.RW{Err: io.EOF}
114+ tc := &teepot.TestConn{Reads: []teepot.RW{read1}, Deadlines: []*teepot.Deadline{&deadl}}
115 _, err := ReadWireFormatVersion(tc, time.Minute)
116 c.Check(err, Equals, io.EOF)
117 }
118
119 func (s *protocolSuite) TestSetDeadline(c *C) {
120- deadl := deadline{}
121- tc := &testConn{deadlines: []*deadline{&deadl}}
122+ deadl := teepot.Deadline{}
123+ tc := &teepot.TestConn{Deadlines: []*teepot.Deadline{&deadl}}
124 pc := NewProtocol0(tc)
125 pc.SetDeadline(time.Now().Add(time.Minute))
126- c.Check(deadl.kind, Equals, "both")
127- c.Check(deadl.deadAfter, Equals, time.Minute)
128-}
129-
130-type testMsg struct {
131- Type string `json:"T"`
132- A uint64
133+ c.Check(deadl.Kind, Equals, "both")
134+ c.Check(deadl.DeadAfter, Equals, time.Minute)
135 }
136
137 func lengthAsBytes(length uint16) []byte {
138@@ -149,43 +69,43 @@
139 }
140
141 func (s *protocolSuite) TestReadMessage(c *C) {
142- msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})
143- readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
144- readMsgBody := rw{buf: msgBuf, n: len(msgBuf)}
145- tc := &testConn{reads: []rw{readMsgLen, readMsgBody}}
146+ msgBuf, _ := json.Marshal(teepot.TestMsg{Type: "msg", A: 2000})
147+ readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
148+ readMsgBody := teepot.RW{Buf: msgBuf, N: len(msgBuf)}
149+ tc := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody}}
150 pc := NewProtocol0(tc)
151- var recvMsg testMsg
152+ var recvMsg teepot.TestMsg
153 err := pc.ReadMessage(&recvMsg)
154 c.Check(err, IsNil)
155- c.Check(recvMsg, DeepEquals, testMsg{Type: "msg", A: 2000})
156+ c.Check(recvMsg, DeepEquals, teepot.TestMsg{Type: "msg", A: 2000})
157 }
158
159 func (s *protocolSuite) TestReadMessageBits(c *C) {
160- msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})
161- readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
162- readMsgBody1 := rw{buf: msgBuf[:5], n: 5}
163- readMsgBody2 := rw{buf: msgBuf[5:], n: len(msgBuf) - 5}
164- tc := &testConn{reads: []rw{readMsgLen, readMsgBody1, readMsgBody2}}
165+ msgBuf, _ := json.Marshal(teepot.TestMsg{Type: "msg", A: 2000})
166+ readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
167+ readMsgBody1 := teepot.RW{Buf: msgBuf[:5], N: 5}
168+ readMsgBody2 := teepot.RW{Buf: msgBuf[5:], N: len(msgBuf) - 5}
169+ tc := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody1, readMsgBody2}}
170 pc := NewProtocol0(tc)
171- var recvMsg testMsg
172+ var recvMsg teepot.TestMsg
173 err := pc.ReadMessage(&recvMsg)
174 c.Check(err, IsNil)
175- c.Check(recvMsg, DeepEquals, testMsg{Type: "msg", A: 2000})
176+ c.Check(recvMsg, DeepEquals, teepot.TestMsg{Type: "msg", A: 2000})
177 }
178
179 func (s *protocolSuite) TestReadMessageIOErrors(c *C) {
180- msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})
181- readMsgLenErr := rw{n: 1, err: io.ErrClosedPipe}
182- tc1 := &testConn{reads: []rw{readMsgLenErr}}
183+ msgBuf, _ := json.Marshal(teepot.TestMsg{Type: "msg", A: 2000})
184+ readMsgLenErr := teepot.RW{N: 1, Err: io.ErrClosedPipe}
185+ tc1 := &teepot.TestConn{Reads: []teepot.RW{readMsgLenErr}}
186 pc1 := NewProtocol0(tc1)
187- var recvMsg testMsg
188+ var recvMsg teepot.TestMsg
189 err := pc1.ReadMessage(&recvMsg)
190 c.Check(err, Equals, io.ErrClosedPipe)
191
192- readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
193- readMsgBody1 := rw{buf: msgBuf[:5], n: 5}
194- readMsgBody2Err := rw{n: 2, err: io.EOF}
195- tc2 := &testConn{reads: []rw{readMsgLen, readMsgBody1, readMsgBody2Err}}
196+ readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
197+ readMsgBody1 := teepot.RW{Buf: msgBuf[:5], N: 5}
198+ readMsgBody2Err := teepot.RW{N: 2, Err: io.EOF}
199+ tc2 := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody1, readMsgBody2Err}}
200 pc2 := NewProtocol0(tc2)
201 err = pc2.ReadMessage(&recvMsg)
202 c.Check(err, Equals, io.EOF)
203@@ -193,35 +113,35 @@
204
205 func (s *protocolSuite) TestReadMessageBrokenJSON(c *C) {
206 msgBuf := []byte("{\"T\"}")
207- readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
208- readMsgBody := rw{buf: msgBuf, n: len(msgBuf)}
209- tc := &testConn{reads: []rw{readMsgLen, readMsgBody}}
210+ readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
211+ readMsgBody := teepot.RW{Buf: msgBuf, N: len(msgBuf)}
212+ tc := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody}}
213 pc := NewProtocol0(tc)
214- var recvMsg testMsg
215+ var recvMsg teepot.TestMsg
216 err := pc.ReadMessage(&recvMsg)
217 c.Check(err, FitsTypeOf, &json.SyntaxError{})
218 }
219
220 func (s *protocolSuite) TestWriteMessage(c *C) {
221- writeMsg := rw{buf: make([]byte, 64)}
222- tc := &testConn{writes: []*rw{&writeMsg}}
223+ writeMsg := teepot.RW{Buf: make([]byte, 64)}
224+ tc := &teepot.TestConn{Writes: []*teepot.RW{&writeMsg}}
225 pc := NewProtocol0(tc)
226- msg := testMsg{Type: "m", A: 9999}
227+ msg := teepot.TestMsg{Type: "m", A: 9999}
228 err := pc.WriteMessage(&msg)
229 c.Check(err, IsNil)
230- var msgLen int = int(binary.BigEndian.Uint16(writeMsg.buf[:2]))
231- c.Check(msgLen, Equals, len(writeMsg.buf)-2)
232- var wroteMsg testMsg
233- formatErr := json.Unmarshal(writeMsg.buf[2:], &wroteMsg)
234+ var msgLen int = int(binary.BigEndian.Uint16(writeMsg.Buf[:2]))
235+ c.Check(msgLen, Equals, len(writeMsg.Buf)-2)
236+ var wroteMsg teepot.TestMsg
237+ formatErr := json.Unmarshal(writeMsg.Buf[2:], &wroteMsg)
238 c.Check(formatErr, IsNil)
239- c.Check(wroteMsg, DeepEquals, testMsg{Type: "m", A: 9999})
240+ c.Check(wroteMsg, DeepEquals, teepot.TestMsg{Type: "m", A: 9999})
241 }
242
243 func (s *protocolSuite) TestWriteMessageIOErrors(c *C) {
244- writeMsgErr := rw{buf: make([]byte, 0), err: io.ErrClosedPipe}
245- tc1 := &testConn{writes: []*rw{&writeMsgErr}}
246+ writeMsgErr := teepot.RW{Buf: make([]byte, 0), Err: io.ErrClosedPipe}
247+ tc1 := &teepot.TestConn{Writes: []*teepot.RW{&writeMsgErr}}
248 pc1 := NewProtocol0(tc1)
249- msg := testMsg{Type: "m", A: 9999}
250+ msg := teepot.TestMsg{Type: "m", A: 9999}
251 err := pc1.WriteMessage(&msg)
252 c.Check(err, Equals, io.ErrClosedPipe)
253 }
254
255=== added directory 'protocol/testing'
256=== added file 'protocol/testing/testing.go'
257--- protocol/testing/testing.go 1970-01-01 00:00:00 +0000
258+++ protocol/testing/testing.go 2014-01-24 00:49:53 +0000
259@@ -0,0 +1,102 @@
260+/*
261+ Copyright 2013-2014 Canonical Ltd.
262+
263+ This program is free software: you can redistribute it and/or modify it
264+ under the terms of the GNU General Public License version 3, as published
265+ by the Free Software Foundation.
266+
267+ This program is distributed in the hope that it will be useful, but
268+ WITHOUT ANY WARRANTY; without even the implied warranties of
269+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
270+ PURPOSE. See the GNU General Public License for more details.
271+
272+ You should have received a copy of the GNU General Public License along
273+ with this program. If not, see <http://www.gnu.org/licenses/>.
274+*/
275+
276+package testing
277+
278+import (
279+ "net"
280+ "time"
281+)
282+
283+type Deadline struct {
284+ Kind string
285+ DeadAfter time.Duration
286+}
287+
288+func (d *Deadline) SetDeadAfter(t time.Time) {
289+ deadAfter := t.Sub(time.Now())
290+ d.DeadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond
291+}
292+
293+type RW struct {
294+ Buf []byte
295+ N int
296+ Err error
297+}
298+
299+type TestConn struct {
300+ Deadlines []*Deadline
301+ Reads []RW
302+ Writes []*RW
303+}
304+
305+func (tc *TestConn) LocalAddr() net.Addr {
306+ return nil
307+}
308+
309+func (tc *TestConn) RemoteAddr() net.Addr {
310+ return nil
311+}
312+
313+func (tc *TestConn) Close() error {
314+ return nil
315+}
316+
317+func (tc *TestConn) SetDeadline(t time.Time) error {
318+ deadline := tc.Deadlines[0]
319+ deadline.Kind = "both"
320+ deadline.SetDeadAfter(t)
321+ tc.Deadlines = tc.Deadlines[1:]
322+ return nil
323+}
324+
325+func (tc *TestConn) SetReadDeadline(t time.Time) error {
326+ deadline := tc.Deadlines[0]
327+ deadline.Kind = "read"
328+ deadline.SetDeadAfter(t)
329+ tc.Deadlines = tc.Deadlines[1:]
330+ return nil
331+}
332+
333+func (tc *TestConn) SetWriteDeadline(t time.Time) error {
334+ deadline := tc.Deadlines[0]
335+ deadline.Kind = "write"
336+ deadline.SetDeadAfter(t)
337+ tc.Deadlines = tc.Deadlines[1:]
338+ return nil
339+}
340+
341+func (tc *TestConn) Read(buf []byte) (n int, err error) {
342+ read := tc.Reads[0]
343+ copy(buf, read.Buf)
344+ tc.Reads = tc.Reads[1:]
345+ return read.N, read.Err
346+}
347+
348+func (tc *TestConn) Write(buf []byte) (n int, err error) {
349+ write := tc.Writes[0]
350+ n = copy(write.Buf, buf)
351+ write.Buf = write.Buf[:n]
352+ write.N = n
353+ err = write.Err
354+ tc.Writes = tc.Writes[1:]
355+ return
356+}
357+
358+type TestMsg struct {
359+ Type string `json:"T"`
360+ A uint64
361+}

Subscribers

People subscribed via source and target branches