Merge lp:~chipaca/ubuntu-push/advanced-client-session into lp:ubuntu-push

Proposed by John Lenton
Status: Merged
Approved by: John Lenton
Approved revision: 40
Merged at revision: 36
Proposed branch: lp:~chipaca/ubuntu-push/advanced-client-session
Merge into: lp:ubuntu-push
Prerequisite: lp:~chipaca/ubuntu-push/client-session
Diff against target: 138 lines (+101/-0)
2 files modified
client/session/session.go (+8/-0)
client/session/session_test.go (+93/-0)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/advanced-client-session
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+204112@code.launchpad.net

Commit message

The handling of the ping.

Description of the change

Client session, volume 2: Advanced client session.

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

Merged client-session into advanced-client-session.

37. By John Lenton

Merged client-session into advanced-client-session.

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

protocol itself has a SetDeadline that should be used after you got a protocol and not a plain connection

review: Needs Fixing
38. By John Lenton

seriously bad protocol breakage spotted by pedronis; fixed

39. By John Lenton

Merged watchticker-goes-to-the-races into advanced-client-session.

40. By John Lenton

Assert instead of Check when checking the length of a chan you are about to read from

Revision history for this message
Samuele Pedroni (pedronis) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'client/session/session.go'
--- client/session/session.go 2014-01-31 12:44:29 +0000
+++ client/session/session.go 2014-01-31 14:15:56 +0000
@@ -107,3 +107,11 @@
107 }107 }
108 return nil108 return nil
109}109}
110
111// handle "ping" messages
112func (sess *ClientSession) handlePing() error {
113 sess.proto.SetDeadline(time.Now().Add(sess.ExchangeTimeout))
114 err := sess.proto.WriteMessage(protocol.PingPongMsg{Type: "pong"})
115 sess.Log.Debugf("Ping.")
116 return err
117}
110118
=== modified file 'client/session/session_test.go'
--- client/session/session_test.go 2014-01-31 12:44:29 +0000
+++ client/session/session_test.go 2014-01-31 14:15:56 +0000
@@ -17,14 +17,18 @@
17package session17package session
1818
19import (19import (
20 "encoding/json"
20 "errors"21 "errors"
22 "fmt"
21 "io/ioutil"23 "io/ioutil"
22 . "launchpad.net/gocheck"24 . "launchpad.net/gocheck"
23 "launchpad.net/ubuntu-push/logger"25 "launchpad.net/ubuntu-push/logger"
26 "launchpad.net/ubuntu-push/protocol"
24 helpers "launchpad.net/ubuntu-push/testing"27 helpers "launchpad.net/ubuntu-push/testing"
25 "launchpad.net/ubuntu-push/testing/condition"28 "launchpad.net/ubuntu-push/testing/condition"
26 "net"29 "net"
27 "os"30 "os"
31 "reflect"
28 "testing"32 "testing"
29 "time"33 "time"
30)34)
@@ -75,6 +79,52 @@
75func (tc *testConn) Read(buf []byte) (n int, err error) { panic("Read not implemented.") }79func (tc *testConn) Read(buf []byte) (n int, err error) { panic("Read not implemented.") }
76func (tc *testConn) Write(buf []byte) (int, error) { panic("Write not implemented.") }80func (tc *testConn) Write(buf []byte) (int, error) { panic("Write not implemented.") }
7781
82// test protocol (from session_test)
83
84type testProtocol struct {
85 up chan interface{}
86 down chan interface{}
87}
88
89// takeNext takes a value from given channel with a 5s timeout
90func takeNext(ch <-chan interface{}) interface{} {
91 select {
92 case <-time.After(5 * time.Second):
93 panic("test protocol exchange stuck: too long waiting")
94 case v := <-ch:
95 return v
96 }
97 return nil
98}
99
100func (c *testProtocol) SetDeadline(t time.Time) {
101 deadAfter := t.Sub(time.Now())
102 deadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond
103 c.down <- fmt.Sprintf("deadline %v", deadAfter)
104}
105
106func (c *testProtocol) ReadMessage(dest interface{}) error {
107 panic("ReadMessage not implemented.")
108}
109
110func (c *testProtocol) WriteMessage(src interface{}) error {
111 // make sure JSON.Marshal works with src
112 _, err := json.Marshal(src)
113 if err != nil {
114 return err
115 }
116 val := reflect.ValueOf(src)
117 if val.Kind() == reflect.Ptr {
118 src = val.Elem().Interface()
119 }
120 c.down <- src
121 switch v := takeNext(c.up).(type) {
122 case error:
123 return v
124 }
125 return nil
126}
127
78/****************************************************************128/****************************************************************
79 NewSession() tests129 NewSession() tests
80****************************************************************/130****************************************************************/
@@ -197,3 +247,46 @@
197 sess.Connection = &testConn{}247 sess.Connection = &testConn{}
198 c.Check(sess.checkRunnable(), IsNil)248 c.Check(sess.checkRunnable(), IsNil)
199}249}
250
251/****************************************************************
252 handlePing() tests
253****************************************************************/
254
255type msgSuite struct {
256 sess *ClientSession
257 upCh chan interface{}
258 downCh chan interface{}
259 errCh chan error
260}
261
262var _ = Suite(&msgSuite{})
263
264func (s *msgSuite) SetUpTest(c *C) {
265 var err error
266 s.sess, err = NewSession("", nil, 0, "wah", debuglog)
267 c.Assert(err, IsNil)
268 s.sess.Connection = &testConn{Name: "TestRun* (small r)"}
269 s.errCh = make(chan error, 1)
270 s.upCh = make(chan interface{}, 5)
271 s.downCh = make(chan interface{}, 5)
272 s.sess.proto = &testProtocol{up: s.upCh, down: s.downCh}
273}
274
275func (s *msgSuite) TestHandlePingWorks(c *C) {
276 s.upCh <- nil // no error
277 s.sess.ExchangeTimeout = time.Millisecond
278 c.Check(s.sess.handlePing(), IsNil)
279 c.Assert(len(s.downCh), Equals, 2)
280 c.Check(<-s.downCh, Equals, "deadline 1ms")
281 c.Check(<-s.downCh, Equals, protocol.PingPongMsg{Type: "pong"})
282}
283
284func (s *msgSuite) TestHandlePingHandlesPongWriteError(c *C) {
285 failure := errors.New("Pong")
286 s.upCh <- failure
287
288 c.Check(s.sess.handlePing(), Equals, failure)
289 c.Assert(len(s.downCh), Equals, 2)
290 c.Check(<-s.downCh, Equals, "deadline 0")
291 c.Check(<-s.downCh, Equals, protocol.PingPongMsg{Type: "pong"})
292}

Subscribers

People subscribed via source and target branches