| 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 session |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"encoding/json" |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"net" |
| 26 |
"reflect" |
| 27 |
stdtesting "testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
. "gopkg.in/check.v1" |
| 31 |
|
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker/testing" |
| 35 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 36 |
) |
| 37 |
|
| 38 |
func TestSession(t *stdtesting.T) { TestingT(t) } |
| 39 |
|
| 40 |
type sessionSuite struct { |
| 41 |
testlog *helpers.TestLogger |
| 42 |
} |
| 43 |
|
| 44 |
func (s *sessionSuite) SetUpTest(c *C) { |
| 45 |
s.testlog = helpers.NewTestLogger(c, "debug") |
| 46 |
} |
| 47 |
|
| 48 |
var _ = Suite(&sessionSuite{}) |
| 49 |
|
| 50 |
type testProtocol struct { |
| 51 |
up chan interface{} |
| 52 |
down chan interface{} |
| 53 |
} |
| 54 |
|
| 55 |
// takeNext takes a value from given channel with a 5s timeout |
| 56 |
func takeNext(ch <-chan interface{}) interface{} { |
| 57 |
select { |
| 58 |
case <-time.After(5 * time.Second): |
| 59 |
panic("test protocol exchange stuck: too long waiting") |
| 60 |
case v := <-ch: |
| 61 |
return v |
| 62 |
} |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
func (c *testProtocol) SetDeadline(t time.Time) { |
| 67 |
deadAfter := t.Sub(time.Now()) |
| 68 |
deadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond |
| 69 |
c.down <- fmt.Sprintf("deadline %v", deadAfter) |
| 70 |
} |
| 71 |
|
| 72 |
func (c *testProtocol) ReadMessage(dest interface{}) error { |
| 73 |
switch v := takeNext(c.up).(type) { |
| 74 |
case error: |
| 75 |
return v |
| 76 |
default: |
| 77 |
// make sure JSON.Unmarshal works with dest |
| 78 |
var marshalledMsg []byte |
| 79 |
marshalledMsg, err := json.Marshal(v) |
| 80 |
if err != nil { |
| 81 |
return fmt.Errorf("can't jsonify test value: %v", v) |
| 82 |
} |
| 83 |
return json.Unmarshal(marshalledMsg, dest) |
| 84 |
} |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
func (c *testProtocol) WriteMessage(src interface{}) error { |
| 89 |
// make sure JSON.Marshal works with src |
| 90 |
_, err := json.Marshal(src) |
| 91 |
if err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
val := reflect.ValueOf(src) |
| 95 |
if val.Kind() == reflect.Ptr { |
| 96 |
src = val.Elem().Interface() |
| 97 |
} |
| 98 |
c.down <- src |
| 99 |
switch v := takeNext(c.up).(type) { |
| 100 |
case error: |
| 101 |
return v |
| 102 |
} |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
type testSessionConfig struct { |
| 107 |
exchangeTimeout time.Duration |
| 108 |
pingInterval time.Duration |
| 109 |
} |
| 110 |
|
| 111 |
func (tsc *testSessionConfig) PingInterval() time.Duration { |
| 112 |
return tsc.pingInterval |
| 113 |
} |
| 114 |
|
| 115 |
func (tsc *testSessionConfig) ExchangeTimeout() time.Duration { |
| 116 |
return tsc.exchangeTimeout |
| 117 |
} |
| 118 |
|
| 119 |
var cfg10msPingInterval5msExchangeTout = &testSessionConfig{ |
| 120 |
pingInterval: 10 * time.Millisecond, |
| 121 |
exchangeTimeout: 5 * time.Millisecond, |
| 122 |
} |
| 123 |
|
| 124 |
type testBroker struct { |
| 125 |
registration chan interface{} |
| 126 |
err error |
| 127 |
} |
| 128 |
|
| 129 |
func newTestBroker() *testBroker { |
| 130 |
return &testBroker{registration: make(chan interface{}, 2)} |
| 131 |
} |
| 132 |
|
| 133 |
func (tb *testBroker) Register(connect *protocol.ConnectMsg, track broker.SessionTracker) (broker.BrokerSession, error) { |
| 134 |
sessionId := track.SessionId() |
| 135 |
tb.registration <- fmt.Sprintf("register %s %s", connect.DeviceId, sessionId) |
| 136 |
return &testing.TestBrokerSession{DeviceId: connect.DeviceId}, tb.err |
| 137 |
} |
| 138 |
|
| 139 |
func (tb *testBroker) Unregister(sess broker.BrokerSession) { |
| 140 |
tb.registration <- "unregister " + sess.DeviceIdentifier() |
| 141 |
} |
| 142 |
|
| 143 |
func (s *sessionSuite) TestSessionStart(c *C) { |
| 144 |
var sess broker.BrokerSession |
| 145 |
errCh := make(chan error, 1) |
| 146 |
up := make(chan interface{}, 5) |
| 147 |
down := make(chan interface{}, 5) |
| 148 |
tp := &testProtocol{up, down} |
| 149 |
brkr := newTestBroker() |
| 150 |
go func() { |
| 151 |
var err error |
| 152 |
sess, err = sessionStart(tp, brkr, cfg10msPingInterval5msExchangeTout, &tracker{sessionId: "s1"}) |
| 153 |
errCh <- err |
| 154 |
}() |
| 155 |
c.Check(takeNext(down), Equals, "deadline 5ms") |
| 156 |
up <- protocol.ConnectMsg{Type: "connect", ClientVer: "1", DeviceId: "dev-1"} |
| 157 |
c.Check(takeNext(down), Equals, protocol.ConnAckMsg{ |
| 158 |
Type: "connack", |
| 159 |
Params: protocol.ConnAckParams{(10 * time.Millisecond).String()}, |
| 160 |
}) |
| 161 |
up <- nil // no write error |
| 162 |
err := <-errCh |
| 163 |
c.Check(err, IsNil) |
| 164 |
c.Check(takeNext(brkr.registration), Equals, "register dev-1 s1") |
| 165 |
c.Check(sess.DeviceIdentifier(), Equals, "dev-1") |
| 166 |
} |
| 167 |
|
| 168 |
func (s *sessionSuite) TestSessionRegisterError(c *C) { |
| 169 |
errCh := make(chan error, 1) |
| 170 |
up := make(chan interface{}, 5) |
| 171 |
down := make(chan interface{}, 5) |
| 172 |
tp := &testProtocol{up, down} |
| 173 |
brkr := newTestBroker() |
| 174 |
errRegister := errors.New("register failure") |
| 175 |
brkr.err = errRegister |
| 176 |
go func() { |
| 177 |
var err error |
| 178 |
_, err = sessionStart(tp, brkr, cfg10msPingInterval5msExchangeTout, &tracker{sessionId: "s2"}) |
| 179 |
errCh <- err |
| 180 |
}() |
| 181 |
up <- protocol.ConnectMsg{Type: "connect", ClientVer: "1", DeviceId: "dev-1"} |
| 182 |
takeNext(down) // CONNACK |
| 183 |
up <- nil // no write error |
| 184 |
err := <-errCh |
| 185 |
c.Check(err, Equals, errRegister) |
| 186 |
} |
| 187 |
|
| 188 |
func (s *sessionSuite) TestSessionStartReadError(c *C) { |
| 189 |
up := make(chan interface{}, 5) |
| 190 |
down := make(chan interface{}, 5) |
| 191 |
tp := &testProtocol{up, down} |
| 192 |
up <- io.ErrUnexpectedEOF |
| 193 |
_, err := sessionStart(tp, nil, cfg10msPingInterval5msExchangeTout, &tracker{sessionId: "s3"}) |
| 194 |
c.Check(err, Equals, io.ErrUnexpectedEOF) |
| 195 |
} |
| 196 |
|
| 197 |
func (s *sessionSuite) TestSessionStartWriteError(c *C) { |
| 198 |
up := make(chan interface{}, 5) |
| 199 |
down := make(chan interface{}, 5) |
| 200 |
tp := &testProtocol{up, down} |
| 201 |
up <- protocol.ConnectMsg{Type: "connect"} |
| 202 |
up <- io.ErrUnexpectedEOF |
| 203 |
_, err := sessionStart(tp, nil, cfg10msPingInterval5msExchangeTout, &tracker{sessionId: "s4"}) |
| 204 |
c.Check(err, Equals, io.ErrUnexpectedEOF) |
| 205 |
// sanity |
| 206 |
c.Check(takeNext(down), Matches, "deadline.*") |
| 207 |
c.Check(takeNext(down), FitsTypeOf, protocol.ConnAckMsg{}) |
| 208 |
} |
| 209 |
|
| 210 |
func (s *sessionSuite) TestSessionStartMismatch(c *C) { |
| 211 |
up := make(chan interface{}, 5) |
| 212 |
down := make(chan interface{}, 5) |
| 213 |
tp := &testProtocol{up, down} |
| 214 |
up <- protocol.ConnectMsg{Type: "what"} |
| 215 |
_, err := sessionStart(tp, nil, cfg10msPingInterval5msExchangeTout, &tracker{sessionId: "s5"}) |
| 216 |
c.Check(err, DeepEquals, &broker.ErrAbort{"expected CONNECT message"}) |
| 217 |
} |
| 218 |
|
| 219 |
func (s *sessionSuite) TestSessionLoop(c *C) { |
| 220 |
track := &testTracker{NewTracker(s.testlog), make(chan interface{}, 2)} |
| 221 |
errCh := make(chan error, 1) |
| 222 |
up := make(chan interface{}, 5) |
| 223 |
down := make(chan interface{}, 5) |
| 224 |
tp := &testProtocol{up, down} |
| 225 |
sess := &testing.TestBrokerSession{} |
| 226 |
go func() { |
| 227 |
errCh <- sessionLoop(tp, sess, cfg10msPingInterval5msExchangeTout, track) |
| 228 |
}() |
| 229 |
c.Check(takeNext(down), Equals, "deadline 5ms") |
| 230 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 231 |
up <- nil // no write error |
| 232 |
up <- protocol.PingPongMsg{Type: "pong"} |
| 233 |
c.Check(takeNext(down), Equals, "deadline 5ms") |
| 234 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 235 |
up <- nil // no write error |
| 236 |
up <- io.ErrUnexpectedEOF |
| 237 |
err := <-errCh |
| 238 |
c.Check(err, Equals, io.ErrUnexpectedEOF) |
| 239 |
c.Check(track.interval, HasLen, 2) |
| 240 |
|
| 241 |
// TODO: Fix racyness. See lp:1522880 |
| 242 |
// c.Check((<-track.interval).(time.Duration) <= 16*time.Millisecond, Equals, true) |
| 243 |
// c.Check((<-track.interval).(time.Duration) <= 16*time.Millisecond, Equals, true) |
| 244 |
} |
| 245 |
|
| 246 |
var cfg5msPingInterval2msExchangeTout = &testSessionConfig{ |
| 247 |
pingInterval: 5 * time.Millisecond, |
| 248 |
exchangeTimeout: 2 * time.Millisecond, |
| 249 |
} |
| 250 |
|
| 251 |
func (s *sessionSuite) TestSessionLoopWriteError(c *C) { |
| 252 |
nopTrack := NewTracker(s.testlog) |
| 253 |
errCh := make(chan error, 1) |
| 254 |
up := make(chan interface{}, 5) |
| 255 |
down := make(chan interface{}, 5) |
| 256 |
tp := &testProtocol{up, down} |
| 257 |
sess := &testing.TestBrokerSession{} |
| 258 |
go func() { |
| 259 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 260 |
}() |
| 261 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 262 |
c.Check(takeNext(down), FitsTypeOf, protocol.PingPongMsg{}) |
| 263 |
up <- io.ErrUnexpectedEOF // write error |
| 264 |
err := <-errCh |
| 265 |
c.Check(err, Equals, io.ErrUnexpectedEOF) |
| 266 |
} |
| 267 |
|
| 268 |
func (s *sessionSuite) TestSessionLoopMismatch(c *C) { |
| 269 |
nopTrack := NewTracker(s.testlog) |
| 270 |
errCh := make(chan error, 1) |
| 271 |
up := make(chan interface{}, 5) |
| 272 |
down := make(chan interface{}, 5) |
| 273 |
tp := &testProtocol{up, down} |
| 274 |
sess := &testing.TestBrokerSession{} |
| 275 |
go func() { |
| 276 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 277 |
}() |
| 278 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 279 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 280 |
up <- nil // no write error |
| 281 |
up <- protocol.PingPongMsg{Type: "what"} |
| 282 |
err := <-errCh |
| 283 |
c.Check(err, DeepEquals, &broker.ErrAbort{"expected PONG message"}) |
| 284 |
} |
| 285 |
|
| 286 |
type testMsg struct { |
| 287 |
Type string `json:"T"` |
| 288 |
Part int `json:"P"` |
| 289 |
nParts int |
| 290 |
} |
| 291 |
|
| 292 |
func (m *testMsg) Split() bool { |
| 293 |
if m.nParts == 0 { |
| 294 |
return true |
| 295 |
} |
| 296 |
m.Part++ |
| 297 |
if m.Part == m.nParts { |
| 298 |
return true |
| 299 |
} |
| 300 |
return false |
| 301 |
} |
| 302 |
|
| 303 |
type testExchange struct { |
| 304 |
inMsg testMsg |
| 305 |
prepErr error |
| 306 |
finErr error |
| 307 |
finSleep time.Duration |
| 308 |
nParts int |
| 309 |
done chan interface{} |
| 310 |
} |
| 311 |
|
| 312 |
func (exchg *testExchange) Prepare(sess broker.BrokerSession) (outMsg protocol.SplittableMsg, inMsg interface{}, err error) { |
| 313 |
return &testMsg{Type: "msg", nParts: exchg.nParts}, &exchg.inMsg, exchg.prepErr |
| 314 |
} |
| 315 |
|
| 316 |
func (exchg *testExchange) Acked(sess broker.BrokerSession, done bool) error { |
| 317 |
time.Sleep(exchg.finSleep) |
| 318 |
if exchg.done != nil { |
| 319 |
var doneStr string |
| 320 |
if done { |
| 321 |
doneStr = "y" |
| 322 |
} else { |
| 323 |
doneStr = "n" |
| 324 |
} |
| 325 |
exchg.done <- doneStr |
| 326 |
} |
| 327 |
return exchg.finErr |
| 328 |
} |
| 329 |
|
| 330 |
func (s *sessionSuite) TestSessionLoopExchange(c *C) { |
| 331 |
nopTrack := NewTracker(s.testlog) |
| 332 |
errCh := make(chan error, 1) |
| 333 |
up := make(chan interface{}, 5) |
| 334 |
down := make(chan interface{}, 5) |
| 335 |
tp := &testProtocol{up, down} |
| 336 |
exchanges := make(chan broker.Exchange, 1) |
| 337 |
exchanges <- &testExchange{} |
| 338 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 339 |
go func() { |
| 340 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 341 |
}() |
| 342 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 343 |
c.Check(takeNext(down), DeepEquals, testMsg{Type: "msg"}) |
| 344 |
up <- nil // no write error |
| 345 |
up <- testMsg{Type: "ack"} |
| 346 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 347 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 348 |
up <- nil // no write error |
| 349 |
up <- io.EOF |
| 350 |
err := <-errCh |
| 351 |
c.Check(err, Equals, io.EOF) |
| 352 |
} |
| 353 |
|
| 354 |
func (s *sessionSuite) TestSessionLoopKick(c *C) { |
| 355 |
nopTrack := NewTracker(s.testlog) |
| 356 |
errCh := make(chan error, 1) |
| 357 |
up := make(chan interface{}, 5) |
| 358 |
down := make(chan interface{}, 5) |
| 359 |
tp := &testProtocol{up, down} |
| 360 |
exchanges := make(chan broker.Exchange, 1) |
| 361 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 362 |
go func() { |
| 363 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 364 |
}() |
| 365 |
exchanges <- nil |
| 366 |
err := <-errCh |
| 367 |
c.Check(err, DeepEquals, &broker.ErrAbort{"terminated"}) |
| 368 |
} |
| 369 |
|
| 370 |
func (s *sessionSuite) TestPingTimerReset(c *C) { |
| 371 |
pingInterval := 50 * time.Millisecond |
| 372 |
now := time.Now() |
| 373 |
l := &loop{ |
| 374 |
pingInterval: pingInterval, |
| 375 |
pingTimer: time.NewTimer(pingInterval), |
| 376 |
intervalStart: now, |
| 377 |
} |
| 378 |
time.Sleep(10 * time.Millisecond) |
| 379 |
l.pingTimer.Stop() |
| 380 |
done := l.pingTimerReset(true) |
| 381 |
c.Assert(done, Equals, true) |
| 382 |
select { |
| 383 |
case <-l.pingTimer.C: |
| 384 |
case <-time.After(1 * time.Second): |
| 385 |
c.Fatal("timer should have triggered") |
| 386 |
} |
| 387 |
c.Check(now, Not(Equals), l.intervalStart) |
| 388 |
} |
| 389 |
|
| 390 |
func (s *sessionSuite) TestPingTimerResetPartial(c *C) { |
| 391 |
pingInterval := 5 * time.Second |
| 392 |
now := time.Now().Add(-pingInterval + 50*time.Millisecond) |
| 393 |
l := &loop{ |
| 394 |
pingInterval: pingInterval, |
| 395 |
pingTimer: time.NewTimer(pingInterval), |
| 396 |
intervalStart: now, |
| 397 |
} |
| 398 |
l.pingTimer.Stop() |
| 399 |
done := l.pingTimerReset(false) |
| 400 |
c.Assert(done, Equals, true) |
| 401 |
select { |
| 402 |
case <-l.pingTimer.C: |
| 403 |
case <-time.After(1 * time.Second): |
| 404 |
c.Fatal("timer should have triggered") |
| 405 |
} |
| 406 |
c.Check(now, Equals, l.intervalStart) |
| 407 |
} |
| 408 |
|
| 409 |
func (s *sessionSuite) TestPingTimerResetPartialTooLate(c *C) { |
| 410 |
pingInterval := 5 * time.Second |
| 411 |
now := time.Now().Add(-pingInterval - 50*time.Millisecond) |
| 412 |
l := &loop{ |
| 413 |
pingInterval: pingInterval, |
| 414 |
pingTimer: time.NewTimer(pingInterval), |
| 415 |
intervalStart: now, |
| 416 |
} |
| 417 |
l.pingTimer.Stop() |
| 418 |
done := l.pingTimerReset(false) |
| 419 |
c.Assert(done, Equals, false) |
| 420 |
} |
| 421 |
|
| 422 |
func (s *sessionSuite) TestSessionLoopExchangeErrNop(c *C) { |
| 423 |
nopTrack := NewTracker(s.testlog) |
| 424 |
errCh := make(chan error, 1) |
| 425 |
up := make(chan interface{}, 5) |
| 426 |
down := make(chan interface{}, 5) |
| 427 |
tp := &testProtocol{up, down} |
| 428 |
exchanges := make(chan broker.Exchange, 1) |
| 429 |
exchanges <- &testExchange{prepErr: broker.ErrNop} |
| 430 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 431 |
pingInterval := 5 * time.Second |
| 432 |
go func() { |
| 433 |
l := &loop{ |
| 434 |
proto: tp, |
| 435 |
sess: sess, |
| 436 |
track: nopTrack, |
| 437 |
pingInterval: pingInterval, |
| 438 |
pingTimer: time.NewTimer(pingInterval), |
| 439 |
exchangeTimeout: 1 * time.Second, |
| 440 |
intervalStart: time.Now().Add(-pingInterval + 50*time.Millisecond), |
| 441 |
} |
| 442 |
errCh <- l.run() |
| 443 |
}() |
| 444 |
c.Check(takeNext(down), Equals, "deadline 1s") |
| 445 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 446 |
up <- nil // no write error |
| 447 |
up <- io.EOF |
| 448 |
err := <-errCh |
| 449 |
c.Check(err, Equals, io.EOF) |
| 450 |
} |
| 451 |
|
| 452 |
func (s *sessionSuite) TestSessionLoopExchangeErrNopNeedPing(c *C) { |
| 453 |
nopTrack := NewTracker(s.testlog) |
| 454 |
errCh := make(chan error, 1) |
| 455 |
up := make(chan interface{}, 5) |
| 456 |
down := make(chan interface{}, 5) |
| 457 |
tp := &testProtocol{up, down} |
| 458 |
exchanges := make(chan broker.Exchange, 1) |
| 459 |
exchanges <- &testExchange{prepErr: broker.ErrNop} |
| 460 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 461 |
pingInterval := 5 * time.Second |
| 462 |
go func() { |
| 463 |
l := &loop{ |
| 464 |
proto: tp, |
| 465 |
sess: sess, |
| 466 |
track: nopTrack, |
| 467 |
pingInterval: pingInterval, |
| 468 |
pingTimer: time.NewTimer(pingInterval), |
| 469 |
exchangeTimeout: 1 * time.Second, |
| 470 |
intervalStart: time.Now().Add(-pingInterval - 50*time.Millisecond), |
| 471 |
} |
| 472 |
errCh <- l.run() |
| 473 |
}() |
| 474 |
c.Check(takeNext(down), Equals, "deadline 1s") |
| 475 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 476 |
up <- nil // no write error |
| 477 |
up <- io.EOF |
| 478 |
err := <-errCh |
| 479 |
c.Check(err, Equals, io.EOF) |
| 480 |
} |
| 481 |
|
| 482 |
func (s *sessionSuite) TestSessionLoopExchangeSplit(c *C) { |
| 483 |
nopTrack := NewTracker(s.testlog) |
| 484 |
errCh := make(chan error, 1) |
| 485 |
up := make(chan interface{}, 5) |
| 486 |
down := make(chan interface{}, 5) |
| 487 |
tp := &testProtocol{up, down} |
| 488 |
exchanges := make(chan broker.Exchange, 1) |
| 489 |
exchange := &testExchange{nParts: 2, done: make(chan interface{}, 2)} |
| 490 |
exchanges <- exchange |
| 491 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 492 |
go func() { |
| 493 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 494 |
}() |
| 495 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 496 |
c.Check(takeNext(down), DeepEquals, testMsg{Type: "msg", Part: 1, nParts: 2}) |
| 497 |
up <- nil // no write error |
| 498 |
up <- testMsg{Type: "ack"} |
| 499 |
c.Check(takeNext(exchange.done), Equals, "n") |
| 500 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 501 |
c.Check(takeNext(down), DeepEquals, testMsg{Type: "msg", Part: 2, nParts: 2}) |
| 502 |
up <- nil // no write error |
| 503 |
up <- testMsg{Type: "ack"} |
| 504 |
c.Check(takeNext(exchange.done), Equals, "y") |
| 505 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 506 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 507 |
up <- nil // no write error |
| 508 |
up <- io.EOF |
| 509 |
err := <-errCh |
| 510 |
c.Check(err, Equals, io.EOF) |
| 511 |
} |
| 512 |
|
| 513 |
func (s *sessionSuite) TestSessionLoopExchangePrepareError(c *C) { |
| 514 |
nopTrack := NewTracker(s.testlog) |
| 515 |
errCh := make(chan error, 1) |
| 516 |
up := make(chan interface{}, 5) |
| 517 |
down := make(chan interface{}, 5) |
| 518 |
tp := &testProtocol{up, down} |
| 519 |
exchanges := make(chan broker.Exchange, 1) |
| 520 |
prepErr := errors.New("prepare failure") |
| 521 |
exchanges <- &testExchange{prepErr: prepErr} |
| 522 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 523 |
go func() { |
| 524 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 525 |
}() |
| 526 |
err := <-errCh |
| 527 |
c.Check(err, Equals, prepErr) |
| 528 |
} |
| 529 |
|
| 530 |
func (s *sessionSuite) TestSessionLoopExchangeAckedError(c *C) { |
| 531 |
nopTrack := NewTracker(s.testlog) |
| 532 |
errCh := make(chan error, 1) |
| 533 |
up := make(chan interface{}, 5) |
| 534 |
down := make(chan interface{}, 5) |
| 535 |
tp := &testProtocol{up, down} |
| 536 |
exchanges := make(chan broker.Exchange, 1) |
| 537 |
finErr := errors.New("finish error") |
| 538 |
exchanges <- &testExchange{finErr: finErr} |
| 539 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 540 |
go func() { |
| 541 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 542 |
}() |
| 543 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 544 |
c.Check(takeNext(down), DeepEquals, testMsg{Type: "msg"}) |
| 545 |
up <- nil // no write error |
| 546 |
up <- testMsg{Type: "ack"} |
| 547 |
err := <-errCh |
| 548 |
c.Check(err, Equals, finErr) |
| 549 |
} |
| 550 |
|
| 551 |
func (s *sessionSuite) TestSessionLoopExchangeWriteError(c *C) { |
| 552 |
nopTrack := NewTracker(s.testlog) |
| 553 |
errCh := make(chan error, 1) |
| 554 |
up := make(chan interface{}, 5) |
| 555 |
down := make(chan interface{}, 5) |
| 556 |
tp := &testProtocol{up, down} |
| 557 |
exchanges := make(chan broker.Exchange, 1) |
| 558 |
exchanges <- &testExchange{} |
| 559 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 560 |
go func() { |
| 561 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 562 |
}() |
| 563 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 564 |
c.Check(takeNext(down), FitsTypeOf, testMsg{}) |
| 565 |
up <- io.ErrUnexpectedEOF |
| 566 |
err := <-errCh |
| 567 |
c.Check(err, Equals, io.ErrUnexpectedEOF) |
| 568 |
} |
| 569 |
|
| 570 |
func (s *sessionSuite) TestSessionLoopConnBrokenExchange(c *C) { |
| 571 |
nopTrack := NewTracker(s.testlog) |
| 572 |
errCh := make(chan error, 1) |
| 573 |
up := make(chan interface{}, 5) |
| 574 |
down := make(chan interface{}, 5) |
| 575 |
tp := &testProtocol{up, down} |
| 576 |
exchanges := make(chan broker.Exchange, 1) |
| 577 |
msg := &protocol.ConnBrokenMsg{"connbroken", "BREASON"} |
| 578 |
exchanges <- &broker.ConnMetaExchange{msg} |
| 579 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 580 |
go func() { |
| 581 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 582 |
}() |
| 583 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 584 |
c.Check(takeNext(down), DeepEquals, protocol.ConnBrokenMsg{"connbroken", "BREASON"}) |
| 585 |
up <- nil // no write error |
| 586 |
err := <-errCh |
| 587 |
c.Check(err, DeepEquals, &broker.ErrAbort{"session broken for reason"}) |
| 588 |
} |
| 589 |
|
| 590 |
func (s *sessionSuite) TestSessionLoopConnWarnExchange(c *C) { |
| 591 |
nopTrack := NewTracker(s.testlog) |
| 592 |
errCh := make(chan error, 1) |
| 593 |
up := make(chan interface{}, 5) |
| 594 |
down := make(chan interface{}, 5) |
| 595 |
tp := &testProtocol{up, down} |
| 596 |
exchanges := make(chan broker.Exchange, 1) |
| 597 |
msg := &protocol.ConnWarnMsg{"connwarn", "WREASON"} |
| 598 |
exchanges <- &broker.ConnMetaExchange{msg} |
| 599 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 600 |
go func() { |
| 601 |
errCh <- sessionLoop(tp, sess, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 602 |
}() |
| 603 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 604 |
c.Check(takeNext(down), DeepEquals, protocol.ConnWarnMsg{"connwarn", "WREASON"}) |
| 605 |
up <- nil // no write error |
| 606 |
// session continues |
| 607 |
c.Check(takeNext(down), Equals, "deadline 2ms") |
| 608 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 609 |
up <- nil // no write error |
| 610 |
up <- io.EOF |
| 611 |
err := <-errCh |
| 612 |
c.Check(err, Equals, io.EOF) |
| 613 |
} |
| 614 |
|
| 615 |
type testTracker struct { |
| 616 |
SessionTracker |
| 617 |
interval chan interface{} |
| 618 |
} |
| 619 |
|
| 620 |
func (trk *testTracker) EffectivePingInterval(interval time.Duration) { |
| 621 |
trk.interval <- interval |
| 622 |
} |
| 623 |
|
| 624 |
var cfg50msPingInterval = &testSessionConfig{ |
| 625 |
pingInterval: 50 * time.Millisecond, |
| 626 |
exchangeTimeout: 10 * time.Millisecond, |
| 627 |
} |
| 628 |
|
| 629 |
func (s *sessionSuite) TestSessionLoopExchangeNextPing(c *C) { |
| 630 |
track := &testTracker{NewTracker(s.testlog), make(chan interface{}, 1)} |
| 631 |
errCh := make(chan error, 1) |
| 632 |
up := make(chan interface{}, 5) |
| 633 |
down := make(chan interface{}, 5) |
| 634 |
tp := &testProtocol{up, down} |
| 635 |
exchanges := make(chan broker.Exchange, 1) |
| 636 |
exchanges <- &testExchange{finSleep: 15 * time.Millisecond} |
| 637 |
sess := &testing.TestBrokerSession{Exchanges: exchanges} |
| 638 |
go func() { |
| 639 |
errCh <- sessionLoop(tp, sess, cfg50msPingInterval, track) |
| 640 |
}() |
| 641 |
c.Check(takeNext(down), Equals, "deadline 10ms") |
| 642 |
c.Check(takeNext(down), DeepEquals, testMsg{Type: "msg"}) |
| 643 |
up <- nil // no write error |
| 644 |
up <- testMsg{Type: "ack"} |
| 645 |
// next ping interval starts around here |
| 646 |
interval := takeNext(track.interval).(time.Duration) |
| 647 |
c.Check(takeNext(down), Equals, "deadline 10ms") |
| 648 |
c.Check(takeNext(down), DeepEquals, protocol.PingPongMsg{Type: "ping"}) |
| 649 |
effectiveOfPing := float64(interval) / float64(50*time.Millisecond) |
| 650 |
comment := Commentf("effectiveOfPing=%f", effectiveOfPing) |
| 651 |
c.Check(effectiveOfPing > 0.95, Equals, true, comment) |
| 652 |
c.Check(effectiveOfPing < 1.19, Equals, true, comment) |
| 653 |
up <- nil // no write error |
| 654 |
up <- io.EOF |
| 655 |
err := <-errCh |
| 656 |
c.Check(err, Equals, io.EOF) |
| 657 |
} |
| 658 |
|
| 659 |
func serverClientWire() (srv net.Conn, cli net.Conn, lst net.Listener) { |
| 660 |
lst, err := net.Listen("tcp", "127.0.0.1:0") |
| 661 |
if err != nil { |
| 662 |
panic(err) |
| 663 |
} |
| 664 |
cli, err = net.DialTCP("tcp", nil, lst.Addr().(*net.TCPAddr)) |
| 665 |
if err != nil { |
| 666 |
panic(err) |
| 667 |
} |
| 668 |
srv, err = lst.Accept() |
| 669 |
if err != nil { |
| 670 |
panic(err) |
| 671 |
} |
| 672 |
return |
| 673 |
} |
| 674 |
|
| 675 |
type rememberDeadlineConn struct { |
| 676 |
net.Conn |
| 677 |
deadlineKind []string |
| 678 |
} |
| 679 |
|
| 680 |
func (c *rememberDeadlineConn) SetDeadline(t time.Time) error { |
| 681 |
c.deadlineKind = append(c.deadlineKind, "both") |
| 682 |
return c.Conn.SetDeadline(t) |
| 683 |
} |
| 684 |
|
| 685 |
func (c *rememberDeadlineConn) SetReadDeadline(t time.Time) error { |
| 686 |
c.deadlineKind = append(c.deadlineKind, "read") |
| 687 |
return c.Conn.SetDeadline(t) |
| 688 |
} |
| 689 |
|
| 690 |
func (c *rememberDeadlineConn) SetWriteDeadline(t time.Time) error { |
| 691 |
c.deadlineKind = append(c.deadlineKind, "write") |
| 692 |
return c.Conn.SetDeadline(t) |
| 693 |
} |
| 694 |
func (s *sessionSuite) TestSessionWire(c *C) { |
| 695 |
track := NewTracker(s.testlog) |
| 696 |
errCh := make(chan error, 1) |
| 697 |
srv, cli, lst := serverClientWire() |
| 698 |
defer lst.Close() |
| 699 |
remSrv := &rememberDeadlineConn{srv, make([]string, 0, 2)} |
| 700 |
brkr := newTestBroker() |
| 701 |
go func() { |
| 702 |
errCh <- Session(remSrv, brkr, cfg50msPingInterval, track) |
| 703 |
}() |
| 704 |
io.WriteString(cli, "\x00") |
| 705 |
io.WriteString(cli, "\x00\x20{\"T\":\"connect\",\"DeviceId\":\"DEV\"}") |
| 706 |
// connack |
| 707 |
downStream := bufio.NewReader(cli) |
| 708 |
msg, err := downStream.ReadBytes(byte('}')) |
| 709 |
c.Check(err, IsNil) |
| 710 |
c.Check(msg, DeepEquals, []byte("\x00\x30{\"T\":\"connack\",\"Params\":{\"PingInterval\":\"50ms\"}")) |
| 711 |
// eat the last } |
| 712 |
rbr, err := downStream.ReadByte() |
| 713 |
c.Check(err, IsNil) |
| 714 |
c.Check(rbr, Equals, byte('}')) |
| 715 |
// first ping |
| 716 |
msg, err = downStream.ReadBytes(byte('}')) |
| 717 |
c.Check(err, IsNil) |
| 718 |
c.Check(msg, DeepEquals, []byte("\x00\x0c{\"T\":\"ping\"}")) |
| 719 |
c.Check(takeNext(brkr.registration), Equals, "register DEV "+track.SessionId()) |
| 720 |
c.Check(len(brkr.registration), Equals, 0) // not yet unregistered |
| 721 |
cli.Close() |
| 722 |
err = <-errCh |
| 723 |
c.Check(remSrv.deadlineKind, DeepEquals, []string{"read", "both", "both"}) |
| 724 |
c.Check(err, Equals, io.EOF) |
| 725 |
c.Check(takeNext(brkr.registration), Equals, "unregister DEV") |
| 726 |
// tracking |
| 727 |
c.Check(s.testlog.Captured(), Matches, `.*connected.*\n.*registered DEV.*\n.*ended with: EOF\n`) |
| 728 |
} |
| 729 |
|
| 730 |
func (s *sessionSuite) TestSessionWireTimeout(c *C) { |
| 731 |
nopTrack := NewTracker(s.testlog) |
| 732 |
errCh := make(chan error, 1) |
| 733 |
srv, cli, lst := serverClientWire() |
| 734 |
defer lst.Close() |
| 735 |
remSrv := &rememberDeadlineConn{srv, make([]string, 0, 2)} |
| 736 |
brkr := newTestBroker() |
| 737 |
go func() { |
| 738 |
errCh <- Session(remSrv, brkr, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 739 |
}() |
| 740 |
err := <-errCh |
| 741 |
c.Check(err, FitsTypeOf, &net.OpError{}) |
| 742 |
c.Check(remSrv.deadlineKind, DeepEquals, []string{"read"}) |
| 743 |
cli.Close() |
| 744 |
} |
| 745 |
|
| 746 |
func (s *sessionSuite) TestSessionWireWrongVersion(c *C) { |
| 747 |
track := NewTracker(s.testlog) |
| 748 |
errCh := make(chan error, 1) |
| 749 |
srv, cli, lst := serverClientWire() |
| 750 |
defer lst.Close() |
| 751 |
brkr := newTestBroker() |
| 752 |
go func() { |
| 753 |
errCh <- Session(srv, brkr, cfg50msPingInterval, track) |
| 754 |
}() |
| 755 |
io.WriteString(cli, "\x10") |
| 756 |
err := <-errCh |
| 757 |
c.Check(err, DeepEquals, &broker.ErrAbort{"unexpected wire format version"}) |
| 758 |
cli.Close() |
| 759 |
// tracking |
| 760 |
c.Check(s.testlog.Captured(), Matches, `.*connected.*\n.*ended with: session aborted \(unexpected.*version\)\n`) |
| 761 |
|
| 762 |
} |
| 763 |
|
| 764 |
func (s *sessionSuite) TestSessionWireEarlyClose(c *C) { |
| 765 |
track := NewTracker(s.testlog) |
| 766 |
errCh := make(chan error, 1) |
| 767 |
srv, cli, lst := serverClientWire() |
| 768 |
defer lst.Close() |
| 769 |
brkr := newTestBroker() |
| 770 |
go func() { |
| 771 |
errCh <- Session(srv, brkr, cfg50msPingInterval, track) |
| 772 |
}() |
| 773 |
cli.Close() |
| 774 |
err := <-errCh |
| 775 |
c.Check(err, Equals, io.EOF) |
| 776 |
// tracking |
| 777 |
c.Check(s.testlog.Captured(), Matches, `.*connected.*\n.*ended with: EOF\n`) |
| 778 |
|
| 779 |
} |
| 780 |
|
| 781 |
func (s *sessionSuite) TestSessionWireEarlyClose2(c *C) { |
| 782 |
track := NewTracker(s.testlog) |
| 783 |
errCh := make(chan error, 1) |
| 784 |
srv, cli, lst := serverClientWire() |
| 785 |
defer lst.Close() |
| 786 |
brkr := newTestBroker() |
| 787 |
go func() { |
| 788 |
errCh <- Session(srv, brkr, cfg50msPingInterval, track) |
| 789 |
}() |
| 790 |
io.WriteString(cli, "\x00") |
| 791 |
io.WriteString(cli, "\x00") |
| 792 |
cli.Close() |
| 793 |
err := <-errCh |
| 794 |
c.Check(err, Equals, io.EOF) |
| 795 |
// tracking |
| 796 |
c.Check(s.testlog.Captured(), Matches, `.*connected.*\n.*ended with: EOF\n`) |
| 797 |
} |
| 798 |
|
| 799 |
func (s *sessionSuite) TestSessionWireTimeout2(c *C) { |
| 800 |
nopTrack := NewTracker(s.testlog) |
| 801 |
errCh := make(chan error, 1) |
| 802 |
srv, cli, lst := serverClientWire() |
| 803 |
defer lst.Close() |
| 804 |
remSrv := &rememberDeadlineConn{srv, make([]string, 0, 2)} |
| 805 |
brkr := newTestBroker() |
| 806 |
go func() { |
| 807 |
errCh <- Session(remSrv, brkr, cfg5msPingInterval2msExchangeTout, nopTrack) |
| 808 |
}() |
| 809 |
io.WriteString(cli, "\x00") |
| 810 |
io.WriteString(cli, "\x00") |
| 811 |
err := <-errCh |
| 812 |
c.Check(err, FitsTypeOf, &net.OpError{}) |
| 813 |
c.Check(remSrv.deadlineKind, DeepEquals, []string{"read", "both"}) |
| 814 |
cli.Close() |
| 815 |
} |