Debian Codegraph @

server/acceptance/suites/pingpong.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 suites
18
19 import (
20 "runtime"
21 "strings"
22 "time"
23
24 . "gopkg.in/check.v1"
25 )
26
27 // PingPongAcceptanceSuite has tests about connectivity and ping-pong requests.
28 type PingPongAcceptanceSuite struct {
29 AcceptanceSuite
30 }
31
32 // Tests about connection, ping-pong, disconnection scenarios
33
34 func (s *PingPongAcceptanceSuite) TestConnectPingPing(c *C) {
35 errCh := make(chan error, 1)
36 events := make(chan string, 10)
37 sess := testClientSession(s.ServerAddr, "DEVA", "m1", "img1", true)
38 err := sess.Dial()
39 c.Assert(err, IsNil)
40 intercept := func(ic *interceptingConn, op string, b []byte) (bool, int, error) {
41 // would be 3rd ping read, based on logged traffic
42 if op == "read" && ic.totalRead >= 79 {
43 // exit the sess.Run() goroutine, client will close
44 runtime.Goexit()
45 }
46 return false, 0, nil
47 }
48 sess.Connection = &interceptingConn{sess.Connection, 0, 0, intercept}
49 go func() {
50 errCh <- sess.Run(events)
51 }()
52 connectCli := NextEvent(events, errCh)
53 connectSrv := NextEvent(s.ServerEvents, nil)
54 registeredSrv := NextEvent(s.ServerEvents, nil)
55 tconnect := time.Now()
56 c.Assert(connectSrv, Matches, ".*session.* connected .*")
57 c.Assert(registeredSrv, Matches, ".*session.* registered DEVA")
58 c.Assert(strings.HasSuffix(connectSrv, connectCli), Equals, true)
59 c.Assert(NextEvent(events, errCh), Equals, "ping")
60 elapsedOfPing := float64(time.Since(tconnect)) / float64(500*time.Millisecond)
61 c.Check(elapsedOfPing >= 1.0, Equals, true)
62 c.Check(elapsedOfPing < 1.05, Equals, true)
63 c.Assert(NextEvent(events, errCh), Equals, "ping")
64 c.Assert(NextEvent(s.ServerEvents, nil), Matches, ".*session.* ended with: EOF")
65 c.Check(len(errCh), Equals, 0)
66 }
67
68 func (s *PingPongAcceptanceSuite) TestConnectPingNeverPong(c *C) {
69 errCh := make(chan error, 1)
70 events := make(chan string, 10)
71 sess := testClientSession(s.ServerAddr, "DEVB", "m1", "img1", true)
72 err := sess.Dial()
73 c.Assert(err, IsNil)
74 intercept := func(ic *interceptingConn, op string, b []byte) (bool, int, error) {
75 // would be pong to 2nd ping, based on logged traffic
76 if op == "write" && ic.totalRead >= 67 {
77 time.Sleep(200 * time.Millisecond)
78 // exit the sess.Run() goroutine, client will close
79 runtime.Goexit()
80 }
81 return false, 0, nil
82 }
83 sess.Connection = &interceptingConn{sess.Connection, 0, 0, intercept}
84 go func() {
85 errCh <- sess.Run(events)
86 }()
87 c.Assert(NextEvent(events, errCh), Matches, "connected .*")
88 c.Assert(NextEvent(s.ServerEvents, nil), Matches, ".*session.* connected .*")
89 c.Assert(NextEvent(s.ServerEvents, nil), Matches, ".*session.* registered .*")
90 c.Assert(NextEvent(events, errCh), Equals, "ping")
91 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*timeout`)
92 c.Check(len(errCh), Equals, 0)
93 }