| 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 |
"crypto/tls" |
| 21 |
"encoding/json" |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"io/ioutil" |
| 26 |
"net" |
| 27 |
"net/http" |
| 28 |
"net/http/httptest" |
| 29 |
"reflect" |
| 30 |
"testing" |
| 31 |
"time" |
| 32 |
|
| 33 |
. "gopkg.in/check.v1" |
| 34 |
|
| 35 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 36 |
"gitlab.com/ubports/development/core/lomiri-push-service/client/gethosts" |
| 37 |
"gitlab.com/ubports/development/core/lomiri-push-service/client/session/seenstate" |
| 38 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 39 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 40 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 41 |
"gitlab.com/ubports/development/core/lomiri-push-service/util" |
| 42 |
) |
| 43 |
|
| 44 |
func TestSession(t *testing.T) { TestingT(t) } |
| 45 |
|
| 46 |
// |
| 47 |
// helpers! candidates to live in their own ../testing/ package. |
| 48 |
// |
| 49 |
|
| 50 |
type xAddr string |
| 51 |
|
| 52 |
func (x xAddr) Network() string { return "<:>" } |
| 53 |
func (x xAddr) String() string { return string(x) } |
| 54 |
|
| 55 |
// testConn (roughly based on the one in protocol_test) |
| 56 |
|
| 57 |
type testConn struct { |
| 58 |
Name string |
| 59 |
Deadlines []time.Duration |
| 60 |
Writes [][]byte |
| 61 |
WriteCondition condition.Interface |
| 62 |
DeadlineCondition condition.Interface |
| 63 |
CloseCondition condition.Interface |
| 64 |
} |
| 65 |
|
| 66 |
func (tc *testConn) LocalAddr() net.Addr { return xAddr(tc.Name) } |
| 67 |
|
| 68 |
func (tc *testConn) RemoteAddr() net.Addr { return xAddr(tc.Name) } |
| 69 |
|
| 70 |
func (tc *testConn) Close() error { |
| 71 |
if tc.CloseCondition == nil || tc.CloseCondition.OK() { |
| 72 |
return nil |
| 73 |
} else { |
| 74 |
return errors.New("closer on fire") |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func (tc *testConn) SetDeadline(t time.Time) error { |
| 79 |
tc.Deadlines = append(tc.Deadlines, t.Sub(time.Now())) |
| 80 |
if tc.DeadlineCondition == nil || tc.DeadlineCondition.OK() { |
| 81 |
return nil |
| 82 |
} else { |
| 83 |
return errors.New("deadliner on fire") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func (tc *testConn) SetReadDeadline(t time.Time) error { panic("SetReadDeadline not implemented.") } |
| 88 |
func (tc *testConn) SetWriteDeadline(t time.Time) error { panic("SetWriteDeadline not implemented.") } |
| 89 |
func (tc *testConn) Read(buf []byte) (n int, err error) { panic("Read not implemented.") } |
| 90 |
|
| 91 |
func (tc *testConn) Write(buf []byte) (int, error) { |
| 92 |
store := make([]byte, len(buf)) |
| 93 |
copy(store, buf) |
| 94 |
tc.Writes = append(tc.Writes, store) |
| 95 |
if tc.WriteCondition == nil || tc.WriteCondition.OK() { |
| 96 |
return len(store), nil |
| 97 |
} else { |
| 98 |
return -1, errors.New("writer on fire") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// test protocol (from session_test) |
| 103 |
|
| 104 |
type testProtocol struct { |
| 105 |
up chan interface{} |
| 106 |
down chan interface{} |
| 107 |
} |
| 108 |
|
| 109 |
// takeNext takes a value from given channel with a 5s timeout |
| 110 |
func takeNext(ch <-chan interface{}) interface{} { |
| 111 |
select { |
| 112 |
case <-time.After(5 * time.Second): |
| 113 |
panic("test protocol exchange stuck: too long waiting") |
| 114 |
case v := <-ch: |
| 115 |
return v |
| 116 |
} |
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
func (c *testProtocol) SetDeadline(t time.Time) { |
| 121 |
deadAfter := t.Sub(time.Now()) |
| 122 |
deadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond |
| 123 |
c.down <- fmt.Sprintf("deadline %v", deadAfter) |
| 124 |
} |
| 125 |
|
| 126 |
func (c *testProtocol) ReadMessage(dest interface{}) error { |
| 127 |
switch v := takeNext(c.up).(type) { |
| 128 |
case error: |
| 129 |
return v |
| 130 |
default: |
| 131 |
// make sure JSON.Unmarshal works with dest |
| 132 |
var marshalledMsg []byte |
| 133 |
marshalledMsg, err := json.Marshal(v) |
| 134 |
if err != nil { |
| 135 |
return fmt.Errorf("can't jsonify test value %v: %s", v, err) |
| 136 |
} |
| 137 |
return json.Unmarshal(marshalledMsg, dest) |
| 138 |
} |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func (c *testProtocol) WriteMessage(src interface{}) error { |
| 143 |
// make sure JSON.Marshal works with src |
| 144 |
_, err := json.Marshal(src) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
val := reflect.ValueOf(src) |
| 149 |
if val.Kind() == reflect.Ptr { |
| 150 |
src = val.Elem().Interface() |
| 151 |
} |
| 152 |
c.down <- src |
| 153 |
switch v := takeNext(c.up).(type) { |
| 154 |
case error: |
| 155 |
return v |
| 156 |
} |
| 157 |
return nil |
| 158 |
} |
| 159 |
|
| 160 |
// brokenSeenState is a SeenState that always breaks |
| 161 |
type brokenSeenState struct{} |
| 162 |
|
| 163 |
func (*brokenSeenState) SetLevel(string, int64) error { return errors.New("broken.") } |
| 164 |
func (*brokenSeenState) GetAllLevels() (map[string]int64, error) { return nil, errors.New("broken.") } |
| 165 |
func (*brokenSeenState) Close() {} |
| 166 |
func (*brokenSeenState) FilterBySeen([]protocol.Notification) ([]protocol.Notification, error) { |
| 167 |
return nil, errors.New("broken.") |
| 168 |
} |
| 169 |
|
| 170 |
///// |
| 171 |
|
| 172 |
type clientSessionSuite struct { |
| 173 |
log *helpers.TestLogger |
| 174 |
lvls func() (seenstate.SeenState, error) |
| 175 |
} |
| 176 |
|
| 177 |
func (cs *clientSessionSuite) SetUpTest(c *C) { |
| 178 |
cs.log = helpers.NewTestLogger(c, "debug") |
| 179 |
} |
| 180 |
|
| 181 |
// in-memory level map testing |
| 182 |
var _ = Suite(&clientSessionSuite{lvls: seenstate.NewSeenState}) |
| 183 |
|
| 184 |
// sqlite level map testing |
| 185 |
type clientSqlevelsSessionSuite struct{ clientSessionSuite } |
| 186 |
|
| 187 |
var _ = Suite(&clientSqlevelsSessionSuite{}) |
| 188 |
|
| 189 |
func (cs *clientSqlevelsSessionSuite) SetUpSuite(c *C) { |
| 190 |
cs.lvls = func() (seenstate.SeenState, error) { return seenstate.NewSqliteSeenState(":memory:") } |
| 191 |
} |
| 192 |
|
| 193 |
func (cs *clientSessionSuite) TestStateString(c *C) { |
| 194 |
for _, i := range []struct { |
| 195 |
v ClientSessionState |
| 196 |
s string |
| 197 |
}{ |
| 198 |
{Error, "Error"}, |
| 199 |
{Pristine, "Pristine"}, |
| 200 |
{Disconnected, "Disconnected"}, |
| 201 |
{Connected, "Connected"}, |
| 202 |
{Started, "Started"}, |
| 203 |
{Running, "Running"}, |
| 204 |
{Shutdown, "Shutdown"}, |
| 205 |
{Unknown, fmt.Sprintf("??? (%d)", Unknown)}, |
| 206 |
} { |
| 207 |
c.Check(i.v.String(), Equals, i.s) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/**************************************************************** |
| 212 |
parseServerAddrSpec() tests |
| 213 |
****************************************************************/ |
| 214 |
|
| 215 |
func (cs *clientSessionSuite) TestParseServerAddrSpec(c *C) { |
| 216 |
hEp, fallbackHosts := parseServerAddrSpec("http://foo/hosts") |
| 217 |
c.Check(hEp, Equals, "http://foo/hosts") |
| 218 |
c.Check(fallbackHosts, IsNil) |
| 219 |
|
| 220 |
hEp, fallbackHosts = parseServerAddrSpec("foo:443") |
| 221 |
c.Check(hEp, Equals, "") |
| 222 |
c.Check(fallbackHosts, DeepEquals, []string{"foo:443"}) |
| 223 |
|
| 224 |
hEp, fallbackHosts = parseServerAddrSpec("foo:443|bar:443") |
| 225 |
c.Check(hEp, Equals, "") |
| 226 |
c.Check(fallbackHosts, DeepEquals, []string{"foo:443", "bar:443"}) |
| 227 |
} |
| 228 |
|
| 229 |
/**************************************************************** |
| 230 |
NewSession() tests |
| 231 |
****************************************************************/ |
| 232 |
|
| 233 |
func dummyConf() ClientSessionConfig { |
| 234 |
return ClientSessionConfig{ |
| 235 |
BroadcastCh: make(chan *BroadcastNotification, 5), |
| 236 |
NotificationsCh: make(chan AddressedNotification, 5), |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func (cs *clientSessionSuite) TestNewSessionPlainWorks(c *C) { |
| 241 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 242 |
c.Check(sess, NotNil) |
| 243 |
c.Check(err, IsNil) |
| 244 |
c.Check(sess.fallbackHosts, DeepEquals, []string{"foo:443"}) |
| 245 |
// the session is happy and redial delayer is default |
| 246 |
c.Check(sess.ShouldDelay(), Equals, false) |
| 247 |
c.Check(fmt.Sprintf("%p", sess.redialDelay), Equals, fmt.Sprintf("%p", redialDelay)) |
| 248 |
c.Check(sess.redialDelays, DeepEquals, util.Timeouts()) |
| 249 |
// but no root CAs set |
| 250 |
c.Check(sess.TLS.RootCAs, IsNil) |
| 251 |
c.Check(sess.State(), Equals, Pristine) |
| 252 |
c.Check(sess.stopCh, NotNil) |
| 253 |
c.Check(sess.cmdCh, NotNil) |
| 254 |
} |
| 255 |
|
| 256 |
func (cs *clientSessionSuite) TestNewSessionHostEndpointWorks(c *C) { |
| 257 |
sess, err := NewSession("http://foo/hosts", dummyConf(), "wah", cs.lvls, cs.log) |
| 258 |
c.Assert(err, IsNil) |
| 259 |
c.Check(sess.getHost, NotNil) |
| 260 |
} |
| 261 |
|
| 262 |
func (cs *clientSessionSuite) TestNewSessionPEMWorks(c *C) { |
| 263 |
pem, err := ioutil.ReadFile(helpers.SourceRelative("../../server/acceptance/ssl/testing.cert")) |
| 264 |
c.Assert(err, IsNil) |
| 265 |
conf := ClientSessionConfig{PEM: pem} |
| 266 |
sess, err := NewSession("", conf, "wah", cs.lvls, cs.log) |
| 267 |
c.Check(sess, NotNil) |
| 268 |
c.Assert(err, IsNil) |
| 269 |
c.Check(sess.TLS.RootCAs, NotNil) |
| 270 |
} |
| 271 |
|
| 272 |
func (cs *clientSessionSuite) TestNewSessionBadPEMFileContentFails(c *C) { |
| 273 |
badpem := []byte("This is not the PEM you're looking for.") |
| 274 |
conf := ClientSessionConfig{PEM: badpem} |
| 275 |
sess, err := NewSession("", conf, "wah", cs.lvls, cs.log) |
| 276 |
c.Check(sess, IsNil) |
| 277 |
c.Check(err, NotNil) |
| 278 |
} |
| 279 |
|
| 280 |
func (cs *clientSessionSuite) TestNewSessionBadSeenStateFails(c *C) { |
| 281 |
ferr := func() (seenstate.SeenState, error) { return nil, errors.New("Busted.") } |
| 282 |
sess, err := NewSession("", dummyConf(), "wah", ferr, cs.log) |
| 283 |
c.Check(sess, IsNil) |
| 284 |
c.Assert(err, NotNil) |
| 285 |
} |
| 286 |
|
| 287 |
/**************************************************************** |
| 288 |
getHosts() tests |
| 289 |
****************************************************************/ |
| 290 |
|
| 291 |
func (cs *clientSessionSuite) TestGetHostsFallback(c *C) { |
| 292 |
fallback := []string{"foo:443", "bar:443"} |
| 293 |
sess := &clientSession{fallbackHosts: fallback} |
| 294 |
err := sess.getHosts() |
| 295 |
c.Assert(err, IsNil) |
| 296 |
c.Check(sess.deliveryHosts, DeepEquals, fallback) |
| 297 |
} |
| 298 |
|
| 299 |
type testHostGetter struct { |
| 300 |
domain string |
| 301 |
hosts []string |
| 302 |
err error |
| 303 |
} |
| 304 |
|
| 305 |
func (thg *testHostGetter) Get() (*gethosts.Host, error) { |
| 306 |
return &gethosts.Host{thg.domain, thg.hosts}, thg.err |
| 307 |
} |
| 308 |
|
| 309 |
func (cs *clientSessionSuite) TestGetHostsRemote(c *C) { |
| 310 |
hostGetter := &testHostGetter{"example.com", []string{"foo:443", "bar:443"}, nil} |
| 311 |
sess := &clientSession{getHost: hostGetter, timeSince: time.Since} |
| 312 |
err := sess.getHosts() |
| 313 |
c.Assert(err, IsNil) |
| 314 |
c.Check(sess.deliveryHosts, DeepEquals, []string{"foo:443", "bar:443"}) |
| 315 |
} |
| 316 |
|
| 317 |
func (cs *clientSessionSuite) TestGetHostsRemoteError(c *C) { |
| 318 |
sess, err := NewSession("", dummyConf(), "", cs.lvls, cs.log) |
| 319 |
c.Assert(err, IsNil) |
| 320 |
hostsErr := errors.New("failed") |
| 321 |
hostGetter := &testHostGetter{"", nil, hostsErr} |
| 322 |
sess.getHost = hostGetter |
| 323 |
err = sess.getHosts() |
| 324 |
c.Assert(err, Equals, hostsErr) |
| 325 |
c.Check(sess.deliveryHosts, IsNil) |
| 326 |
c.Check(sess.State(), Equals, Error) |
| 327 |
} |
| 328 |
|
| 329 |
func (cs *clientSessionSuite) TestGetHostsRemoteCaching(c *C) { |
| 330 |
hostGetter := &testHostGetter{"example.com", []string{"foo:443", "bar:443"}, nil} |
| 331 |
sess := &clientSession{ |
| 332 |
getHost: hostGetter, |
| 333 |
ClientSessionConfig: ClientSessionConfig{ |
| 334 |
HostsCachingExpiryTime: 2 * time.Hour, |
| 335 |
}, |
| 336 |
timeSince: time.Since, |
| 337 |
} |
| 338 |
err := sess.getHosts() |
| 339 |
c.Assert(err, IsNil) |
| 340 |
hostGetter.hosts = []string{"baz:443"} |
| 341 |
// cached |
| 342 |
err = sess.getHosts() |
| 343 |
c.Assert(err, IsNil) |
| 344 |
c.Check(sess.deliveryHosts, DeepEquals, []string{"foo:443", "bar:443"}) |
| 345 |
// expired |
| 346 |
sess.timeSince = func(ts time.Time) time.Duration { |
| 347 |
return 3 * time.Hour |
| 348 |
} |
| 349 |
err = sess.getHosts() |
| 350 |
c.Assert(err, IsNil) |
| 351 |
c.Check(sess.deliveryHosts, DeepEquals, []string{"baz:443"}) |
| 352 |
} |
| 353 |
|
| 354 |
func (cs *clientSessionSuite) TestGetHostsRemoteCachingReset(c *C) { |
| 355 |
hostGetter := &testHostGetter{"example.com", []string{"foo:443", "bar:443"}, nil} |
| 356 |
sess := &clientSession{ |
| 357 |
getHost: hostGetter, |
| 358 |
ClientSessionConfig: ClientSessionConfig{ |
| 359 |
HostsCachingExpiryTime: 2 * time.Hour, |
| 360 |
}, |
| 361 |
timeSince: time.Since, |
| 362 |
} |
| 363 |
err := sess.getHosts() |
| 364 |
c.Assert(err, IsNil) |
| 365 |
hostGetter.hosts = []string{"baz:443"} |
| 366 |
// cached |
| 367 |
err = sess.getHosts() |
| 368 |
c.Assert(err, IsNil) |
| 369 |
c.Check(sess.deliveryHosts, DeepEquals, []string{"foo:443", "bar:443"}) |
| 370 |
// reset |
| 371 |
sess.resetHosts() |
| 372 |
err = sess.getHosts() |
| 373 |
c.Assert(err, IsNil) |
| 374 |
c.Check(sess.deliveryHosts, DeepEquals, []string{"baz:443"}) |
| 375 |
} |
| 376 |
|
| 377 |
/**************************************************************** |
| 378 |
startConnectionAttempt()/nextHostToTry()/started tests |
| 379 |
****************************************************************/ |
| 380 |
|
| 381 |
func (cs *clientSessionSuite) TestStartConnectionAttempt(c *C) { |
| 382 |
since := time.Since(time.Time{}) |
| 383 |
sess := &clientSession{ |
| 384 |
ClientSessionConfig: ClientSessionConfig{ |
| 385 |
ExpectAllRepairedTime: 10 * time.Second, |
| 386 |
}, |
| 387 |
timeSince: func(ts time.Time) time.Duration { |
| 388 |
return since |
| 389 |
}, |
| 390 |
deliveryHosts: []string{"foo:443", "bar:443"}, |
| 391 |
} |
| 392 |
// start from first host |
| 393 |
sess.startConnectionAttempt() |
| 394 |
c.Check(sess.lastAttemptTimestamp, Not(Equals), 0) |
| 395 |
c.Check(sess.tryHost, Equals, 0) |
| 396 |
c.Check(sess.leftToTry, Equals, 2) |
| 397 |
since = 1 * time.Second |
| 398 |
sess.tryHost = 1 |
| 399 |
// just continue |
| 400 |
sess.startConnectionAttempt() |
| 401 |
c.Check(sess.tryHost, Equals, 1) |
| 402 |
sess.tryHost = 2 |
| 403 |
} |
| 404 |
|
| 405 |
func (cs *clientSessionSuite) TestStartConnectionAttemptNoHostsPanic(c *C) { |
| 406 |
since := time.Since(time.Time{}) |
| 407 |
sess := &clientSession{ |
| 408 |
ClientSessionConfig: ClientSessionConfig{ |
| 409 |
ExpectAllRepairedTime: 10 * time.Second, |
| 410 |
}, |
| 411 |
timeSince: func(ts time.Time) time.Duration { |
| 412 |
return since |
| 413 |
}, |
| 414 |
} |
| 415 |
c.Check(sess.startConnectionAttempt, PanicMatches, "should have got hosts from config or remote at this point") |
| 416 |
} |
| 417 |
|
| 418 |
func (cs *clientSessionSuite) TestNextHostToTry(c *C) { |
| 419 |
sess := &clientSession{ |
| 420 |
deliveryHosts: []string{"foo:443", "bar:443", "baz:443"}, |
| 421 |
tryHost: 0, |
| 422 |
leftToTry: 3, |
| 423 |
} |
| 424 |
c.Check(sess.nextHostToTry(), Equals, "foo:443") |
| 425 |
c.Check(sess.nextHostToTry(), Equals, "bar:443") |
| 426 |
c.Check(sess.nextHostToTry(), Equals, "baz:443") |
| 427 |
c.Check(sess.nextHostToTry(), Equals, "") |
| 428 |
c.Check(sess.nextHostToTry(), Equals, "") |
| 429 |
c.Check(sess.tryHost, Equals, 0) |
| 430 |
|
| 431 |
sess.leftToTry = 3 |
| 432 |
sess.tryHost = 1 |
| 433 |
c.Check(sess.nextHostToTry(), Equals, "bar:443") |
| 434 |
c.Check(sess.nextHostToTry(), Equals, "baz:443") |
| 435 |
c.Check(sess.nextHostToTry(), Equals, "foo:443") |
| 436 |
c.Check(sess.nextHostToTry(), Equals, "") |
| 437 |
c.Check(sess.nextHostToTry(), Equals, "") |
| 438 |
c.Check(sess.tryHost, Equals, 1) |
| 439 |
} |
| 440 |
|
| 441 |
func (cs *clientSessionSuite) TestStarted(c *C) { |
| 442 |
sess, err := NewSession("", dummyConf(), "", cs.lvls, cs.log) |
| 443 |
c.Assert(err, IsNil) |
| 444 |
|
| 445 |
sess.deliveryHosts = []string{"foo:443", "bar:443", "baz:443"} |
| 446 |
sess.tryHost = 1 |
| 447 |
|
| 448 |
sess.started() |
| 449 |
c.Check(sess.tryHost, Equals, 0) |
| 450 |
c.Check(sess.State(), Equals, Started) |
| 451 |
|
| 452 |
sess.started() |
| 453 |
c.Check(sess.tryHost, Equals, 2) |
| 454 |
} |
| 455 |
|
| 456 |
/**************************************************************** |
| 457 |
connect() tests |
| 458 |
****************************************************************/ |
| 459 |
|
| 460 |
func (cs *clientSessionSuite) TestConnectFailsWithNoAddress(c *C) { |
| 461 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 462 |
c.Assert(err, IsNil) |
| 463 |
sess.deliveryHosts = []string{"nowhere"} |
| 464 |
sess.clearShouldDelay() |
| 465 |
err = sess.connect() |
| 466 |
c.Check(sess.ShouldDelay(), Equals, true) |
| 467 |
c.Check(err, ErrorMatches, ".*connect.*address.*") |
| 468 |
c.Check(sess.State(), Equals, Error) |
| 469 |
} |
| 470 |
|
| 471 |
func (cs *clientSessionSuite) TestConnectConnects(c *C) { |
| 472 |
srv, err := net.Listen("tcp", "localhost:0") |
| 473 |
c.Assert(err, IsNil) |
| 474 |
defer srv.Close() |
| 475 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 476 |
c.Assert(err, IsNil) |
| 477 |
sess.deliveryHosts = []string{srv.Addr().String()} |
| 478 |
sess.clearShouldDelay() |
| 479 |
err = sess.connect() |
| 480 |
c.Check(sess.ShouldDelay(), Equals, true) |
| 481 |
c.Check(err, IsNil) |
| 482 |
c.Check(sess.Connection, NotNil) |
| 483 |
c.Check(sess.State(), Equals, Connected) |
| 484 |
} |
| 485 |
|
| 486 |
func (cs *clientSessionSuite) TestConnectSecondConnects(c *C) { |
| 487 |
srv, err := net.Listen("tcp", "localhost:0") |
| 488 |
c.Assert(err, IsNil) |
| 489 |
defer srv.Close() |
| 490 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 491 |
c.Assert(err, IsNil) |
| 492 |
sess.deliveryHosts = []string{"nowhere", srv.Addr().String()} |
| 493 |
sess.clearShouldDelay() |
| 494 |
err = sess.connect() |
| 495 |
c.Check(sess.ShouldDelay(), Equals, true) |
| 496 |
c.Check(err, IsNil) |
| 497 |
c.Check(sess.Connection, NotNil) |
| 498 |
c.Check(sess.State(), Equals, Connected) |
| 499 |
c.Check(sess.tryHost, Equals, 0) |
| 500 |
} |
| 501 |
|
| 502 |
func (cs *clientSessionSuite) TestConnectConnectFail(c *C) { |
| 503 |
srv, err := net.Listen("tcp", "localhost:0") |
| 504 |
c.Assert(err, IsNil) |
| 505 |
sess, err := NewSession(srv.Addr().String(), dummyConf(), "wah", cs.lvls, cs.log) |
| 506 |
srv.Close() |
| 507 |
c.Assert(err, IsNil) |
| 508 |
sess.deliveryHosts = []string{srv.Addr().String()} |
| 509 |
sess.clearShouldDelay() |
| 510 |
err = sess.connect() |
| 511 |
c.Check(sess.ShouldDelay(), Equals, true) |
| 512 |
c.Check(err, ErrorMatches, ".*connection refused") |
| 513 |
c.Check(sess.State(), Equals, Error) |
| 514 |
} |
| 515 |
|
| 516 |
type dumbRetrier struct{ stopped bool } |
| 517 |
|
| 518 |
func (*dumbRetrier) Redial() uint32 { return 0 } |
| 519 |
func (d *dumbRetrier) Stop() { d.stopped = true } |
| 520 |
|
| 521 |
// /**************************************************************** |
| 522 |
// AutoRedial() tests |
| 523 |
// ****************************************************************/ |
| 524 |
|
| 525 |
func (cs *clientSessionSuite) TestAutoRedialWorks(c *C) { |
| 526 |
// checks that AutoRedial sets up a retrier and tries redialing it |
| 527 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 528 |
c.Assert(err, IsNil) |
| 529 |
ar := new(dumbRetrier) |
| 530 |
sess.retrier = ar |
| 531 |
c.Check(ar.stopped, Equals, false) |
| 532 |
sess.autoRedial() |
| 533 |
defer sess.stopRedial() |
| 534 |
c.Check(ar.stopped, Equals, true) |
| 535 |
} |
| 536 |
|
| 537 |
func (cs *clientSessionSuite) TestAutoRedialStopsRetrier(c *C) { |
| 538 |
// checks that AutoRedial stops the previous retrier |
| 539 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 540 |
c.Assert(err, IsNil) |
| 541 |
sess.doneCh = make(chan uint32) |
| 542 |
c.Check(sess.retrier, IsNil) |
| 543 |
sess.autoRedial() |
| 544 |
c.Assert(sess.retrier, NotNil) |
| 545 |
sess.retrier.Stop() |
| 546 |
c.Check(<-sess.doneCh, Not(Equals), 0) |
| 547 |
} |
| 548 |
|
| 549 |
func (cs *clientSessionSuite) TestAutoRedialCallsRedialDelay(c *C) { |
| 550 |
// NOTE there are tests that use calling redialDelay as an indication of calling autoRedial! |
| 551 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 552 |
c.Assert(err, IsNil) |
| 553 |
flag := false |
| 554 |
sess.redialDelay = func(sess *clientSession) time.Duration { flag = true; return 0 } |
| 555 |
sess.autoRedial() |
| 556 |
c.Check(flag, Equals, true) |
| 557 |
} |
| 558 |
|
| 559 |
func (cs *clientSessionSuite) TestAutoRedialSetsRedialDelayIfTooQuick(c *C) { |
| 560 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 561 |
c.Assert(err, IsNil) |
| 562 |
sess.redialDelay = func(sess *clientSession) time.Duration { return 0 } |
| 563 |
sess.autoRedial() |
| 564 |
c.Check(sess.ShouldDelay(), Equals, false) |
| 565 |
sess.stopRedial() |
| 566 |
sess.clearShouldDelay() |
| 567 |
sess.autoRedial() |
| 568 |
c.Check(sess.ShouldDelay(), Equals, true) |
| 569 |
} |
| 570 |
|
| 571 |
/**************************************************************** |
| 572 |
handlePing() tests |
| 573 |
****************************************************************/ |
| 574 |
|
| 575 |
type msgSuite struct { |
| 576 |
sess *clientSession |
| 577 |
upCh chan interface{} |
| 578 |
downCh chan interface{} |
| 579 |
} |
| 580 |
|
| 581 |
var _ = Suite(&msgSuite{}) |
| 582 |
|
| 583 |
func (s *msgSuite) SetUpTest(c *C) { |
| 584 |
var err error |
| 585 |
conf := dummyConf() |
| 586 |
conf.ExchangeTimeout = time.Millisecond |
| 587 |
s.sess, err = NewSession("", conf, "wah", seenstate.NewSeenState, helpers.NewTestLogger(c, "debug")) |
| 588 |
c.Assert(err, IsNil) |
| 589 |
s.sess.Connection = &testConn{Name: "TestHandle*"} |
| 590 |
s.upCh = make(chan interface{}, 5) |
| 591 |
s.downCh = make(chan interface{}, 5) |
| 592 |
s.sess.proto = &testProtocol{up: s.upCh, down: s.downCh} |
| 593 |
} |
| 594 |
|
| 595 |
func (s *msgSuite) TestHandlePingWorks(c *C) { |
| 596 |
s.upCh <- nil // no error |
| 597 |
c.Check(s.sess.handlePing(), IsNil) |
| 598 |
c.Assert(len(s.downCh), Equals, 1) |
| 599 |
c.Check(<-s.downCh, Equals, protocol.PingPongMsg{Type: "pong"}) |
| 600 |
} |
| 601 |
|
| 602 |
func (s *msgSuite) TestHandlePingHandlesPongWriteError(c *C) { |
| 603 |
failure := errors.New("Pong") |
| 604 |
s.upCh <- failure |
| 605 |
|
| 606 |
c.Check(s.sess.handlePing(), Equals, failure) |
| 607 |
c.Assert(len(s.downCh), Equals, 1) |
| 608 |
c.Check(<-s.downCh, Equals, protocol.PingPongMsg{Type: "pong"}) |
| 609 |
c.Check(s.sess.State(), Equals, Error) |
| 610 |
} |
| 611 |
|
| 612 |
func (s *msgSuite) TestHandlePingClearsDelay(c *C) { |
| 613 |
s.sess.setShouldDelay() |
| 614 |
s.upCh <- nil // no error |
| 615 |
c.Check(s.sess.handlePing(), IsNil) |
| 616 |
c.Assert(len(s.downCh), Equals, 1) |
| 617 |
c.Check(<-s.downCh, Equals, protocol.PingPongMsg{Type: "pong"}) |
| 618 |
c.Check(s.sess.ShouldDelay(), Equals, false) |
| 619 |
} |
| 620 |
|
| 621 |
func (s *msgSuite) TestHandlePingDoesNotClearsDelayOnError(c *C) { |
| 622 |
s.sess.setShouldDelay() |
| 623 |
s.upCh <- errors.New("Pong") |
| 624 |
c.Check(s.sess.handlePing(), NotNil) |
| 625 |
c.Assert(len(s.downCh), Equals, 1) |
| 626 |
c.Check(<-s.downCh, Equals, protocol.PingPongMsg{Type: "pong"}) |
| 627 |
c.Check(s.sess.ShouldDelay(), Equals, true) |
| 628 |
} |
| 629 |
|
| 630 |
/**************************************************************** |
| 631 |
handleBroadcast() tests |
| 632 |
****************************************************************/ |
| 633 |
|
| 634 |
func (s *msgSuite) TestHandleBroadcastWorks(c *C) { |
| 635 |
msg := new(serverMsg) |
| 636 |
msg.Type = "broadcast" |
| 637 |
msg.BroadcastMsg = protocol.BroadcastMsg{ |
| 638 |
Type: "broadcast", |
| 639 |
AppId: "--ignored--", |
| 640 |
ChanId: "0", |
| 641 |
TopLevel: 2, |
| 642 |
Payloads: []json.RawMessage{ |
| 643 |
json.RawMessage(`{"img1/m1":[101,"tubular"]}`), |
| 644 |
json.RawMessage("false"), // shouldn't happen but robust |
| 645 |
json.RawMessage(`{"img1/m1":[102,"tubular"]}`), |
| 646 |
}, |
| 647 |
} |
| 648 |
go func() { s.sess.errCh <- s.sess.handleBroadcast(msg) }() |
| 649 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 650 |
s.upCh <- nil // ack ok |
| 651 |
c.Check(<-s.sess.errCh, Equals, nil) |
| 652 |
c.Assert(len(s.sess.BroadcastCh), Equals, 1) |
| 653 |
c.Check(<-s.sess.BroadcastCh, DeepEquals, &BroadcastNotification{ |
| 654 |
TopLevel: 2, |
| 655 |
Decoded: []map[string]interface{}{ |
| 656 |
map[string]interface{}{ |
| 657 |
"img1/m1": []interface{}{float64(101), "tubular"}, |
| 658 |
}, |
| 659 |
map[string]interface{}{ |
| 660 |
"img1/m1": []interface{}{float64(102), "tubular"}, |
| 661 |
}, |
| 662 |
}, |
| 663 |
}) |
| 664 |
// and finally, the session keeps track of the levels |
| 665 |
levels, err := s.sess.SeenState.GetAllLevels() |
| 666 |
c.Check(err, IsNil) |
| 667 |
c.Check(levels, DeepEquals, map[string]int64{"0": 2}) |
| 668 |
} |
| 669 |
|
| 670 |
func (s *msgSuite) TestHandleBroadcastBadAckWrite(c *C) { |
| 671 |
msg := new(serverMsg) |
| 672 |
msg.Type = "broadcast" |
| 673 |
msg.BroadcastMsg = protocol.BroadcastMsg{ |
| 674 |
Type: "broadcast", |
| 675 |
AppId: "APP", |
| 676 |
ChanId: "0", |
| 677 |
TopLevel: 2, |
| 678 |
Payloads: []json.RawMessage{json.RawMessage(`{"b":1}`)}, |
| 679 |
} |
| 680 |
go func() { s.sess.errCh <- s.sess.handleBroadcast(msg) }() |
| 681 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 682 |
failure := errors.New("ACK ACK ACK") |
| 683 |
s.upCh <- failure |
| 684 |
c.Assert(<-s.sess.errCh, Equals, failure) |
| 685 |
c.Check(s.sess.State(), Equals, Error) |
| 686 |
} |
| 687 |
|
| 688 |
func (s *msgSuite) TestHandleBroadcastWrongChannel(c *C) { |
| 689 |
msg := new(serverMsg) |
| 690 |
msg.Type = "brodacast" |
| 691 |
msg.BroadcastMsg = protocol.BroadcastMsg{ |
| 692 |
Type: "broadcast", |
| 693 |
AppId: "APP", |
| 694 |
ChanId: "something awful", |
| 695 |
TopLevel: 2, |
| 696 |
Payloads: []json.RawMessage{json.RawMessage(`{"b":1}`)}, |
| 697 |
} |
| 698 |
go func() { s.sess.errCh <- s.sess.handleBroadcast(msg) }() |
| 699 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 700 |
s.upCh <- nil // ack ok |
| 701 |
c.Check(<-s.sess.errCh, IsNil) |
| 702 |
c.Check(len(s.sess.BroadcastCh), Equals, 0) |
| 703 |
} |
| 704 |
|
| 705 |
func (s *msgSuite) TestHandleBroadcastBrokenSeenState(c *C) { |
| 706 |
s.sess.SeenState = &brokenSeenState{} |
| 707 |
msg := new(serverMsg) |
| 708 |
msg.Type = "broadcast" |
| 709 |
msg.BroadcastMsg = protocol.BroadcastMsg{ |
| 710 |
Type: "broadcast", |
| 711 |
AppId: "--ignored--", |
| 712 |
ChanId: "0", |
| 713 |
TopLevel: 2, |
| 714 |
Payloads: []json.RawMessage{json.RawMessage(`{"b":1}`)}, |
| 715 |
} |
| 716 |
go func() { s.sess.errCh <- s.sess.handleBroadcast(msg) }() |
| 717 |
s.upCh <- nil // ack ok |
| 718 |
// start returns with error |
| 719 |
c.Check(<-s.sess.errCh, Not(Equals), nil) |
| 720 |
c.Check(s.sess.State(), Equals, Error) |
| 721 |
// no message sent out |
| 722 |
c.Check(len(s.sess.BroadcastCh), Equals, 0) |
| 723 |
// and nak'ed it |
| 724 |
c.Check(len(s.downCh), Equals, 1) |
| 725 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"nak"}) |
| 726 |
} |
| 727 |
|
| 728 |
func (s *msgSuite) TestHandleBroadcastClearsDelay(c *C) { |
| 729 |
s.sess.setShouldDelay() |
| 730 |
|
| 731 |
msg := &serverMsg{Type: "broadcast"} |
| 732 |
go func() { s.sess.errCh <- s.sess.handleBroadcast(msg) }() |
| 733 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 734 |
s.upCh <- nil // ack ok |
| 735 |
c.Check(<-s.sess.errCh, IsNil) |
| 736 |
|
| 737 |
c.Check(s.sess.ShouldDelay(), Equals, false) |
| 738 |
} |
| 739 |
|
| 740 |
func (s *msgSuite) TestHandleBroadcastDoesNotClearDelayOnError(c *C) { |
| 741 |
s.sess.setShouldDelay() |
| 742 |
|
| 743 |
msg := &serverMsg{Type: "broadcast"} |
| 744 |
go func() { s.sess.errCh <- s.sess.handleBroadcast(msg) }() |
| 745 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 746 |
s.upCh <- errors.New("bcast") |
| 747 |
c.Check(<-s.sess.errCh, NotNil) |
| 748 |
|
| 749 |
c.Check(s.sess.ShouldDelay(), Equals, true) |
| 750 |
} |
| 751 |
|
| 752 |
/**************************************************************** |
| 753 |
handleNotifications() tests |
| 754 |
****************************************************************/ |
| 755 |
|
| 756 |
type testAddresseeChecking struct { |
| 757 |
ops chan string |
| 758 |
missing string |
| 759 |
} |
| 760 |
|
| 761 |
func (ac *testAddresseeChecking) StartAddresseeBatch() { |
| 762 |
ac.ops <- "start" |
| 763 |
} |
| 764 |
|
| 765 |
func (ac *testAddresseeChecking) CheckForAddressee(notif *protocol.Notification) *click.AppId { |
| 766 |
ac.ops <- notif.AppId |
| 767 |
if notif.AppId != ac.missing { |
| 768 |
id, err := click.ParseAppId(notif.AppId) |
| 769 |
if err != nil { |
| 770 |
panic(err) |
| 771 |
} |
| 772 |
return id |
| 773 |
} else { |
| 774 |
return nil |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
func (s *msgSuite) TestHandleNotificationsWorks(c *C) { |
| 779 |
ac := &testAddresseeChecking{ops: make(chan string, 10)} |
| 780 |
s.sess.AddresseeChecker = ac |
| 781 |
s.sess.setShouldDelay() |
| 782 |
n1 := protocol.Notification{ |
| 783 |
AppId: "com.example.app1_app1", |
| 784 |
MsgId: "a", |
| 785 |
Payload: json.RawMessage(`{"m": 1}`), |
| 786 |
} |
| 787 |
n2 := protocol.Notification{ |
| 788 |
AppId: "com.example.app2_app2", |
| 789 |
MsgId: "b", |
| 790 |
Payload: json.RawMessage(`{"m": 2}`), |
| 791 |
} |
| 792 |
msg := new(serverMsg) |
| 793 |
msg.Type = "notifications" |
| 794 |
msg.NotificationsMsg = protocol.NotificationsMsg{ |
| 795 |
Notifications: []protocol.Notification{n1, n2}, |
| 796 |
} |
| 797 |
go func() { s.sess.errCh <- s.sess.handleNotifications(msg) }() |
| 798 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 799 |
s.upCh <- nil // ack ok |
| 800 |
c.Check(<-s.sess.errCh, Equals, nil) |
| 801 |
c.Check(s.sess.ShouldDelay(), Equals, false) |
| 802 |
c.Assert(s.sess.NotificationsCh, HasLen, 2) |
| 803 |
app1, err := click.ParseAppId("com.example.app1_app1") |
| 804 |
c.Assert(err, IsNil) |
| 805 |
c.Check(<-s.sess.NotificationsCh, DeepEquals, AddressedNotification{ |
| 806 |
To: app1, |
| 807 |
Notification: &n1, |
| 808 |
}) |
| 809 |
app2, err := click.ParseAppId("com.example.app2_app2") |
| 810 |
c.Assert(err, IsNil) |
| 811 |
c.Check(<-s.sess.NotificationsCh, DeepEquals, AddressedNotification{ |
| 812 |
To: app2, |
| 813 |
Notification: &n2, |
| 814 |
}) |
| 815 |
c.Check(ac.ops, HasLen, 3) |
| 816 |
c.Check(<-ac.ops, Equals, "start") |
| 817 |
c.Check(<-ac.ops, Equals, "com.example.app1_app1") |
| 818 |
c.Check(<-ac.ops, Equals, "com.example.app2_app2") |
| 819 |
} |
| 820 |
|
| 821 |
func (s *msgSuite) TestHandleNotificationsAddresseeCheck(c *C) { |
| 822 |
ac := &testAddresseeChecking{ |
| 823 |
ops: make(chan string, 10), |
| 824 |
missing: "com.example.app1_app1", |
| 825 |
} |
| 826 |
s.sess.AddresseeChecker = ac |
| 827 |
s.sess.setShouldDelay() |
| 828 |
n1 := protocol.Notification{ |
| 829 |
AppId: "com.example.app1_app1", |
| 830 |
MsgId: "a", |
| 831 |
Payload: json.RawMessage(`{"m": 1}`), |
| 832 |
} |
| 833 |
n2 := protocol.Notification{ |
| 834 |
AppId: "com.example.app2_app2", |
| 835 |
MsgId: "b", |
| 836 |
Payload: json.RawMessage(`{"m": 2}`), |
| 837 |
} |
| 838 |
msg := new(serverMsg) |
| 839 |
msg.Type = "notifications" |
| 840 |
msg.NotificationsMsg = protocol.NotificationsMsg{ |
| 841 |
Notifications: []protocol.Notification{n1, n2}, |
| 842 |
} |
| 843 |
go func() { s.sess.errCh <- s.sess.handleNotifications(msg) }() |
| 844 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 845 |
s.upCh <- nil // ack ok |
| 846 |
c.Check(<-s.sess.errCh, Equals, nil) |
| 847 |
c.Check(s.sess.ShouldDelay(), Equals, false) |
| 848 |
c.Assert(s.sess.NotificationsCh, HasLen, 1) |
| 849 |
app2, err := click.ParseAppId("com.example.app2_app2") |
| 850 |
c.Assert(err, IsNil) |
| 851 |
c.Check(<-s.sess.NotificationsCh, DeepEquals, AddressedNotification{ |
| 852 |
To: app2, |
| 853 |
Notification: &n2, |
| 854 |
}) |
| 855 |
c.Check(ac.ops, HasLen, 3) |
| 856 |
c.Check(<-ac.ops, Equals, "start") |
| 857 |
c.Check(<-ac.ops, Equals, "com.example.app1_app1") |
| 858 |
} |
| 859 |
|
| 860 |
func (s *msgSuite) TestHandleNotificationsFiltersSeen(c *C) { |
| 861 |
ac := &testAddresseeChecking{ops: make(chan string, 10)} |
| 862 |
s.sess.AddresseeChecker = ac |
| 863 |
n1 := protocol.Notification{ |
| 864 |
AppId: "com.example.app1_app1", |
| 865 |
MsgId: "a", |
| 866 |
Payload: json.RawMessage(`{"m": 1}`), |
| 867 |
} |
| 868 |
n2 := protocol.Notification{ |
| 869 |
AppId: "com.example.app2_app2", |
| 870 |
MsgId: "b", |
| 871 |
Payload: json.RawMessage(`{"m": 2}`), |
| 872 |
} |
| 873 |
msg := new(serverMsg) |
| 874 |
msg.Type = "notifications" |
| 875 |
msg.NotificationsMsg = protocol.NotificationsMsg{ |
| 876 |
Notifications: []protocol.Notification{n1, n2}, |
| 877 |
} |
| 878 |
go func() { s.sess.errCh <- s.sess.handleNotifications(msg) }() |
| 879 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 880 |
s.upCh <- nil // ack ok |
| 881 |
c.Check(<-s.sess.errCh, Equals, nil) |
| 882 |
c.Assert(s.sess.NotificationsCh, HasLen, 2) |
| 883 |
app1, err := click.ParseAppId("com.example.app1_app1") |
| 884 |
c.Assert(err, IsNil) |
| 885 |
c.Check(<-s.sess.NotificationsCh, DeepEquals, AddressedNotification{ |
| 886 |
To: app1, |
| 887 |
Notification: &n1, |
| 888 |
}) |
| 889 |
app2, err := click.ParseAppId("com.example.app2_app2") |
| 890 |
c.Assert(err, IsNil) |
| 891 |
c.Check(<-s.sess.NotificationsCh, DeepEquals, AddressedNotification{ |
| 892 |
To: app2, |
| 893 |
Notification: &n2, |
| 894 |
}) |
| 895 |
c.Check(ac.ops, HasLen, 3) |
| 896 |
|
| 897 |
// second time they get ignored |
| 898 |
go func() { s.sess.errCh <- s.sess.handleNotifications(msg) }() |
| 899 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 900 |
s.upCh <- nil // ack ok |
| 901 |
c.Check(<-s.sess.errCh, Equals, nil) |
| 902 |
c.Assert(s.sess.NotificationsCh, HasLen, 0) |
| 903 |
c.Check(ac.ops, HasLen, 4) |
| 904 |
} |
| 905 |
|
| 906 |
func (s *msgSuite) TestHandleNotificationsBadAckWrite(c *C) { |
| 907 |
s.sess.setShouldDelay() |
| 908 |
n1 := protocol.Notification{ |
| 909 |
AppId: "com.example.app1_app1", |
| 910 |
MsgId: "a", |
| 911 |
Payload: json.RawMessage(`{"m": 1}`), |
| 912 |
} |
| 913 |
msg := new(serverMsg) |
| 914 |
msg.Type = "notifications" |
| 915 |
msg.NotificationsMsg = protocol.NotificationsMsg{ |
| 916 |
Notifications: []protocol.Notification{n1}, |
| 917 |
} |
| 918 |
go func() { s.sess.errCh <- s.sess.handleNotifications(msg) }() |
| 919 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 920 |
failure := errors.New("ACK ACK ACK") |
| 921 |
s.upCh <- failure |
| 922 |
c.Assert(<-s.sess.errCh, Equals, failure) |
| 923 |
c.Check(s.sess.State(), Equals, Error) |
| 924 |
// didn't get to clear |
| 925 |
c.Check(s.sess.ShouldDelay(), Equals, true) |
| 926 |
} |
| 927 |
|
| 928 |
func (s *msgSuite) TestHandleNotificationsBrokenSeenState(c *C) { |
| 929 |
s.sess.setShouldDelay() |
| 930 |
s.sess.SeenState = &brokenSeenState{} |
| 931 |
n1 := protocol.Notification{ |
| 932 |
AppId: "com.example.app1_app1", |
| 933 |
MsgId: "a", |
| 934 |
Payload: json.RawMessage(`{"m": 1}`), |
| 935 |
} |
| 936 |
msg := new(serverMsg) |
| 937 |
msg.Type = "notifications" |
| 938 |
msg.NotificationsMsg = protocol.NotificationsMsg{ |
| 939 |
Notifications: []protocol.Notification{n1}, |
| 940 |
} |
| 941 |
go func() { s.sess.errCh <- s.sess.handleNotifications(msg) }() |
| 942 |
s.upCh <- nil // ack ok |
| 943 |
// start returns with error |
| 944 |
c.Check(<-s.sess.errCh, Not(Equals), nil) |
| 945 |
c.Check(s.sess.State(), Equals, Error) |
| 946 |
// no message sent out |
| 947 |
c.Check(len(s.sess.NotificationsCh), Equals, 0) |
| 948 |
// and nak'ed it |
| 949 |
c.Check(len(s.downCh), Equals, 1) |
| 950 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"nak"}) |
| 951 |
// didn't get to clear |
| 952 |
c.Check(s.sess.ShouldDelay(), Equals, true) |
| 953 |
} |
| 954 |
|
| 955 |
/**************************************************************** |
| 956 |
handleConnBroken() tests |
| 957 |
****************************************************************/ |
| 958 |
|
| 959 |
func (s *msgSuite) TestHandleConnBrokenUnkwown(c *C) { |
| 960 |
msg := new(serverMsg) |
| 961 |
msg.Type = "connbroken" |
| 962 |
msg.ConnBrokenMsg = protocol.ConnBrokenMsg{ |
| 963 |
Reason: "REASON", |
| 964 |
} |
| 965 |
go func() { s.sess.errCh <- s.sess.handleConnBroken(msg) }() |
| 966 |
c.Check(<-s.sess.errCh, ErrorMatches, "server broke connection: REASON") |
| 967 |
c.Check(s.sess.State(), Equals, Error) |
| 968 |
} |
| 969 |
|
| 970 |
func (s *msgSuite) TestHandleConnBrokenHostMismatch(c *C) { |
| 971 |
msg := new(serverMsg) |
| 972 |
msg.Type = "connbroken" |
| 973 |
msg.ConnBrokenMsg = protocol.ConnBrokenMsg{ |
| 974 |
Reason: protocol.BrokenHostMismatch, |
| 975 |
} |
| 976 |
s.sess.deliveryHosts = []string{"foo:443", "bar:443"} |
| 977 |
go func() { s.sess.errCh <- s.sess.handleConnBroken(msg) }() |
| 978 |
c.Check(<-s.sess.errCh, ErrorMatches, "server broke connection: host-mismatch") |
| 979 |
c.Check(s.sess.State(), Equals, Error) |
| 980 |
// hosts were reset |
| 981 |
c.Check(s.sess.deliveryHosts, IsNil) |
| 982 |
} |
| 983 |
|
| 984 |
/**************************************************************** |
| 985 |
loop() tests |
| 986 |
****************************************************************/ |
| 987 |
|
| 988 |
type loopSuite msgSuite |
| 989 |
|
| 990 |
var _ = Suite(&loopSuite{}) |
| 991 |
|
| 992 |
func (s *loopSuite) SetUpTest(c *C) { |
| 993 |
(*msgSuite)(s).SetUpTest(c) |
| 994 |
s.sess.Connection.(*testConn).Name = "TestLoop*" |
| 995 |
go func() { |
| 996 |
s.sess.errCh <- s.sess.loop() |
| 997 |
}() |
| 998 |
} |
| 999 |
|
| 1000 |
func (s *loopSuite) waitUntilRunning(c *C) { |
| 1001 |
delay := time.Duration(5000) |
| 1002 |
for i := 0; i < 5; i++ { |
| 1003 |
if s.sess.State() == Running { |
| 1004 |
return |
| 1005 |
} |
| 1006 |
time.Sleep(delay) |
| 1007 |
delay *= 2 |
| 1008 |
} |
| 1009 |
c.Check(s.sess.State(), Equals, Running) |
| 1010 |
} |
| 1011 |
|
| 1012 |
func (s *loopSuite) TestLoopReadError(c *C) { |
| 1013 |
s.waitUntilRunning(c) |
| 1014 |
s.upCh <- errors.New("Read") |
| 1015 |
err := <-s.sess.errCh |
| 1016 |
c.Check(err, ErrorMatches, "Read") |
| 1017 |
c.Check(s.sess.State(), Equals, Error) |
| 1018 |
} |
| 1019 |
|
| 1020 |
func (s *loopSuite) TestLoopPing(c *C) { |
| 1021 |
s.waitUntilRunning(c) |
| 1022 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1023 |
s.upCh <- protocol.PingPongMsg{Type: "ping"} |
| 1024 |
c.Check(takeNext(s.downCh), Equals, protocol.PingPongMsg{Type: "pong"}) |
| 1025 |
failure := errors.New("pong") |
| 1026 |
s.upCh <- failure |
| 1027 |
c.Check(<-s.sess.errCh, Equals, failure) |
| 1028 |
} |
| 1029 |
|
| 1030 |
func (s *loopSuite) TestLoopLoopsDaLoop(c *C) { |
| 1031 |
s.waitUntilRunning(c) |
| 1032 |
for i := 1; i < 10; i++ { |
| 1033 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1034 |
s.upCh <- protocol.PingPongMsg{Type: "ping"} |
| 1035 |
c.Check(takeNext(s.downCh), Equals, protocol.PingPongMsg{Type: "pong"}) |
| 1036 |
s.upCh <- nil |
| 1037 |
} |
| 1038 |
failure := errors.New("pong") |
| 1039 |
s.upCh <- failure |
| 1040 |
c.Check(<-s.sess.errCh, Equals, failure) |
| 1041 |
} |
| 1042 |
|
| 1043 |
func (s *loopSuite) TestLoopBroadcast(c *C) { |
| 1044 |
s.waitUntilRunning(c) |
| 1045 |
b := &protocol.BroadcastMsg{ |
| 1046 |
Type: "broadcast", |
| 1047 |
AppId: "--ignored--", |
| 1048 |
ChanId: "0", |
| 1049 |
TopLevel: 2, |
| 1050 |
Payloads: []json.RawMessage{json.RawMessage(`{"b":1}`)}, |
| 1051 |
} |
| 1052 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1053 |
s.upCh <- b |
| 1054 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 1055 |
failure := errors.New("ack") |
| 1056 |
s.upCh <- failure |
| 1057 |
c.Check(<-s.sess.errCh, Equals, failure) |
| 1058 |
} |
| 1059 |
|
| 1060 |
func (s *loopSuite) TestLoopNotifications(c *C) { |
| 1061 |
s.waitUntilRunning(c) |
| 1062 |
|
| 1063 |
n1 := protocol.Notification{ |
| 1064 |
AppId: "app1", |
| 1065 |
MsgId: "a", |
| 1066 |
Payload: json.RawMessage(`{"m": 1}`), |
| 1067 |
} |
| 1068 |
msg := &protocol.NotificationsMsg{ |
| 1069 |
Type: "notifications", |
| 1070 |
Notifications: []protocol.Notification{n1}, |
| 1071 |
} |
| 1072 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1073 |
s.upCh <- msg |
| 1074 |
c.Check(takeNext(s.downCh), Equals, protocol.AckMsg{"ack"}) |
| 1075 |
failure := errors.New("ack") |
| 1076 |
s.upCh <- failure |
| 1077 |
c.Check(<-s.sess.errCh, Equals, failure) |
| 1078 |
} |
| 1079 |
|
| 1080 |
func (s *loopSuite) TestLoopSetParams(c *C) { |
| 1081 |
s.waitUntilRunning(c) |
| 1082 |
setParams := protocol.SetParamsMsg{ |
| 1083 |
Type: "setparams", |
| 1084 |
SetCookie: "COOKIE", |
| 1085 |
} |
| 1086 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1087 |
s.upCh <- setParams |
| 1088 |
failure := errors.New("fail") |
| 1089 |
s.upCh <- failure |
| 1090 |
c.Assert(<-s.sess.errCh, Equals, failure) |
| 1091 |
c.Check(s.sess.getCookie(), Equals, "COOKIE") |
| 1092 |
} |
| 1093 |
|
| 1094 |
func (s *loopSuite) TestLoopConnBroken(c *C) { |
| 1095 |
s.waitUntilRunning(c) |
| 1096 |
broken := protocol.ConnBrokenMsg{ |
| 1097 |
Type: "connbroken", |
| 1098 |
Reason: "REASON", |
| 1099 |
} |
| 1100 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1101 |
s.upCh <- broken |
| 1102 |
c.Check(<-s.sess.errCh, NotNil) |
| 1103 |
} |
| 1104 |
|
| 1105 |
func (s *loopSuite) TestLoopConnWarn(c *C) { |
| 1106 |
warn := protocol.ConnWarnMsg{ |
| 1107 |
Type: "warn", |
| 1108 |
Reason: "XXX", |
| 1109 |
} |
| 1110 |
connwarn := protocol.ConnWarnMsg{ |
| 1111 |
Type: "connwarn", |
| 1112 |
Reason: "REASON", |
| 1113 |
} |
| 1114 |
failure := errors.New("warn") |
| 1115 |
log := s.sess.Log.(*helpers.TestLogger) |
| 1116 |
|
| 1117 |
s.waitUntilRunning(c) |
| 1118 |
c.Check(takeNext(s.downCh), Equals, "deadline 1ms") |
| 1119 |
log.ResetCapture() |
| 1120 |
s.upCh <- warn |
| 1121 |
s.upCh <- connwarn |
| 1122 |
s.upCh <- failure |
| 1123 |
c.Check(<-s.sess.errCh, Equals, failure) |
| 1124 |
c.Check(log.Captured(), |
| 1125 |
Matches, `(?ms).* warning: XXX$.*`) |
| 1126 |
c.Check(log.Captured(), |
| 1127 |
Matches, `(?ms).* warning: REASON$`) |
| 1128 |
} |
| 1129 |
|
| 1130 |
/**************************************************************** |
| 1131 |
start() tests |
| 1132 |
****************************************************************/ |
| 1133 |
func (cs *clientSessionSuite) TestStartFailsIfSetDeadlineFails(c *C) { |
| 1134 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1135 |
c.Assert(err, IsNil) |
| 1136 |
sess.Connection = &testConn{Name: "TestStartFailsIfSetDeadlineFails", |
| 1137 |
DeadlineCondition: condition.Work(false)} // setdeadline will fail |
| 1138 |
err = sess.start() |
| 1139 |
c.Check(err, ErrorMatches, ".*deadline.*") |
| 1140 |
c.Check(sess.State(), Equals, Error) |
| 1141 |
} |
| 1142 |
|
| 1143 |
func (cs *clientSessionSuite) TestStartFailsIfWriteFails(c *C) { |
| 1144 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1145 |
c.Assert(err, IsNil) |
| 1146 |
sess.Connection = &testConn{Name: "TestStartFailsIfWriteFails", |
| 1147 |
WriteCondition: condition.Work(false)} // write will fail |
| 1148 |
err = sess.start() |
| 1149 |
c.Check(err, ErrorMatches, ".*write.*") |
| 1150 |
c.Check(sess.State(), Equals, Error) |
| 1151 |
} |
| 1152 |
|
| 1153 |
func (cs *clientSessionSuite) TestStartFailsIfGetLevelsFails(c *C) { |
| 1154 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1155 |
c.Assert(err, IsNil) |
| 1156 |
sess.SeenState = &brokenSeenState{} |
| 1157 |
sess.Connection = &testConn{Name: "TestStartConnectMessageFails"} |
| 1158 |
errCh := make(chan error, 1) |
| 1159 |
upCh := make(chan interface{}, 5) |
| 1160 |
downCh := make(chan interface{}, 5) |
| 1161 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1162 |
sess.Protocolator = func(_ net.Conn) protocol.Protocol { return proto } |
| 1163 |
|
| 1164 |
go func() { |
| 1165 |
errCh <- sess.start() |
| 1166 |
}() |
| 1167 |
|
| 1168 |
c.Check(takeNext(downCh), Equals, "deadline 0s") |
| 1169 |
err = <-errCh |
| 1170 |
c.Check(err, ErrorMatches, "broken.") |
| 1171 |
} |
| 1172 |
|
| 1173 |
func (cs *clientSessionSuite) TestStartConnectMessageFails(c *C) { |
| 1174 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1175 |
c.Assert(err, IsNil) |
| 1176 |
sess.Connection = &testConn{Name: "TestStartConnectMessageFails"} |
| 1177 |
errCh := make(chan error, 1) |
| 1178 |
upCh := make(chan interface{}, 5) |
| 1179 |
downCh := make(chan interface{}, 5) |
| 1180 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1181 |
sess.Protocolator = func(_ net.Conn) protocol.Protocol { return proto } |
| 1182 |
|
| 1183 |
go func() { |
| 1184 |
errCh <- sess.start() |
| 1185 |
}() |
| 1186 |
|
| 1187 |
c.Check(takeNext(downCh), Equals, "deadline 0s") |
| 1188 |
c.Check(takeNext(downCh), DeepEquals, protocol.ConnectMsg{ |
| 1189 |
Type: "connect", |
| 1190 |
DeviceId: sess.DeviceId, |
| 1191 |
Levels: map[string]int64{}, |
| 1192 |
}) |
| 1193 |
upCh <- errors.New("Overflow error in /dev/null") |
| 1194 |
err = <-errCh |
| 1195 |
c.Check(err, ErrorMatches, "Overflow.*null") |
| 1196 |
c.Check(sess.State(), Equals, Error) |
| 1197 |
} |
| 1198 |
|
| 1199 |
func (cs *clientSessionSuite) TestStartConnackReadError(c *C) { |
| 1200 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1201 |
c.Assert(err, IsNil) |
| 1202 |
sess.Connection = &testConn{Name: "TestStartConnackReadError"} |
| 1203 |
errCh := make(chan error, 1) |
| 1204 |
upCh := make(chan interface{}, 5) |
| 1205 |
downCh := make(chan interface{}, 5) |
| 1206 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1207 |
sess.Protocolator = func(_ net.Conn) protocol.Protocol { return proto } |
| 1208 |
|
| 1209 |
go func() { |
| 1210 |
errCh <- sess.start() |
| 1211 |
}() |
| 1212 |
|
| 1213 |
c.Check(takeNext(downCh), Equals, "deadline 0s") |
| 1214 |
_, ok := takeNext(downCh).(protocol.ConnectMsg) |
| 1215 |
c.Check(ok, Equals, true) |
| 1216 |
upCh <- nil // no error |
| 1217 |
upCh <- io.EOF |
| 1218 |
err = <-errCh |
| 1219 |
c.Check(err, ErrorMatches, ".*EOF.*") |
| 1220 |
c.Check(sess.State(), Equals, Error) |
| 1221 |
} |
| 1222 |
|
| 1223 |
func (cs *clientSessionSuite) TestStartBadConnack(c *C) { |
| 1224 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1225 |
c.Assert(err, IsNil) |
| 1226 |
sess.Connection = &testConn{Name: "TestStartBadConnack"} |
| 1227 |
errCh := make(chan error, 1) |
| 1228 |
upCh := make(chan interface{}, 5) |
| 1229 |
downCh := make(chan interface{}, 5) |
| 1230 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1231 |
sess.Protocolator = func(_ net.Conn) protocol.Protocol { return proto } |
| 1232 |
|
| 1233 |
go func() { |
| 1234 |
errCh <- sess.start() |
| 1235 |
}() |
| 1236 |
|
| 1237 |
c.Check(takeNext(downCh), Equals, "deadline 0s") |
| 1238 |
_, ok := takeNext(downCh).(protocol.ConnectMsg) |
| 1239 |
c.Check(ok, Equals, true) |
| 1240 |
upCh <- nil // no error |
| 1241 |
upCh <- protocol.ConnAckMsg{Type: "connack"} |
| 1242 |
err = <-errCh |
| 1243 |
c.Check(err, ErrorMatches, ".*invalid.*") |
| 1244 |
c.Check(sess.State(), Equals, Error) |
| 1245 |
} |
| 1246 |
|
| 1247 |
func (cs *clientSessionSuite) TestStartNotConnack(c *C) { |
| 1248 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1249 |
c.Assert(err, IsNil) |
| 1250 |
sess.Connection = &testConn{Name: "TestStartBadConnack"} |
| 1251 |
errCh := make(chan error, 1) |
| 1252 |
upCh := make(chan interface{}, 5) |
| 1253 |
downCh := make(chan interface{}, 5) |
| 1254 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1255 |
sess.Protocolator = func(_ net.Conn) protocol.Protocol { return proto } |
| 1256 |
|
| 1257 |
go func() { |
| 1258 |
errCh <- sess.start() |
| 1259 |
}() |
| 1260 |
|
| 1261 |
c.Check(takeNext(downCh), Equals, "deadline 0s") |
| 1262 |
_, ok := takeNext(downCh).(protocol.ConnectMsg) |
| 1263 |
c.Check(ok, Equals, true) |
| 1264 |
upCh <- nil // no error |
| 1265 |
upCh <- protocol.ConnAckMsg{Type: "connnak"} |
| 1266 |
err = <-errCh |
| 1267 |
c.Check(err, ErrorMatches, ".*CONNACK.*") |
| 1268 |
c.Check(sess.State(), Equals, Error) |
| 1269 |
} |
| 1270 |
|
| 1271 |
func (cs *clientSessionSuite) TestStartWorks(c *C) { |
| 1272 |
info := map[string]interface{}{ |
| 1273 |
"foo": 1, |
| 1274 |
"bar": "baz", |
| 1275 |
} |
| 1276 |
conf := ClientSessionConfig{ |
| 1277 |
Info: info, |
| 1278 |
} |
| 1279 |
sess, err := NewSession("", conf, "wah", cs.lvls, cs.log) |
| 1280 |
c.Assert(err, IsNil) |
| 1281 |
sess.Connection = &testConn{Name: "TestStartWorks"} |
| 1282 |
sess.setCookie("COOKIE") |
| 1283 |
errCh := make(chan error, 1) |
| 1284 |
upCh := make(chan interface{}, 5) |
| 1285 |
downCh := make(chan interface{}, 5) |
| 1286 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1287 |
sess.Protocolator = func(_ net.Conn) protocol.Protocol { return proto } |
| 1288 |
|
| 1289 |
go func() { |
| 1290 |
errCh <- sess.start() |
| 1291 |
}() |
| 1292 |
|
| 1293 |
c.Check(takeNext(downCh), Equals, "deadline 0s") |
| 1294 |
msg, ok := takeNext(downCh).(protocol.ConnectMsg) |
| 1295 |
c.Check(ok, Equals, true) |
| 1296 |
c.Check(msg.DeviceId, Equals, "wah") |
| 1297 |
c.Check(msg.Cookie, Equals, "COOKIE") |
| 1298 |
c.Check(msg.Info, DeepEquals, info) |
| 1299 |
upCh <- nil // no error |
| 1300 |
upCh <- protocol.ConnAckMsg{ |
| 1301 |
Type: "connack", |
| 1302 |
Params: protocol.ConnAckParams{(10 * time.Millisecond).String()}, |
| 1303 |
} |
| 1304 |
// start is now done. |
| 1305 |
err = <-errCh |
| 1306 |
c.Check(err, IsNil) |
| 1307 |
c.Check(sess.State(), Equals, Started) |
| 1308 |
} |
| 1309 |
|
| 1310 |
/**************************************************************** |
| 1311 |
run() tests |
| 1312 |
****************************************************************/ |
| 1313 |
|
| 1314 |
func (cs *clientSessionSuite) TestRunCallsCloserWithFalse(c *C) { |
| 1315 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1316 |
c.Assert(err, IsNil) |
| 1317 |
failure := errors.New("bail") |
| 1318 |
has_closed := false |
| 1319 |
with_false := false |
| 1320 |
err = sess.run( |
| 1321 |
func(b bool) { has_closed = true; with_false = !b }, |
| 1322 |
func() error { return failure }, |
| 1323 |
nil, |
| 1324 |
nil, |
| 1325 |
nil) |
| 1326 |
c.Check(err, Equals, failure) |
| 1327 |
c.Check(has_closed, Equals, true) |
| 1328 |
c.Check(with_false, Equals, true) |
| 1329 |
} |
| 1330 |
|
| 1331 |
func (cs *clientSessionSuite) TestRunBailsIfHostGetterFails(c *C) { |
| 1332 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1333 |
c.Assert(err, IsNil) |
| 1334 |
failure := errors.New("TestRunBailsIfHostGetterFails") |
| 1335 |
has_closed := false |
| 1336 |
err = sess.run( |
| 1337 |
func(bool) { has_closed = true }, |
| 1338 |
func() error { return failure }, |
| 1339 |
nil, |
| 1340 |
nil, |
| 1341 |
nil) |
| 1342 |
c.Check(err, Equals, failure) |
| 1343 |
c.Check(has_closed, Equals, true) |
| 1344 |
} |
| 1345 |
|
| 1346 |
func (cs *clientSessionSuite) TestRunBailsIfConnectFails(c *C) { |
| 1347 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1348 |
c.Assert(err, IsNil) |
| 1349 |
failure := errors.New("TestRunBailsIfConnectFails") |
| 1350 |
err = sess.run( |
| 1351 |
func(bool) {}, |
| 1352 |
func() error { return nil }, |
| 1353 |
func() error { return failure }, |
| 1354 |
nil, |
| 1355 |
nil) |
| 1356 |
c.Check(err, Equals, failure) |
| 1357 |
} |
| 1358 |
|
| 1359 |
func (cs *clientSessionSuite) TestRunBailsIfStartFails(c *C) { |
| 1360 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1361 |
c.Assert(err, IsNil) |
| 1362 |
failure := errors.New("TestRunBailsIfStartFails") |
| 1363 |
err = sess.run( |
| 1364 |
func(bool) {}, |
| 1365 |
func() error { return nil }, |
| 1366 |
func() error { return nil }, |
| 1367 |
func() error { return failure }, |
| 1368 |
nil) |
| 1369 |
c.Check(err, Equals, failure) |
| 1370 |
} |
| 1371 |
|
| 1372 |
func (cs *clientSessionSuite) TestRunRunsEvenIfLoopFails(c *C) { |
| 1373 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1374 |
c.Assert(err, IsNil) |
| 1375 |
failureCh := make(chan error) // must be unbuffered |
| 1376 |
notf := &BroadcastNotification{} |
| 1377 |
err = sess.run( |
| 1378 |
func(bool) {}, |
| 1379 |
func() error { return nil }, |
| 1380 |
func() error { return nil }, |
| 1381 |
func() error { return nil }, |
| 1382 |
func() error { sess.BroadcastCh <- notf; return <-failureCh }) |
| 1383 |
c.Check(err, Equals, nil) |
| 1384 |
// if run doesn't error it sets up the channels |
| 1385 |
c.Assert(sess.errCh, NotNil) |
| 1386 |
c.Assert(sess.BroadcastCh, NotNil) |
| 1387 |
c.Check(<-sess.BroadcastCh, Equals, notf) |
| 1388 |
failure := errors.New("TestRunRunsEvenIfLoopFails") |
| 1389 |
failureCh <- failure |
| 1390 |
c.Check(<-sess.errCh, Equals, failure) |
| 1391 |
// so now you know it was running in a goroutine :) |
| 1392 |
} |
| 1393 |
|
| 1394 |
/**************************************************************** |
| 1395 |
Jitter() tests |
| 1396 |
****************************************************************/ |
| 1397 |
|
| 1398 |
func (cs *clientSessionSuite) TestJitter(c *C) { |
| 1399 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1400 |
c.Assert(err, IsNil) |
| 1401 |
num_tries := 20 // should do the math |
| 1402 |
spread := time.Second // |
| 1403 |
has_neg := false |
| 1404 |
has_pos := false |
| 1405 |
has_zero := true |
| 1406 |
for i := 0; i < num_tries; i++ { |
| 1407 |
n := sess.Jitter(spread) |
| 1408 |
if n > 0 { |
| 1409 |
has_pos = true |
| 1410 |
} else if n < 0 { |
| 1411 |
has_neg = true |
| 1412 |
} else { |
| 1413 |
has_zero = true |
| 1414 |
} |
| 1415 |
} |
| 1416 |
c.Check(has_neg, Equals, true) |
| 1417 |
c.Check(has_pos, Equals, true) |
| 1418 |
c.Check(has_zero, Equals, true) |
| 1419 |
|
| 1420 |
// a negative spread is caught in the reasonable place |
| 1421 |
c.Check(func() { sess.Jitter(time.Duration(-1)) }, PanicMatches, |
| 1422 |
"spread must be non-negative") |
| 1423 |
} |
| 1424 |
|
| 1425 |
/**************************************************************** |
| 1426 |
Dial() tests |
| 1427 |
****************************************************************/ |
| 1428 |
|
| 1429 |
func (cs *clientSessionSuite) TestDialPanics(c *C) { |
| 1430 |
// one last unhappy test |
| 1431 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1432 |
c.Assert(err, IsNil) |
| 1433 |
sess.Protocolator = nil |
| 1434 |
c.Check(sess.Dial, PanicMatches, ".*protocol constructor.") |
| 1435 |
} |
| 1436 |
|
| 1437 |
var ( |
| 1438 |
dialTestTimeout = 300 * time.Millisecond |
| 1439 |
) |
| 1440 |
|
| 1441 |
func dialTestConf(certPEM []byte) ClientSessionConfig { |
| 1442 |
conf := dummyConf() |
| 1443 |
conf.ExchangeTimeout = dialTestTimeout |
| 1444 |
if certPEM == nil { |
| 1445 |
conf.PEM = helpers.TestCertPEMBlock |
| 1446 |
} else { |
| 1447 |
conf.PEM = certPEM |
| 1448 |
} |
| 1449 |
return conf |
| 1450 |
} |
| 1451 |
|
| 1452 |
func (cs *clientSessionSuite) TestDialBadServerName(c *C) { |
| 1453 |
// a borked server name |
| 1454 |
lst, err := tls.Listen("tcp", "localhost:0", helpers.TestTLSServerConfig) |
| 1455 |
c.Assert(err, IsNil) |
| 1456 |
// advertise |
| 1457 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1458 |
b, err := json.Marshal(map[string]interface{}{ |
| 1459 |
"domain": "xyzzy", // <-- *** THIS *** is the bit that'll break it |
| 1460 |
"hosts": []string{"nowhere", lst.Addr().String()}, |
| 1461 |
}) |
| 1462 |
if err != nil { |
| 1463 |
panic(err) |
| 1464 |
} |
| 1465 |
w.Header().Set("Content-Type", "application/json") |
| 1466 |
w.Write(b) |
| 1467 |
})) |
| 1468 |
defer ts.Close() |
| 1469 |
|
| 1470 |
sess, err := NewSession(ts.URL, dialTestConf(nil), "wah", cs.lvls, cs.log) |
| 1471 |
c.Assert(err, IsNil) |
| 1472 |
tconn := &testConn{} |
| 1473 |
sess.Connection = tconn |
| 1474 |
|
| 1475 |
upCh := make(chan interface{}, 5) |
| 1476 |
downCh := make(chan interface{}, 5) |
| 1477 |
errCh := make(chan error, 1) |
| 1478 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1479 |
sess.Protocolator = func(net.Conn) protocol.Protocol { return proto } |
| 1480 |
|
| 1481 |
go func() { |
| 1482 |
errCh <- sess.Dial() |
| 1483 |
}() |
| 1484 |
|
| 1485 |
srv, err := lst.Accept() |
| 1486 |
c.Assert(err, IsNil) |
| 1487 |
|
| 1488 |
// connect done |
| 1489 |
|
| 1490 |
_, err = protocol.ReadWireFormatVersion(srv, dialTestTimeout) |
| 1491 |
c.Check(err, NotNil) |
| 1492 |
|
| 1493 |
c.Check(<-errCh, NotNil) |
| 1494 |
c.Check(sess.State(), Equals, Error) |
| 1495 |
} |
| 1496 |
|
| 1497 |
func (cs *clientSessionSuite) TestDialWorks(c *C) { |
| 1498 |
// happy path thoughts |
| 1499 |
lst, err := tls.Listen("tcp", "localhost:0", helpers.TestTLSServerConfig) |
| 1500 |
c.Assert(err, IsNil) |
| 1501 |
// advertise |
| 1502 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1503 |
b, err := json.Marshal(map[string]interface{}{ |
| 1504 |
"domain": "push-delivery", |
| 1505 |
"hosts": []string{"nowhere", lst.Addr().String()}, |
| 1506 |
}) |
| 1507 |
if err != nil { |
| 1508 |
panic(err) |
| 1509 |
} |
| 1510 |
w.Header().Set("Content-Type", "application/json") |
| 1511 |
w.Write(b) |
| 1512 |
})) |
| 1513 |
defer ts.Close() |
| 1514 |
|
| 1515 |
sess, err := NewSession(ts.URL, dialTestConf(nil), "wah", cs.lvls, cs.log) |
| 1516 |
c.Assert(err, IsNil) |
| 1517 |
tconn := &testConn{CloseCondition: condition.Fail2Work(10)} |
| 1518 |
sess.Connection = tconn |
| 1519 |
// just to be sure: |
| 1520 |
c.Check(tconn.CloseCondition.String(), Matches, ".* 10 to go.") |
| 1521 |
|
| 1522 |
upCh := make(chan interface{}, 5) |
| 1523 |
downCh := make(chan interface{}, 5) |
| 1524 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1525 |
sess.Protocolator = func(net.Conn) protocol.Protocol { return proto } |
| 1526 |
|
| 1527 |
go sess.Dial() |
| 1528 |
|
| 1529 |
srv, err := lst.Accept() |
| 1530 |
c.Assert(err, IsNil) |
| 1531 |
|
| 1532 |
// connect done |
| 1533 |
|
| 1534 |
// Dial should have had the session's old connection (tconn) closed |
| 1535 |
// before connecting a new one; if that was done, tconn's condition |
| 1536 |
// ticked forward: |
| 1537 |
c.Check(tconn.CloseCondition.String(), Matches, ".* 9 to go.") |
| 1538 |
|
| 1539 |
// now, start: 1. protocol version |
| 1540 |
v, err := protocol.ReadWireFormatVersion(srv, dialTestTimeout) |
| 1541 |
c.Assert(err, IsNil) |
| 1542 |
c.Assert(v, Equals, protocol.ProtocolWireVersion) |
| 1543 |
|
| 1544 |
// if something goes wrong session would try the first/other host |
| 1545 |
c.Check(sess.tryHost, Equals, 0) |
| 1546 |
|
| 1547 |
// 2. "connect" (but on the fake protcol above! woo) |
| 1548 |
|
| 1549 |
c.Check(takeNext(downCh), Equals, fmt.Sprintf("deadline %v", dialTestTimeout)) |
| 1550 |
_, ok := takeNext(downCh).(protocol.ConnectMsg) |
| 1551 |
c.Check(ok, Equals, true) |
| 1552 |
upCh <- nil // no error |
| 1553 |
upCh <- protocol.ConnAckMsg{ |
| 1554 |
Type: "connack", |
| 1555 |
Params: protocol.ConnAckParams{(10 * time.Millisecond).String()}, |
| 1556 |
} |
| 1557 |
// start is now done. |
| 1558 |
|
| 1559 |
// 3. "loop" |
| 1560 |
|
| 1561 |
// ping works, |
| 1562 |
c.Check(takeNext(downCh), Equals, fmt.Sprintf("deadline %v", dialTestTimeout+10*time.Millisecond)) |
| 1563 |
upCh <- protocol.PingPongMsg{Type: "ping"} |
| 1564 |
c.Check(takeNext(downCh), Equals, protocol.PingPongMsg{Type: "pong"}) |
| 1565 |
upCh <- nil |
| 1566 |
|
| 1567 |
// session would retry the same host |
| 1568 |
c.Check(sess.tryHost, Equals, 1) |
| 1569 |
|
| 1570 |
// and broadcasts... |
| 1571 |
b := &protocol.BroadcastMsg{ |
| 1572 |
Type: "broadcast", |
| 1573 |
AppId: "--ignored--", |
| 1574 |
ChanId: "0", |
| 1575 |
TopLevel: 2, |
| 1576 |
Payloads: []json.RawMessage{json.RawMessage(`{"b":1}`)}, |
| 1577 |
} |
| 1578 |
c.Check(takeNext(downCh), Equals, fmt.Sprintf("deadline %v", dialTestTimeout+10*time.Millisecond)) |
| 1579 |
upCh <- b |
| 1580 |
c.Check(takeNext(downCh), Equals, protocol.AckMsg{"ack"}) |
| 1581 |
upCh <- nil |
| 1582 |
// ...get bubbled up, |
| 1583 |
c.Check(<-sess.BroadcastCh, NotNil) |
| 1584 |
// and their TopLevel remembered |
| 1585 |
levels, err := sess.SeenState.GetAllLevels() |
| 1586 |
c.Check(err, IsNil) |
| 1587 |
c.Check(levels, DeepEquals, map[string]int64{"0": 2}) |
| 1588 |
|
| 1589 |
// and ping still work even after that. |
| 1590 |
c.Check(takeNext(downCh), Equals, fmt.Sprintf("deadline %v", dialTestTimeout+10*time.Millisecond)) |
| 1591 |
upCh <- protocol.PingPongMsg{Type: "ping"} |
| 1592 |
c.Check(takeNext(downCh), Equals, protocol.PingPongMsg{Type: "pong"}) |
| 1593 |
failure := errors.New("pongs") |
| 1594 |
upCh <- failure |
| 1595 |
c.Check(<-sess.errCh, Equals, failure) |
| 1596 |
} |
| 1597 |
|
| 1598 |
func (cs *clientSessionSuite) TestDialWorksDirect(c *C) { |
| 1599 |
// happy path thoughts |
| 1600 |
lst, err := tls.Listen("tcp", "localhost:0", helpers.TestTLSServerConfig) |
| 1601 |
c.Assert(err, IsNil) |
| 1602 |
sess, err := NewSession(lst.Addr().String(), dialTestConf(nil), "wah", cs.lvls, cs.log) |
| 1603 |
c.Assert(err, IsNil) |
| 1604 |
defer sess.StopKeepConnection() |
| 1605 |
|
| 1606 |
upCh := make(chan interface{}, 5) |
| 1607 |
downCh := make(chan interface{}, 5) |
| 1608 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1609 |
sess.Protocolator = func(net.Conn) protocol.Protocol { return proto } |
| 1610 |
|
| 1611 |
go sess.Dial() |
| 1612 |
|
| 1613 |
cli, err := lst.Accept() |
| 1614 |
c.Assert(err, IsNil) |
| 1615 |
cli.SetReadDeadline(time.Now().Add(2 * time.Second)) |
| 1616 |
var buf [1]byte |
| 1617 |
_, err = cli.Read(buf[:]) |
| 1618 |
c.Assert(err, IsNil) |
| 1619 |
// connect done |
| 1620 |
} |
| 1621 |
|
| 1622 |
func (cs *clientSessionSuite) TestDialWorksDirectSHA512Cert(c *C) { |
| 1623 |
// happy path thoughts |
| 1624 |
lst, err := tls.Listen("tcp", "localhost:0", helpers.TestTLSServerConfigs["sha512"]) |
| 1625 |
c.Assert(err, IsNil) |
| 1626 |
sess, err := NewSession(lst.Addr().String(), dialTestConf(helpers.TestCertPEMBlock512), "wah", cs.lvls, cs.log) |
| 1627 |
c.Assert(err, IsNil) |
| 1628 |
defer sess.StopKeepConnection() |
| 1629 |
|
| 1630 |
upCh := make(chan interface{}, 5) |
| 1631 |
downCh := make(chan interface{}, 5) |
| 1632 |
proto := &testProtocol{up: upCh, down: downCh} |
| 1633 |
sess.Protocolator = func(conn net.Conn) protocol.Protocol { |
| 1634 |
return proto |
| 1635 |
} |
| 1636 |
|
| 1637 |
go sess.Dial() |
| 1638 |
|
| 1639 |
cli, err := lst.Accept() |
| 1640 |
c.Assert(err, IsNil) |
| 1641 |
cli.SetReadDeadline(time.Now().Add(2 * time.Second)) |
| 1642 |
var buf [1]byte |
| 1643 |
_, err = cli.Read(buf[:]) |
| 1644 |
c.Assert(err, IsNil) |
| 1645 |
// connect done |
| 1646 |
} |
| 1647 |
|
| 1648 |
/**************************************************************** |
| 1649 |
redialDelay() tests |
| 1650 |
****************************************************************/ |
| 1651 |
|
| 1652 |
func (cs *clientSessionSuite) TestShouldDelay(c *C) { |
| 1653 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1654 |
c.Assert(err, IsNil) |
| 1655 |
c.Check(sess.ShouldDelay(), Equals, false) |
| 1656 |
sess.setShouldDelay() |
| 1657 |
c.Check(sess.ShouldDelay(), Equals, true) |
| 1658 |
sess.clearShouldDelay() |
| 1659 |
c.Check(sess.ShouldDelay(), Equals, false) |
| 1660 |
} |
| 1661 |
|
| 1662 |
func (cs *clientSessionSuite) TestRedialDelay(c *C) { |
| 1663 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1664 |
c.Assert(err, IsNil) |
| 1665 |
sess.redialDelays = []time.Duration{17, 42} |
| 1666 |
n := 0 |
| 1667 |
sess.redialJitter = func(time.Duration) time.Duration { n++; return 0 } |
| 1668 |
// we get increasing delays while we're unhappy |
| 1669 |
sess.setShouldDelay() |
| 1670 |
c.Check(redialDelay(sess), Equals, time.Duration(17)) |
| 1671 |
c.Check(redialDelay(sess), Equals, time.Duration(42)) |
| 1672 |
c.Check(redialDelay(sess), Equals, time.Duration(42)) |
| 1673 |
// once we're happy, delays drop to 0 |
| 1674 |
sess.clearShouldDelay() |
| 1675 |
c.Check(redialDelay(sess), Equals, time.Duration(0)) |
| 1676 |
// and start again from the top if we become unhappy again |
| 1677 |
sess.setShouldDelay() |
| 1678 |
c.Check(redialDelay(sess), Equals, time.Duration(17)) |
| 1679 |
// and redialJitter got called every time shouldDelay was true |
| 1680 |
c.Check(n, Equals, 4) |
| 1681 |
} |
| 1682 |
|
| 1683 |
/**************************************************************** |
| 1684 |
ResetCookie() tests |
| 1685 |
****************************************************************/ |
| 1686 |
|
| 1687 |
func (cs *clientSessionSuite) TestResetCookie(c *C) { |
| 1688 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1689 |
c.Assert(err, IsNil) |
| 1690 |
c.Assert(sess.KeepConnection(), IsNil) |
| 1691 |
defer sess.StopKeepConnection() |
| 1692 |
c.Check(sess.getCookie(), Equals, "") |
| 1693 |
sess.setCookie("COOKIE") |
| 1694 |
c.Check(sess.getCookie(), Equals, "COOKIE") |
| 1695 |
sess.ResetCookie() |
| 1696 |
c.Check(sess.getCookie(), Equals, "") |
| 1697 |
} |
| 1698 |
|
| 1699 |
/**************************************************************** |
| 1700 |
KeepConnection() (and related) tests |
| 1701 |
****************************************************************/ |
| 1702 |
|
| 1703 |
func (cs *clientSessionSuite) TestKeepConnectionDoesNothingIfNotConnected(c *C) { |
| 1704 |
// how do you test "does nothing?" |
| 1705 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1706 |
c.Assert(err, IsNil) |
| 1707 |
c.Assert(sess, NotNil) |
| 1708 |
c.Assert(sess.State(), Equals, Pristine) |
| 1709 |
c.Assert(sess.KeepConnection(), IsNil) |
| 1710 |
defer sess.StopKeepConnection() |
| 1711 |
// stopCh is meant to be used just for closing it, but abusing |
| 1712 |
// it for testing seems the right thing to do: this ensures |
| 1713 |
// the thing is ticking along before we check the state of |
| 1714 |
// stuff. |
| 1715 |
sess.stopCh <- struct{}{} |
| 1716 |
c.Check(sess.State(), Equals, Disconnected) |
| 1717 |
} |
| 1718 |
|
| 1719 |
func (cs *clientSessionSuite) TestYouCantCallKeepConnectionTwice(c *C) { |
| 1720 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1721 |
c.Assert(err, IsNil) |
| 1722 |
c.Assert(sess, NotNil) |
| 1723 |
c.Assert(sess.State(), Equals, Pristine) |
| 1724 |
c.Assert(sess.KeepConnection(), IsNil) |
| 1725 |
defer sess.StopKeepConnection() |
| 1726 |
c.Check(sess.KeepConnection(), NotNil) |
| 1727 |
} |
| 1728 |
|
| 1729 |
func (cs *clientSessionSuite) TestStopKeepConnectionShutsdown(c *C) { |
| 1730 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1731 |
c.Assert(err, IsNil) |
| 1732 |
c.Assert(sess, NotNil) |
| 1733 |
sess.StopKeepConnection() |
| 1734 |
c.Check(sess.State(), Equals, Shutdown) |
| 1735 |
} |
| 1736 |
|
| 1737 |
func (cs *clientSessionSuite) TestHasConnectivityTriggersConnectivityHandler(c *C) { |
| 1738 |
sess, err := NewSession("foo:443", dummyConf(), "", cs.lvls, cs.log) |
| 1739 |
c.Assert(err, IsNil) |
| 1740 |
c.Assert(sess, NotNil) |
| 1741 |
testCh := make(chan bool) |
| 1742 |
sess.connHandler = func(p bool) { testCh <- p } |
| 1743 |
go sess.doKeepConnection() |
| 1744 |
defer sess.StopKeepConnection() |
| 1745 |
sess.HasConnectivity(true) |
| 1746 |
c.Check(<-testCh, Equals, true) |
| 1747 |
sess.HasConnectivity(false) |
| 1748 |
c.Check(<-testCh, Equals, false) |
| 1749 |
} |
| 1750 |
|
| 1751 |
func (cs *clientSessionSuite) TestDoneChIsEmptiedAndLogged(c *C) { |
| 1752 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1753 |
c.Assert(err, IsNil) |
| 1754 |
sess.doneCh = make(chan uint32) // unbuffered |
| 1755 |
|
| 1756 |
sess.KeepConnection() |
| 1757 |
defer sess.StopKeepConnection() |
| 1758 |
|
| 1759 |
sess.doneCh <- 23 |
| 1760 |
sess.doneCh <- 24 // makes sure the first one has been processed before checking |
| 1761 |
|
| 1762 |
c.Check(cs.log.Captured(), |
| 1763 |
Matches, `(?ms).* connected after 23 attempts\.`) |
| 1764 |
} |
| 1765 |
|
| 1766 |
func (cs *clientSessionSuite) TestErrChIsEmptiedAndLoggedAndAutoRedial(c *C) { |
| 1767 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1768 |
c.Assert(err, IsNil) |
| 1769 |
ch := make(chan struct{}, 1) |
| 1770 |
sess.errCh = make(chan error) // unbuffered |
| 1771 |
sess.redialDelay = func(sess *clientSession) time.Duration { ch <- struct{}{}; return 0 } |
| 1772 |
sess.lastConn = true // -> autoRedial, if the session is in Disconnected |
| 1773 |
|
| 1774 |
sess.KeepConnection() |
| 1775 |
defer sess.StopKeepConnection() |
| 1776 |
|
| 1777 |
sess.setState(Error) |
| 1778 |
sess.errCh <- errors.New("potato") |
| 1779 |
select { |
| 1780 |
case <-ch: |
| 1781 |
// all ok |
| 1782 |
case <-time.After(100 * time.Millisecond): |
| 1783 |
c.Fatalf("redialDelay not called (-> autoRedial not called)?") |
| 1784 |
} |
| 1785 |
|
| 1786 |
c.Check(cs.log.Captured(), |
| 1787 |
Matches, `(?ms).* session error.*potato`) |
| 1788 |
} |
| 1789 |
|
| 1790 |
func (cs *clientSessionSuite) TestErrChIsEmptiedAndLoggedNoAutoRedial(c *C) { |
| 1791 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1792 |
c.Assert(err, IsNil) |
| 1793 |
ch := make(chan struct{}, 1) |
| 1794 |
sess.errCh = make(chan error) // unbuffered |
| 1795 |
sess.redialDelay = func(sess *clientSession) time.Duration { ch <- struct{}{}; return 0 } |
| 1796 |
sess.connHandler = func(bool) {} |
| 1797 |
sess.lastConn = false // so, no autoredial |
| 1798 |
|
| 1799 |
sess.KeepConnection() |
| 1800 |
defer sess.StopKeepConnection() |
| 1801 |
|
| 1802 |
sess.errCh <- errors.New("potato") |
| 1803 |
c.Assert(sess.State(), Equals, Disconnected) |
| 1804 |
select { |
| 1805 |
case <-ch: |
| 1806 |
c.Fatalf("redialDelay called (-> autoRedial called) when disconnected?") |
| 1807 |
case <-time.After(100 * time.Millisecond): |
| 1808 |
// all ok |
| 1809 |
} |
| 1810 |
|
| 1811 |
c.Check(cs.log.Captured(), |
| 1812 |
Matches, `(?ms).* session error.*potato`) |
| 1813 |
} |
| 1814 |
|
| 1815 |
func (cs *clientSessionSuite) TestHandleConnConnFromConnected(c *C) { |
| 1816 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1817 |
c.Assert(err, IsNil) |
| 1818 |
ch := make(chan struct{}, 1) |
| 1819 |
sess.redialDelay = func(sess *clientSession) time.Duration { ch <- struct{}{}; return 0 } |
| 1820 |
sess.state = Connected |
| 1821 |
sess.lastConn = true |
| 1822 |
sess.handleConn(true) |
| 1823 |
c.Check(sess.lastConn, Equals, true) |
| 1824 |
|
| 1825 |
select { |
| 1826 |
case <-ch: |
| 1827 |
// all ok |
| 1828 |
case <-time.After(100 * time.Millisecond): |
| 1829 |
c.Fatalf("redialDelay not called (-> autoRedial not called)?") |
| 1830 |
} |
| 1831 |
} |
| 1832 |
|
| 1833 |
func (cs *clientSessionSuite) TestHandleConnConnFromDisconnected(c *C) { |
| 1834 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1835 |
c.Assert(err, IsNil) |
| 1836 |
ch := make(chan struct{}, 1) |
| 1837 |
sess.redialDelay = func(sess *clientSession) time.Duration { ch <- struct{}{}; return 0 } |
| 1838 |
sess.state = Disconnected |
| 1839 |
sess.lastConn = false |
| 1840 |
sess.handleConn(true) |
| 1841 |
c.Check(sess.lastConn, Equals, true) |
| 1842 |
|
| 1843 |
select { |
| 1844 |
case <-ch: |
| 1845 |
// all ok |
| 1846 |
case <-time.After(100 * time.Millisecond): |
| 1847 |
c.Fatalf("redialDelay not called (-> autoRedial not called)?") |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
func (cs *clientSessionSuite) TestHandleConnNotConnFromDisconnected(c *C) { |
| 1852 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1853 |
c.Assert(err, IsNil) |
| 1854 |
ch := make(chan struct{}, 1) |
| 1855 |
sess.redialDelay = func(sess *clientSession) time.Duration { ch <- struct{}{}; return 0 } |
| 1856 |
sess.state = Disconnected |
| 1857 |
sess.lastConn = false |
| 1858 |
sess.handleConn(false) |
| 1859 |
c.Check(sess.lastConn, Equals, false) |
| 1860 |
|
| 1861 |
select { |
| 1862 |
case <-ch: |
| 1863 |
c.Fatalf("redialDelay called (-> autoRedial called)?") |
| 1864 |
case <-time.After(100 * time.Millisecond): |
| 1865 |
// all ok |
| 1866 |
} |
| 1867 |
c.Check(cs.log.Captured(), Matches, `(?ms).*-> Disconnected`) |
| 1868 |
} |
| 1869 |
|
| 1870 |
func (cs *clientSessionSuite) TestHandleConnNotConnFromConnected(c *C) { |
| 1871 |
sess, err := NewSession("", dummyConf(), "wah", cs.lvls, cs.log) |
| 1872 |
c.Assert(err, IsNil) |
| 1873 |
ch := make(chan struct{}, 1) |
| 1874 |
sess.redialDelay = func(sess *clientSession) time.Duration { ch <- struct{}{}; return 0 } |
| 1875 |
sess.state = Connected |
| 1876 |
sess.lastConn = true |
| 1877 |
sess.handleConn(false) |
| 1878 |
c.Check(sess.lastConn, Equals, false) |
| 1879 |
|
| 1880 |
select { |
| 1881 |
case <-ch: |
| 1882 |
c.Fatalf("redialDelay called (-> autoRedial called)?") |
| 1883 |
case <-time.After(100 * time.Millisecond): |
| 1884 |
// all ok |
| 1885 |
} |
| 1886 |
c.Check(cs.log.Captured(), Matches, `(?ms).*-> Disconnected`) |
| 1887 |
} |