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
=== modified file 'protocol/protocol_test.go'
--- protocol/protocol_test.go 2014-01-14 15:35:20 +0000
+++ protocol/protocol_test.go 2014-01-24 00:49:53 +0000
@@ -22,7 +22,7 @@
22 // "fmt"22 // "fmt"
23 "io"23 "io"
24 . "launchpad.net/gocheck"24 . "launchpad.net/gocheck"
25 "net"25 teepot "launchpad.net/ubuntu-push/protocol/testing"
26 "testing"26 "testing"
27 "time"27 "time"
28)28)
@@ -33,112 +33,32 @@
3333
34var _ = Suite(&protocolSuite{})34var _ = Suite(&protocolSuite{})
3535
36type deadline struct {
37 kind string
38 deadAfter time.Duration
39}
40
41func (d *deadline) setDeadAfter(t time.Time) {
42 deadAfter := t.Sub(time.Now())
43 d.deadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond
44}
45
46type rw struct {
47 buf []byte
48 n int
49 err error
50}
51
52type testConn struct {
53 deadlines []*deadline
54 reads []rw
55 writes []*rw
56}
57
58func (tc *testConn) LocalAddr() net.Addr {
59 return nil
60}
61
62func (tc *testConn) RemoteAddr() net.Addr {
63 return nil
64}
65
66func (tc *testConn) Close() error {
67 return nil
68}
69
70func (tc *testConn) SetDeadline(t time.Time) error {
71 deadline := tc.deadlines[0]
72 deadline.kind = "both"
73 deadline.setDeadAfter(t)
74 tc.deadlines = tc.deadlines[1:]
75 return nil
76}
77
78func (tc *testConn) SetReadDeadline(t time.Time) error {
79 deadline := tc.deadlines[0]
80 deadline.kind = "read"
81 deadline.setDeadAfter(t)
82 tc.deadlines = tc.deadlines[1:]
83 return nil
84}
85
86func (tc *testConn) SetWriteDeadline(t time.Time) error {
87 deadline := tc.deadlines[0]
88 deadline.kind = "write"
89 deadline.setDeadAfter(t)
90 tc.deadlines = tc.deadlines[1:]
91 return nil
92}
93
94func (tc *testConn) Read(buf []byte) (n int, err error) {
95 read := tc.reads[0]
96 copy(buf, read.buf)
97 tc.reads = tc.reads[1:]
98 return read.n, read.err
99}
100
101func (tc *testConn) Write(buf []byte) (n int, err error) {
102 write := tc.writes[0]
103 n = copy(write.buf, buf)
104 write.buf = write.buf[:n]
105 write.n = n
106 err = write.err
107 tc.writes = tc.writes[1:]
108 return
109}
110
111func (s *protocolSuite) TestReadWireFormatVersion(c *C) {36func (s *protocolSuite) TestReadWireFormatVersion(c *C) {
112 deadl := deadline{}37 deadl := teepot.Deadline{}
113 read1 := rw{buf: []byte{42}, n: 1}38 read1 := teepot.RW{Buf: []byte{42}, N: 1}
114 tc := &testConn{reads: []rw{read1}, deadlines: []*deadline{&deadl}}39 tc := &teepot.TestConn{Reads: []teepot.RW{read1}, Deadlines: []*teepot.Deadline{&deadl}}
115 ver, err := ReadWireFormatVersion(tc, time.Minute)40 ver, err := ReadWireFormatVersion(tc, time.Minute)
116 c.Check(err, IsNil)41 c.Check(err, IsNil)
117 c.Check(ver, Equals, 42)42 c.Check(ver, Equals, 42)
118 c.Check(deadl.kind, Equals, "read")43 c.Check(deadl.Kind, Equals, "read")
119 c.Check(deadl.deadAfter, Equals, time.Minute)44 c.Check(deadl.DeadAfter, Equals, time.Minute)
120}45}
12146
122func (s *protocolSuite) TestReadWireFormatVersionError(c *C) {47func (s *protocolSuite) TestReadWireFormatVersionError(c *C) {
123 deadl := deadline{}48 deadl := teepot.Deadline{}
124 read1 := rw{err: io.EOF}49 read1 := teepot.RW{Err: io.EOF}
125 tc := &testConn{reads: []rw{read1}, deadlines: []*deadline{&deadl}}50 tc := &teepot.TestConn{Reads: []teepot.RW{read1}, Deadlines: []*teepot.Deadline{&deadl}}
126 _, err := ReadWireFormatVersion(tc, time.Minute)51 _, err := ReadWireFormatVersion(tc, time.Minute)
127 c.Check(err, Equals, io.EOF)52 c.Check(err, Equals, io.EOF)
128}53}
12954
130func (s *protocolSuite) TestSetDeadline(c *C) {55func (s *protocolSuite) TestSetDeadline(c *C) {
131 deadl := deadline{}56 deadl := teepot.Deadline{}
132 tc := &testConn{deadlines: []*deadline{&deadl}}57 tc := &teepot.TestConn{Deadlines: []*teepot.Deadline{&deadl}}
133 pc := NewProtocol0(tc)58 pc := NewProtocol0(tc)
134 pc.SetDeadline(time.Now().Add(time.Minute))59 pc.SetDeadline(time.Now().Add(time.Minute))
135 c.Check(deadl.kind, Equals, "both")60 c.Check(deadl.Kind, Equals, "both")
136 c.Check(deadl.deadAfter, Equals, time.Minute)61 c.Check(deadl.DeadAfter, Equals, time.Minute)
137}
138
139type testMsg struct {
140 Type string `json:"T"`
141 A uint64
142}62}
14363
144func lengthAsBytes(length uint16) []byte {64func lengthAsBytes(length uint16) []byte {
@@ -149,43 +69,43 @@
149}69}
15070
151func (s *protocolSuite) TestReadMessage(c *C) {71func (s *protocolSuite) TestReadMessage(c *C) {
152 msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})72 msgBuf, _ := json.Marshal(teepot.TestMsg{Type: "msg", A: 2000})
153 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}73 readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
154 readMsgBody := rw{buf: msgBuf, n: len(msgBuf)}74 readMsgBody := teepot.RW{Buf: msgBuf, N: len(msgBuf)}
155 tc := &testConn{reads: []rw{readMsgLen, readMsgBody}}75 tc := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody}}
156 pc := NewProtocol0(tc)76 pc := NewProtocol0(tc)
157 var recvMsg testMsg77 var recvMsg teepot.TestMsg
158 err := pc.ReadMessage(&recvMsg)78 err := pc.ReadMessage(&recvMsg)
159 c.Check(err, IsNil)79 c.Check(err, IsNil)
160 c.Check(recvMsg, DeepEquals, testMsg{Type: "msg", A: 2000})80 c.Check(recvMsg, DeepEquals, teepot.TestMsg{Type: "msg", A: 2000})
161}81}
16282
163func (s *protocolSuite) TestReadMessageBits(c *C) {83func (s *protocolSuite) TestReadMessageBits(c *C) {
164 msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})84 msgBuf, _ := json.Marshal(teepot.TestMsg{Type: "msg", A: 2000})
165 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}85 readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
166 readMsgBody1 := rw{buf: msgBuf[:5], n: 5}86 readMsgBody1 := teepot.RW{Buf: msgBuf[:5], N: 5}
167 readMsgBody2 := rw{buf: msgBuf[5:], n: len(msgBuf) - 5}87 readMsgBody2 := teepot.RW{Buf: msgBuf[5:], N: len(msgBuf) - 5}
168 tc := &testConn{reads: []rw{readMsgLen, readMsgBody1, readMsgBody2}}88 tc := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody1, readMsgBody2}}
169 pc := NewProtocol0(tc)89 pc := NewProtocol0(tc)
170 var recvMsg testMsg90 var recvMsg teepot.TestMsg
171 err := pc.ReadMessage(&recvMsg)91 err := pc.ReadMessage(&recvMsg)
172 c.Check(err, IsNil)92 c.Check(err, IsNil)
173 c.Check(recvMsg, DeepEquals, testMsg{Type: "msg", A: 2000})93 c.Check(recvMsg, DeepEquals, teepot.TestMsg{Type: "msg", A: 2000})
174}94}
17595
176func (s *protocolSuite) TestReadMessageIOErrors(c *C) {96func (s *protocolSuite) TestReadMessageIOErrors(c *C) {
177 msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})97 msgBuf, _ := json.Marshal(teepot.TestMsg{Type: "msg", A: 2000})
178 readMsgLenErr := rw{n: 1, err: io.ErrClosedPipe}98 readMsgLenErr := teepot.RW{N: 1, Err: io.ErrClosedPipe}
179 tc1 := &testConn{reads: []rw{readMsgLenErr}}99 tc1 := &teepot.TestConn{Reads: []teepot.RW{readMsgLenErr}}
180 pc1 := NewProtocol0(tc1)100 pc1 := NewProtocol0(tc1)
181 var recvMsg testMsg101 var recvMsg teepot.TestMsg
182 err := pc1.ReadMessage(&recvMsg)102 err := pc1.ReadMessage(&recvMsg)
183 c.Check(err, Equals, io.ErrClosedPipe)103 c.Check(err, Equals, io.ErrClosedPipe)
184104
185 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}105 readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
186 readMsgBody1 := rw{buf: msgBuf[:5], n: 5}106 readMsgBody1 := teepot.RW{Buf: msgBuf[:5], N: 5}
187 readMsgBody2Err := rw{n: 2, err: io.EOF}107 readMsgBody2Err := teepot.RW{N: 2, Err: io.EOF}
188 tc2 := &testConn{reads: []rw{readMsgLen, readMsgBody1, readMsgBody2Err}}108 tc2 := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody1, readMsgBody2Err}}
189 pc2 := NewProtocol0(tc2)109 pc2 := NewProtocol0(tc2)
190 err = pc2.ReadMessage(&recvMsg)110 err = pc2.ReadMessage(&recvMsg)
191 c.Check(err, Equals, io.EOF)111 c.Check(err, Equals, io.EOF)
@@ -193,35 +113,35 @@
193113
194func (s *protocolSuite) TestReadMessageBrokenJSON(c *C) {114func (s *protocolSuite) TestReadMessageBrokenJSON(c *C) {
195 msgBuf := []byte("{\"T\"}")115 msgBuf := []byte("{\"T\"}")
196 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}116 readMsgLen := teepot.RW{Buf: lengthAsBytes(uint16(len(msgBuf))), N: 2}
197 readMsgBody := rw{buf: msgBuf, n: len(msgBuf)}117 readMsgBody := teepot.RW{Buf: msgBuf, N: len(msgBuf)}
198 tc := &testConn{reads: []rw{readMsgLen, readMsgBody}}118 tc := &teepot.TestConn{Reads: []teepot.RW{readMsgLen, readMsgBody}}
199 pc := NewProtocol0(tc)119 pc := NewProtocol0(tc)
200 var recvMsg testMsg120 var recvMsg teepot.TestMsg
201 err := pc.ReadMessage(&recvMsg)121 err := pc.ReadMessage(&recvMsg)
202 c.Check(err, FitsTypeOf, &json.SyntaxError{})122 c.Check(err, FitsTypeOf, &json.SyntaxError{})
203}123}
204124
205func (s *protocolSuite) TestWriteMessage(c *C) {125func (s *protocolSuite) TestWriteMessage(c *C) {
206 writeMsg := rw{buf: make([]byte, 64)}126 writeMsg := teepot.RW{Buf: make([]byte, 64)}
207 tc := &testConn{writes: []*rw{&writeMsg}}127 tc := &teepot.TestConn{Writes: []*teepot.RW{&writeMsg}}
208 pc := NewProtocol0(tc)128 pc := NewProtocol0(tc)
209 msg := testMsg{Type: "m", A: 9999}129 msg := teepot.TestMsg{Type: "m", A: 9999}
210 err := pc.WriteMessage(&msg)130 err := pc.WriteMessage(&msg)
211 c.Check(err, IsNil)131 c.Check(err, IsNil)
212 var msgLen int = int(binary.BigEndian.Uint16(writeMsg.buf[:2]))132 var msgLen int = int(binary.BigEndian.Uint16(writeMsg.Buf[:2]))
213 c.Check(msgLen, Equals, len(writeMsg.buf)-2)133 c.Check(msgLen, Equals, len(writeMsg.Buf)-2)
214 var wroteMsg testMsg134 var wroteMsg teepot.TestMsg
215 formatErr := json.Unmarshal(writeMsg.buf[2:], &wroteMsg)135 formatErr := json.Unmarshal(writeMsg.Buf[2:], &wroteMsg)
216 c.Check(formatErr, IsNil)136 c.Check(formatErr, IsNil)
217 c.Check(wroteMsg, DeepEquals, testMsg{Type: "m", A: 9999})137 c.Check(wroteMsg, DeepEquals, teepot.TestMsg{Type: "m", A: 9999})
218}138}
219139
220func (s *protocolSuite) TestWriteMessageIOErrors(c *C) {140func (s *protocolSuite) TestWriteMessageIOErrors(c *C) {
221 writeMsgErr := rw{buf: make([]byte, 0), err: io.ErrClosedPipe}141 writeMsgErr := teepot.RW{Buf: make([]byte, 0), Err: io.ErrClosedPipe}
222 tc1 := &testConn{writes: []*rw{&writeMsgErr}}142 tc1 := &teepot.TestConn{Writes: []*teepot.RW{&writeMsgErr}}
223 pc1 := NewProtocol0(tc1)143 pc1 := NewProtocol0(tc1)
224 msg := testMsg{Type: "m", A: 9999}144 msg := teepot.TestMsg{Type: "m", A: 9999}
225 err := pc1.WriteMessage(&msg)145 err := pc1.WriteMessage(&msg)
226 c.Check(err, Equals, io.ErrClosedPipe)146 c.Check(err, Equals, io.ErrClosedPipe)
227}147}
228148
=== added directory 'protocol/testing'
=== added file 'protocol/testing/testing.go'
--- protocol/testing/testing.go 1970-01-01 00:00:00 +0000
+++ protocol/testing/testing.go 2014-01-24 00:49:53 +0000
@@ -0,0 +1,102 @@
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
17package testing
18
19import (
20 "net"
21 "time"
22)
23
24type Deadline struct {
25 Kind string
26 DeadAfter time.Duration
27}
28
29func (d *Deadline) SetDeadAfter(t time.Time) {
30 deadAfter := t.Sub(time.Now())
31 d.DeadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond
32}
33
34type RW struct {
35 Buf []byte
36 N int
37 Err error
38}
39
40type TestConn struct {
41 Deadlines []*Deadline
42 Reads []RW
43 Writes []*RW
44}
45
46func (tc *TestConn) LocalAddr() net.Addr {
47 return nil
48}
49
50func (tc *TestConn) RemoteAddr() net.Addr {
51 return nil
52}
53
54func (tc *TestConn) Close() error {
55 return nil
56}
57
58func (tc *TestConn) SetDeadline(t time.Time) error {
59 deadline := tc.Deadlines[0]
60 deadline.Kind = "both"
61 deadline.SetDeadAfter(t)
62 tc.Deadlines = tc.Deadlines[1:]
63 return nil
64}
65
66func (tc *TestConn) SetReadDeadline(t time.Time) error {
67 deadline := tc.Deadlines[0]
68 deadline.Kind = "read"
69 deadline.SetDeadAfter(t)
70 tc.Deadlines = tc.Deadlines[1:]
71 return nil
72}
73
74func (tc *TestConn) SetWriteDeadline(t time.Time) error {
75 deadline := tc.Deadlines[0]
76 deadline.Kind = "write"
77 deadline.SetDeadAfter(t)
78 tc.Deadlines = tc.Deadlines[1:]
79 return nil
80}
81
82func (tc *TestConn) Read(buf []byte) (n int, err error) {
83 read := tc.Reads[0]
84 copy(buf, read.Buf)
85 tc.Reads = tc.Reads[1:]
86 return read.N, read.Err
87}
88
89func (tc *TestConn) Write(buf []byte) (n int, err error) {
90 write := tc.Writes[0]
91 n = copy(write.Buf, buf)
92 write.Buf = write.Buf[:n]
93 write.N = n
94 err = write.Err
95 tc.Writes = tc.Writes[1:]
96 return
97}
98
99type TestMsg struct {
100 Type string `json:"T"`
101 A uint64
102}

Subscribers

People subscribed via source and target branches