| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.net.BrowserChannelTest'); |
| 16 |
goog.setTestOnly('goog.net.BrowserChannelTest'); |
| 17 |
|
| 18 |
goog.require('goog.Timer'); |
| 19 |
goog.require('goog.array'); |
| 20 |
goog.require('goog.dom'); |
| 21 |
goog.require('goog.functions'); |
| 22 |
goog.require('goog.json'); |
| 23 |
goog.require('goog.net.BrowserChannel'); |
| 24 |
goog.require('goog.net.ChannelDebug'); |
| 25 |
goog.require('goog.net.ChannelRequest'); |
| 26 |
goog.require('goog.net.tmpnetwork'); |
| 27 |
goog.require('goog.structs.Map'); |
| 28 |
goog.require('goog.testing.MockClock'); |
| 29 |
goog.require('goog.testing.PropertyReplacer'); |
| 30 |
goog.require('goog.testing.asserts'); |
| 31 |
goog.require('goog.testing.jsunit'); |
| 32 |
goog.require('goog.testing.recordFunction'); |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Delay between a network failure and the next network request. |
| 37 |
*/ |
| 38 |
var RETRY_TIME = 1000; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* A really long time - used to make sure no more timeouts will fire. |
| 43 |
*/ |
| 44 |
var ALL_DAY_MS = 1000 * 60 * 60 * 24; |
| 45 |
|
| 46 |
var stubs = new goog.testing.PropertyReplacer(); |
| 47 |
|
| 48 |
var browserChannel; |
| 49 |
var deliveredMaps; |
| 50 |
var handler; |
| 51 |
var mockClock; |
| 52 |
var gotError; |
| 53 |
var numStatEvents; |
| 54 |
var lastStatEvent; |
| 55 |
var numTimingEvents; |
| 56 |
var lastPostSize; |
| 57 |
var lastPostRtt; |
| 58 |
var lastPostRetryCount; |
| 59 |
|
| 60 |
// Set to true to see the channel debug output in the browser window. |
| 61 |
var debug = false; |
| 62 |
// Debug message to print out when debug is true. |
| 63 |
var debugMessage = ''; |
| 64 |
|
| 65 |
function debugToWindow(message) { |
| 66 |
if (debug) { |
| 67 |
debugMessage += message + '<br>'; |
| 68 |
goog.dom.getElement('debug').innerHTML = debugMessage; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Stubs goog.net.tmpnetwork to always time out. It maintains the |
| 75 |
* contract given by goog.net.tmpnetwork.testGoogleCom, but always |
| 76 |
* times out (calling callback(false)). |
| 77 |
* |
| 78 |
* stubTmpnetwork should be called in tests that require it before |
| 79 |
* a call to testGoogleCom happens. It is reset at tearDown. |
| 80 |
*/ |
| 81 |
function stubTmpnetwork() { |
| 82 |
stubs.set( |
| 83 |
goog.net.tmpnetwork, 'testLoadImage', function(url, timeout, callback) { |
| 84 |
goog.Timer.callOnce(goog.partial(callback, false), timeout); |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Mock ChannelRequest. |
| 92 |
* @constructor |
| 93 |
*/ |
| 94 |
var MockChannelRequest = function( |
| 95 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) { |
| 96 |
this.channel_ = channel; |
| 97 |
this.channelDebug_ = channelDebug; |
| 98 |
this.sessionId_ = opt_sessionId; |
| 99 |
this.requestId_ = opt_requestId; |
| 100 |
this.successful_ = true; |
| 101 |
this.lastError_ = null; |
| 102 |
this.lastStatusCode_ = 200; |
| 103 |
|
| 104 |
// For debugging, keep track of whether this is a back or forward channel. |
| 105 |
this.isBack = !!(opt_requestId == 'rpc'); |
| 106 |
this.isForward = !this.isBack; |
| 107 |
}; |
| 108 |
|
| 109 |
MockChannelRequest.prototype.postData_ = null; |
| 110 |
|
| 111 |
MockChannelRequest.prototype.requestStartTime_ = null; |
| 112 |
|
| 113 |
MockChannelRequest.prototype.requestUri_ = null; |
| 114 |
|
| 115 |
MockChannelRequest.prototype.setExtraHeaders = function(extraHeaders) {}; |
| 116 |
|
| 117 |
MockChannelRequest.prototype.setTimeout = function(timeout) {}; |
| 118 |
|
| 119 |
MockChannelRequest.prototype.setReadyStateChangeThrottle = function(throttle) { |
| 120 |
}; |
| 121 |
|
| 122 |
MockChannelRequest.prototype.xmlHttpPost = function( |
| 123 |
uri, postData, decodeChunks) { |
| 124 |
this.channelDebug_.debug( |
| 125 |
'---> POST: ' + uri + ', ' + postData + ', ' + decodeChunks); |
| 126 |
this.postData_ = postData; |
| 127 |
this.requestStartTime_ = goog.now(); |
| 128 |
}; |
| 129 |
|
| 130 |
MockChannelRequest.prototype.xmlHttpGet = function( |
| 131 |
uri, decodeChunks, opt_noClose) { |
| 132 |
this.channelDebug_.debug( |
| 133 |
'<--- GET: ' + uri + ', ' + decodeChunks + ', ' + opt_noClose); |
| 134 |
this.requestUri_ = uri; |
| 135 |
this.requestStartTime_ = goog.now(); |
| 136 |
}; |
| 137 |
|
| 138 |
MockChannelRequest.prototype.tridentGet = function(uri, usingSecondaryDomain) { |
| 139 |
this.channelDebug_.debug('<---GET (T): ' + uri); |
| 140 |
this.requestUri_ = uri; |
| 141 |
this.requestStartTime_ = goog.now(); |
| 142 |
}; |
| 143 |
|
| 144 |
MockChannelRequest.prototype.sendUsingImgTag = function(uri) { |
| 145 |
this.requestStartTime_ = goog.now(); |
| 146 |
}; |
| 147 |
|
| 148 |
MockChannelRequest.prototype.cancel = function() { |
| 149 |
this.successful_ = false; |
| 150 |
}; |
| 151 |
|
| 152 |
MockChannelRequest.prototype.getSuccess = function() { |
| 153 |
return this.successful_; |
| 154 |
}; |
| 155 |
|
| 156 |
MockChannelRequest.prototype.getLastError = function() { |
| 157 |
return this.lastError_; |
| 158 |
}; |
| 159 |
|
| 160 |
MockChannelRequest.prototype.getLastStatusCode = function() { |
| 161 |
return this.lastStatusCode_; |
| 162 |
}; |
| 163 |
|
| 164 |
MockChannelRequest.prototype.getSessionId = function() { |
| 165 |
return this.sessionId_; |
| 166 |
}; |
| 167 |
|
| 168 |
MockChannelRequest.prototype.getRequestId = function() { |
| 169 |
return this.requestId_; |
| 170 |
}; |
| 171 |
|
| 172 |
MockChannelRequest.prototype.getPostData = function() { |
| 173 |
return this.postData_; |
| 174 |
}; |
| 175 |
|
| 176 |
MockChannelRequest.prototype.getRequestStartTime = function() { |
| 177 |
return this.requestStartTime_; |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
function setUpPage() { |
| 182 |
// Use our MockChannelRequests instead of the real ones. |
| 183 |
goog.net.BrowserChannel.createChannelRequest = function( |
| 184 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) { |
| 185 |
return new MockChannelRequest( |
| 186 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId); |
| 187 |
}; |
| 188 |
|
| 189 |
// Mock out the stat notification code. |
| 190 |
goog.net.BrowserChannel.notifyStatEvent = function(stat) { |
| 191 |
numStatEvents++; |
| 192 |
lastStatEvent = stat; |
| 193 |
}; |
| 194 |
|
| 195 |
goog.net.BrowserChannel.notifyTimingEvent = function(size, rtt, retries) { |
| 196 |
numTimingEvents++; |
| 197 |
lastPostSize = size; |
| 198 |
lastPostRtt = rtt; |
| 199 |
lastPostRetryCount = retries; |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
function setUp() { |
| 205 |
numTimingEvents = 0; |
| 206 |
lastPostSize = null; |
| 207 |
lastPostRtt = null; |
| 208 |
lastPostRetryCount = null; |
| 209 |
|
| 210 |
mockClock = new goog.testing.MockClock(true); |
| 211 |
browserChannel = new goog.net.BrowserChannel('1'); |
| 212 |
gotError = false; |
| 213 |
|
| 214 |
handler = new goog.net.BrowserChannel.Handler(); |
| 215 |
handler.channelOpened = function() {}; |
| 216 |
handler.channelError = function(channel, error) { gotError = true; }; |
| 217 |
handler.channelSuccess = function(channel, maps) { |
| 218 |
deliveredMaps = goog.array.clone(maps); |
| 219 |
}; |
| 220 |
handler.channelClosed = function( |
| 221 |
channel, opt_pendingMaps, opt_undeliveredMaps) { |
| 222 |
// Mock out the handler, and let it set a formatted user readable string |
| 223 |
// of the undelivered maps which we can use when verifying our assertions. |
| 224 |
if (opt_pendingMaps) { |
| 225 |
this.pendingMapsString = formatArrayOfMaps(opt_pendingMaps); |
| 226 |
} |
| 227 |
if (opt_undeliveredMaps) { |
| 228 |
this.undeliveredMapsString = formatArrayOfMaps(opt_undeliveredMaps); |
| 229 |
} |
| 230 |
}; |
| 231 |
handler.channelHandleMultipleArrays = function() {}; |
| 232 |
handler.channelHandleArray = function() {}; |
| 233 |
|
| 234 |
browserChannel.setHandler(handler); |
| 235 |
|
| 236 |
// Provide a predictable retry time for testing. |
| 237 |
browserChannel.getRetryTime_ = function(retryCount) { return RETRY_TIME; }; |
| 238 |
|
| 239 |
var channelDebug = new goog.net.ChannelDebug(); |
| 240 |
channelDebug.debug = function(message) { debugToWindow(message); }; |
| 241 |
browserChannel.setChannelDebug(channelDebug); |
| 242 |
|
| 243 |
numStatEvents = 0; |
| 244 |
lastStatEvent = null; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
function tearDown() { |
| 249 |
mockClock.dispose(); |
| 250 |
stubs.reset(); |
| 251 |
debugToWindow('<hr>'); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
/** |
| 256 |
* Helper function to return a formatted string representing an array of maps. |
| 257 |
*/ |
| 258 |
function formatArrayOfMaps(arrayOfMaps) { |
| 259 |
var result = []; |
| 260 |
for (var i = 0; i < arrayOfMaps.length; i++) { |
| 261 |
var map = arrayOfMaps[i]; |
| 262 |
var keys = map.map.getKeys(); |
| 263 |
for (var j = 0; j < keys.length; j++) { |
| 264 |
var tmp = keys[j] + ':' + map.map.get(keys[j]) + |
| 265 |
(map.context ? ':' + map.context : ''); |
| 266 |
result.push(tmp); |
| 267 |
} |
| 268 |
} |
| 269 |
return result.join(', '); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
function testFormatArrayOfMaps() { |
| 274 |
// This function is used in a non-trivial test, so let's verify that it works. |
| 275 |
var map1 = new goog.structs.Map(); |
| 276 |
map1.set('k1', 'v1'); |
| 277 |
map1.set('k2', 'v2'); |
| 278 |
var map2 = new goog.structs.Map(); |
| 279 |
map2.set('k3', 'v3'); |
| 280 |
var map3 = new goog.structs.Map(); |
| 281 |
map3.set('k4', 'v4'); |
| 282 |
map3.set('k5', 'v5'); |
| 283 |
map3.set('k6', 'v6'); |
| 284 |
|
| 285 |
// One map. |
| 286 |
var a = []; |
| 287 |
a.push(new goog.net.BrowserChannel.QueuedMap(0, map1)); |
| 288 |
assertEquals('k1:v1, k2:v2', formatArrayOfMaps(a)); |
| 289 |
|
| 290 |
// Many maps. |
| 291 |
var b = []; |
| 292 |
b.push(new goog.net.BrowserChannel.QueuedMap(0, map1)); |
| 293 |
b.push(new goog.net.BrowserChannel.QueuedMap(0, map2)); |
| 294 |
b.push(new goog.net.BrowserChannel.QueuedMap(0, map3)); |
| 295 |
assertEquals( |
| 296 |
'k1:v1, k2:v2, k3:v3, k4:v4, k5:v5, k6:v6', formatArrayOfMaps(b)); |
| 297 |
|
| 298 |
// One map with a context. |
| 299 |
var c = []; |
| 300 |
c.push(new goog.net.BrowserChannel.QueuedMap(0, map1, 'c1')); |
| 301 |
assertEquals('k1:v1:c1, k2:v2:c1', formatArrayOfMaps(c)); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
function connectForwardChannel( |
| 306 |
opt_serverVersion, opt_hostPrefix, opt_uriPrefix) { |
| 307 |
var uriPrefix = opt_uriPrefix || ''; |
| 308 |
browserChannel.connect(uriPrefix + '/test', uriPrefix + '/bind', null); |
| 309 |
mockClock.tick(0); |
| 310 |
completeTestConnection(); |
| 311 |
completeForwardChannel(opt_serverVersion, opt_hostPrefix); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
function connect(opt_serverVersion, opt_hostPrefix, opt_uriPrefix) { |
| 316 |
connectForwardChannel(opt_serverVersion, opt_hostPrefix, opt_uriPrefix); |
| 317 |
completeBackChannel(); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
function disconnect() { |
| 322 |
browserChannel.disconnect(); |
| 323 |
mockClock.tick(0); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
function completeTestConnection() { |
| 328 |
completeForwardTestConnection(); |
| 329 |
completeBackTestConnection(); |
| 330 |
assertEquals( |
| 331 |
goog.net.BrowserChannel.State.OPENING, browserChannel.getState()); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
function completeForwardTestConnection() { |
| 336 |
browserChannel.connectionTest_.onRequestData( |
| 337 |
browserChannel.connectionTest_, '["b"]'); |
| 338 |
browserChannel.connectionTest_.onRequestComplete( |
| 339 |
browserChannel.connectionTest_); |
| 340 |
mockClock.tick(0); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
function completeBackTestConnection() { |
| 345 |
browserChannel.connectionTest_.onRequestData( |
| 346 |
browserChannel.connectionTest_, '11111'); |
| 347 |
mockClock.tick(0); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
function completeForwardChannel(opt_serverVersion, opt_hostPrefix) { |
| 352 |
var responseData = '[[0,["c","1234567890ABCDEF",' + |
| 353 |
(opt_hostPrefix ? '"' + opt_hostPrefix + '"' : 'null') + |
| 354 |
(opt_serverVersion ? ',' + opt_serverVersion : '') + ']]]'; |
| 355 |
browserChannel.onRequestData( |
| 356 |
browserChannel.forwardChannelRequest_, responseData); |
| 357 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 358 |
mockClock.tick(0); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
function completeBackChannel() { |
| 363 |
browserChannel.onRequestData( |
| 364 |
browserChannel.backChannelRequest_, '[[1,["foo"]]]'); |
| 365 |
browserChannel.onRequestComplete(browserChannel.backChannelRequest_); |
| 366 |
mockClock.tick(0); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
function responseVersion7() { |
| 371 |
browserChannel.onRequestData( |
| 372 |
browserChannel.forwardChannelRequest_, |
| 373 |
goog.net.ChannelDebug.MAGIC_RESPONSE_COOKIE); |
| 374 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 375 |
mockClock.tick(0); |
| 376 |
} |
| 377 |
|
| 378 |
function responseNoBackchannel(lastArrayIdSentFromServer, outstandingDataSize) { |
| 379 |
responseData = |
| 380 |
goog.json.serialize([0, lastArrayIdSentFromServer, outstandingDataSize]); |
| 381 |
browserChannel.onRequestData( |
| 382 |
browserChannel.forwardChannelRequest_, responseData); |
| 383 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 384 |
mockClock.tick(0); |
| 385 |
} |
| 386 |
|
| 387 |
function response(lastArrayIdSentFromServer, outstandingDataSize) { |
| 388 |
responseData = |
| 389 |
goog.json.serialize([1, lastArrayIdSentFromServer, outstandingDataSize]); |
| 390 |
browserChannel.onRequestData( |
| 391 |
browserChannel.forwardChannelRequest_, responseData); |
| 392 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 393 |
mockClock.tick(0); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
function receive(data) { |
| 398 |
browserChannel.onRequestData( |
| 399 |
browserChannel.backChannelRequest_, '[[1,' + data + ']]'); |
| 400 |
browserChannel.onRequestComplete(browserChannel.backChannelRequest_); |
| 401 |
mockClock.tick(0); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
function responseTimeout() { |
| 406 |
browserChannel.forwardChannelRequest_lastError_ = |
| 407 |
goog.net.ChannelRequest.Error.TIMEOUT; |
| 408 |
browserChannel.forwardChannelRequest_.successful_ = false; |
| 409 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 410 |
mockClock.tick(0); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
function responseRequestFailed(opt_statusCode) { |
| 415 |
browserChannel.forwardChannelRequest_.lastError_ = |
| 416 |
goog.net.ChannelRequest.Error.STATUS; |
| 417 |
browserChannel.forwardChannelRequest_.lastStatusCode_ = opt_statusCode || 503; |
| 418 |
browserChannel.forwardChannelRequest_.successful_ = false; |
| 419 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 420 |
mockClock.tick(0); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
function responseUnknownSessionId() { |
| 425 |
browserChannel.forwardChannelRequest_.lastError_ = |
| 426 |
goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID; |
| 427 |
browserChannel.forwardChannelRequest_.successful_ = false; |
| 428 |
browserChannel.onRequestComplete(browserChannel.forwardChannelRequest_); |
| 429 |
mockClock.tick(0); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
function responseActiveXBlocked() { |
| 434 |
browserChannel.backChannelRequest_.lastError_ = |
| 435 |
goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED; |
| 436 |
browserChannel.backChannelRequest_.successful_ = false; |
| 437 |
browserChannel.onRequestComplete(browserChannel.backChannelRequest_); |
| 438 |
mockClock.tick(0); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
function sendMap(key, value, opt_context) { |
| 443 |
var map = new goog.structs.Map(); |
| 444 |
map.set(key, value); |
| 445 |
browserChannel.sendMap(map, opt_context); |
| 446 |
mockClock.tick(0); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
function hasForwardChannel() { |
| 451 |
return !!browserChannel.forwardChannelRequest_; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
function hasBackChannel() { |
| 456 |
return !!browserChannel.backChannelRequest_; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
function hasDeadBackChannelTimer() { |
| 461 |
return goog.isDefAndNotNull(browserChannel.deadBackChannelTimerId_); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
function assertHasForwardChannel() { |
| 466 |
assertTrue('Forward channel missing.', hasForwardChannel()); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
function assertHasBackChannel() { |
| 471 |
assertTrue('Back channel missing.', hasBackChannel()); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
function testConnect() { |
| 476 |
connect(); |
| 477 |
assertEquals(goog.net.BrowserChannel.State.OPENED, browserChannel.getState()); |
| 478 |
// If the server specifies no version, the client assumes 6 |
| 479 |
assertEquals(6, browserChannel.channelVersion_); |
| 480 |
assertFalse(browserChannel.isBuffered()); |
| 481 |
} |
| 482 |
|
| 483 |
function testConnect_backChannelEstablished() { |
| 484 |
connect(); |
| 485 |
assertHasBackChannel(); |
| 486 |
} |
| 487 |
|
| 488 |
function testConnect_withServerHostPrefix() { |
| 489 |
connect(undefined, 'serverHostPrefix'); |
| 490 |
assertEquals('serverHostPrefix', browserChannel.hostPrefix_); |
| 491 |
} |
| 492 |
|
| 493 |
function testConnect_withClientHostPrefix() { |
| 494 |
handler.correctHostPrefix = function(hostPrefix) { |
| 495 |
return 'clientHostPrefix'; |
| 496 |
}; |
| 497 |
connect(); |
| 498 |
assertEquals('clientHostPrefix', browserChannel.hostPrefix_); |
| 499 |
} |
| 500 |
|
| 501 |
function testConnect_overrideServerHostPrefix() { |
| 502 |
handler.correctHostPrefix = function(hostPrefix) { |
| 503 |
return 'clientHostPrefix'; |
| 504 |
}; |
| 505 |
connect(undefined, 'serverHostPrefix'); |
| 506 |
assertEquals('clientHostPrefix', browserChannel.hostPrefix_); |
| 507 |
} |
| 508 |
|
| 509 |
function testConnect_withServerVersion() { |
| 510 |
connect(8); |
| 511 |
assertEquals(8, browserChannel.channelVersion_); |
| 512 |
} |
| 513 |
|
| 514 |
function testConnect_notOkToMakeRequestForTest() { |
| 515 |
handler.okToMakeRequest = |
| 516 |
goog.functions.constant(goog.net.BrowserChannel.Error.NETWORK); |
| 517 |
browserChannel.connect('/test', '/bind', null); |
| 518 |
mockClock.tick(0); |
| 519 |
assertEquals(goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 520 |
} |
| 521 |
|
| 522 |
function testConnect_notOkToMakeRequestForBind() { |
| 523 |
browserChannel.connect('/test', '/bind', null); |
| 524 |
mockClock.tick(0); |
| 525 |
completeTestConnection(); |
| 526 |
handler.okToMakeRequest = |
| 527 |
goog.functions.constant(goog.net.BrowserChannel.Error.NETWORK); |
| 528 |
completeForwardChannel(); |
| 529 |
assertEquals(goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
function testSendMap() { |
| 534 |
connect(); |
| 535 |
assertEquals(1, numTimingEvents); |
| 536 |
sendMap('foo', 'bar'); |
| 537 |
responseVersion7(); |
| 538 |
assertEquals(2, numTimingEvents); |
| 539 |
assertEquals('foo:bar', formatArrayOfMaps(deliveredMaps)); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
function testSendMap_twice() { |
| 544 |
connect(); |
| 545 |
sendMap('foo1', 'bar1'); |
| 546 |
responseVersion7(); |
| 547 |
assertEquals('foo1:bar1', formatArrayOfMaps(deliveredMaps)); |
| 548 |
sendMap('foo2', 'bar2'); |
| 549 |
responseVersion7(); |
| 550 |
assertEquals('foo2:bar2', formatArrayOfMaps(deliveredMaps)); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
function testSendMap_andReceive() { |
| 555 |
connect(); |
| 556 |
sendMap('foo', 'bar'); |
| 557 |
responseVersion7(); |
| 558 |
receive('["the server reply"]'); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
function testReceive() { |
| 563 |
connect(); |
| 564 |
receive('["message from server"]'); |
| 565 |
assertHasBackChannel(); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
function testReceive_twice() { |
| 570 |
connect(); |
| 571 |
receive('["message one from server"]'); |
| 572 |
receive('["message two from server"]'); |
| 573 |
assertHasBackChannel(); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
function testReceive_andSendMap() { |
| 578 |
connect(); |
| 579 |
receive('["the server reply"]'); |
| 580 |
sendMap('foo', 'bar'); |
| 581 |
responseVersion7(); |
| 582 |
assertHasBackChannel(); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
function testBackChannelRemainsEstablished_afterSingleSendMap() { |
| 587 |
connect(); |
| 588 |
|
| 589 |
sendMap('foo', 'bar'); |
| 590 |
responseVersion7(); |
| 591 |
receive('["ack"]'); |
| 592 |
|
| 593 |
assertHasBackChannel(); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
function testBackChannelRemainsEstablished_afterDoubleSendMap() { |
| 598 |
connect(); |
| 599 |
|
| 600 |
sendMap('foo1', 'bar1'); |
| 601 |
sendMap('foo2', 'bar2'); |
| 602 |
responseVersion7(); |
| 603 |
receive('["ack"]'); |
| 604 |
|
| 605 |
// This assertion would fail prior to CL 13302660. |
| 606 |
assertHasBackChannel(); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
function testTimingEvent() { |
| 611 |
connect(); |
| 612 |
assertEquals(1, numTimingEvents); |
| 613 |
sendMap('', ''); |
| 614 |
assertEquals(1, numTimingEvents); |
| 615 |
mockClock.tick(20); |
| 616 |
var expSize = browserChannel.forwardChannelRequest_.getPostData().length; |
| 617 |
responseVersion7(); |
| 618 |
|
| 619 |
assertEquals(2, numTimingEvents); |
| 620 |
assertEquals(expSize, lastPostSize); |
| 621 |
assertEquals(20, lastPostRtt); |
| 622 |
assertEquals(0, lastPostRetryCount); |
| 623 |
|
| 624 |
sendMap('abcdefg', '123456'); |
| 625 |
expSize = browserChannel.forwardChannelRequest_.getPostData().length; |
| 626 |
responseTimeout(); |
| 627 |
assertEquals(2, numTimingEvents); |
| 628 |
mockClock.tick(RETRY_TIME + 1); |
| 629 |
responseVersion7(); |
| 630 |
assertEquals(3, numTimingEvents); |
| 631 |
assertEquals(expSize, lastPostSize); |
| 632 |
assertEquals(1, lastPostRetryCount); |
| 633 |
assertEquals(1, lastPostRtt); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* Make sure that dropping the forward channel retry limit below the retry count |
| 639 |
* reports an error, and prevents another request from firing. |
| 640 |
*/ |
| 641 |
function testSetFailFastWhileWaitingForRetry() { |
| 642 |
stubTmpnetwork(); |
| 643 |
|
| 644 |
connect(); |
| 645 |
assertEquals(1, numTimingEvents); |
| 646 |
|
| 647 |
sendMap('foo', 'bar'); |
| 648 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 649 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 650 |
assertEquals(0, browserChannel.forwardChannelRetryCount_); |
| 651 |
|
| 652 |
// Watchdog timeout. |
| 653 |
responseTimeout(); |
| 654 |
assertNotNull(browserChannel.forwardChannelTimerId_); |
| 655 |
assertNull(browserChannel.forwardChannelRequest_); |
| 656 |
assertEquals(1, browserChannel.forwardChannelRetryCount_); |
| 657 |
|
| 658 |
// Almost finish the between-retry timeout. |
| 659 |
mockClock.tick(RETRY_TIME - 1); |
| 660 |
assertNotNull(browserChannel.forwardChannelTimerId_); |
| 661 |
assertNull(browserChannel.forwardChannelRequest_); |
| 662 |
assertEquals(1, browserChannel.forwardChannelRetryCount_); |
| 663 |
|
| 664 |
// Setting max retries to 0 should cancel the timer and raise an error. |
| 665 |
browserChannel.setFailFast(true); |
| 666 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 667 |
assertNull(browserChannel.forwardChannelRequest_); |
| 668 |
assertEquals(1, browserChannel.forwardChannelRetryCount_); |
| 669 |
|
| 670 |
assertTrue(gotError); |
| 671 |
assertEquals(0, deliveredMaps.length); |
| 672 |
// We get the error immediately before starting to ping google.com. |
| 673 |
// Simulate that timing out. We should get a network error in addition to the |
| 674 |
// initial failure. |
| 675 |
gotError = false; |
| 676 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 677 |
assertTrue('No error after tmpnetwork ping timed out.', gotError); |
| 678 |
|
| 679 |
// Make sure no more retry timers are firing. |
| 680 |
mockClock.tick(ALL_DAY_MS); |
| 681 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 682 |
assertNull(browserChannel.forwardChannelRequest_); |
| 683 |
assertEquals(1, browserChannel.forwardChannelRetryCount_); |
| 684 |
assertEquals(1, numTimingEvents); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
/** |
| 689 |
* Make sure that dropping the forward channel retry limit below the retry count |
| 690 |
* reports an error, and prevents another request from firing. |
| 691 |
*/ |
| 692 |
function testSetFailFastWhileRetryXhrIsInFlight() { |
| 693 |
stubTmpnetwork(); |
| 694 |
|
| 695 |
connect(); |
| 696 |
assertEquals(1, numTimingEvents); |
| 697 |
|
| 698 |
sendMap('foo', 'bar'); |
| 699 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 700 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 701 |
assertEquals(0, browserChannel.forwardChannelRetryCount_); |
| 702 |
|
| 703 |
// Watchdog timeout. |
| 704 |
responseTimeout(); |
| 705 |
assertNotNull(browserChannel.forwardChannelTimerId_); |
| 706 |
assertNull(browserChannel.forwardChannelRequest_); |
| 707 |
assertEquals(1, browserChannel.forwardChannelRetryCount_); |
| 708 |
|
| 709 |
// Wait for the between-retry timeout. |
| 710 |
mockClock.tick(RETRY_TIME); |
| 711 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 712 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 713 |
assertEquals(1, browserChannel.forwardChannelRetryCount_); |
| 714 |
|
| 715 |
// Simulate a second watchdog timeout. |
| 716 |
responseTimeout(); |
| 717 |
assertNotNull(browserChannel.forwardChannelTimerId_); |
| 718 |
assertNull(browserChannel.forwardChannelRequest_); |
| 719 |
assertEquals(2, browserChannel.forwardChannelRetryCount_); |
| 720 |
|
| 721 |
// Wait for another between-retry timeout. |
| 722 |
mockClock.tick(RETRY_TIME); |
| 723 |
// Now the third req is in flight. |
| 724 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 725 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 726 |
assertEquals(2, browserChannel.forwardChannelRetryCount_); |
| 727 |
|
| 728 |
// Set fail fast, killing the request |
| 729 |
browserChannel.setFailFast(true); |
| 730 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 731 |
assertNull(browserChannel.forwardChannelRequest_); |
| 732 |
assertEquals(2, browserChannel.forwardChannelRetryCount_); |
| 733 |
|
| 734 |
assertTrue(gotError); |
| 735 |
// We get the error immediately before starting to ping google.com. |
| 736 |
// Simulate that timing out. We should get a network error in addition to the |
| 737 |
gotError = false; |
| 738 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 739 |
assertTrue('No error after tmpnetwork ping timed out.', gotError); |
| 740 |
|
| 741 |
// Make sure no more retry timers are firing. |
| 742 |
mockClock.tick(ALL_DAY_MS); |
| 743 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 744 |
assertNull(browserChannel.forwardChannelRequest_); |
| 745 |
assertEquals(2, browserChannel.forwardChannelRetryCount_); |
| 746 |
assertEquals(1, numTimingEvents); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
/** |
| 751 |
* Makes sure that setting fail fast while not retrying doesn't cause a failure. |
| 752 |
*/ |
| 753 |
function testSetFailFastAtRetryCount() { |
| 754 |
stubTmpnetwork(); |
| 755 |
|
| 756 |
connect(); |
| 757 |
assertEquals(1, numTimingEvents); |
| 758 |
|
| 759 |
sendMap('foo', 'bar'); |
| 760 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 761 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 762 |
assertEquals(0, browserChannel.forwardChannelRetryCount_); |
| 763 |
|
| 764 |
// Set fail fast. |
| 765 |
browserChannel.setFailFast(true); |
| 766 |
// Request should still be alive. |
| 767 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 768 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 769 |
assertEquals(0, browserChannel.forwardChannelRetryCount_); |
| 770 |
|
| 771 |
// Watchdog timeout. Now we should get an error. |
| 772 |
responseTimeout(); |
| 773 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 774 |
assertNull(browserChannel.forwardChannelRequest_); |
| 775 |
assertEquals(0, browserChannel.forwardChannelRetryCount_); |
| 776 |
|
| 777 |
assertTrue(gotError); |
| 778 |
// We get the error immediately before starting to ping google.com. |
| 779 |
// Simulate that timing out. We should get a network error in addition to the |
| 780 |
// initial failure. |
| 781 |
gotError = false; |
| 782 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 783 |
assertTrue('No error after tmpnetwork ping timed out.', gotError); |
| 784 |
|
| 785 |
// Make sure no more retry timers are firing. |
| 786 |
mockClock.tick(ALL_DAY_MS); |
| 787 |
assertNull(browserChannel.forwardChannelTimerId_); |
| 788 |
assertNull(browserChannel.forwardChannelRequest_); |
| 789 |
assertEquals(0, browserChannel.forwardChannelRetryCount_); |
| 790 |
assertEquals(1, numTimingEvents); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
function testRequestFailedClosesChannel() { |
| 795 |
stubTmpnetwork(); |
| 796 |
|
| 797 |
connect(); |
| 798 |
assertEquals(1, numTimingEvents); |
| 799 |
|
| 800 |
sendMap('foo', 'bar'); |
| 801 |
responseRequestFailed(); |
| 802 |
|
| 803 |
assertEquals( |
| 804 |
'Should be closed immediately after request failed.', |
| 805 |
goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 806 |
|
| 807 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 808 |
|
| 809 |
assertEquals( |
| 810 |
'Should remain closed after the ping timeout.', |
| 811 |
goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 812 |
assertEquals(1, numTimingEvents); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
function testStatEventReportedOnlyOnce() { |
| 817 |
stubTmpnetwork(); |
| 818 |
|
| 819 |
connect(); |
| 820 |
sendMap('foo', 'bar'); |
| 821 |
numStatEvents = 0; |
| 822 |
lastStatEvent = null; |
| 823 |
responseUnknownSessionId(); |
| 824 |
|
| 825 |
assertEquals(1, numStatEvents); |
| 826 |
assertEquals(goog.net.BrowserChannel.Stat.ERROR_OTHER, lastStatEvent); |
| 827 |
|
| 828 |
numStatEvents = 0; |
| 829 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 830 |
assertEquals('No new stat events should be reported.', 0, numStatEvents); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
function testActiveXBlockedEventReportedOnlyOnce() { |
| 835 |
stubTmpnetwork(); |
| 836 |
|
| 837 |
connectForwardChannel(); |
| 838 |
numStatEvents = 0; |
| 839 |
lastStatEvent = null; |
| 840 |
responseActiveXBlocked(); |
| 841 |
|
| 842 |
assertEquals(1, numStatEvents); |
| 843 |
assertEquals(goog.net.BrowserChannel.Stat.ERROR_OTHER, lastStatEvent); |
| 844 |
|
| 845 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 846 |
assertEquals('No new stat events should be reported.', 1, numStatEvents); |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
function testStatEventReportedOnlyOnce_onNetworkUp() { |
| 851 |
stubTmpnetwork(); |
| 852 |
|
| 853 |
connect(); |
| 854 |
sendMap('foo', 'bar'); |
| 855 |
numStatEvents = 0; |
| 856 |
lastStatEvent = null; |
| 857 |
responseRequestFailed(); |
| 858 |
|
| 859 |
assertEquals( |
| 860 |
'No stat event should be reported before we know the reason.', 0, |
| 861 |
numStatEvents); |
| 862 |
|
| 863 |
// Let the ping time out. |
| 864 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 865 |
|
| 866 |
// Assert we report the correct stat event. |
| 867 |
assertEquals(1, numStatEvents); |
| 868 |
assertEquals(goog.net.BrowserChannel.Stat.ERROR_NETWORK, lastStatEvent); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
function testStatEventReportedOnlyOnce_onNetworkDown() { |
| 873 |
stubTmpnetwork(); |
| 874 |
|
| 875 |
connect(); |
| 876 |
sendMap('foo', 'bar'); |
| 877 |
numStatEvents = 0; |
| 878 |
lastStatEvent = null; |
| 879 |
responseRequestFailed(); |
| 880 |
|
| 881 |
assertEquals( |
| 882 |
'No stat event should be reported before we know the reason.', 0, |
| 883 |
numStatEvents); |
| 884 |
|
| 885 |
// Wait half the ping timeout period, and then fake the network being up. |
| 886 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT / 2); |
| 887 |
browserChannel.testGoogleComCallback_(true); |
| 888 |
|
| 889 |
// Assert we report the correct stat event. |
| 890 |
assertEquals(1, numStatEvents); |
| 891 |
assertEquals(goog.net.BrowserChannel.Stat.ERROR_OTHER, lastStatEvent); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
function testOutgoingMapsAwaitsResponse() { |
| 896 |
connect(); |
| 897 |
assertEquals(0, browserChannel.outgoingMaps_.length); |
| 898 |
|
| 899 |
sendMap('foo1', 'bar'); |
| 900 |
assertEquals(0, browserChannel.outgoingMaps_.length); |
| 901 |
sendMap('foo2', 'bar'); |
| 902 |
assertEquals(1, browserChannel.outgoingMaps_.length); |
| 903 |
sendMap('foo3', 'bar'); |
| 904 |
assertEquals(2, browserChannel.outgoingMaps_.length); |
| 905 |
sendMap('foo4', 'bar'); |
| 906 |
assertEquals(3, browserChannel.outgoingMaps_.length); |
| 907 |
|
| 908 |
responseVersion7(); |
| 909 |
// Now the forward channel request is completed and a new started, so all maps |
| 910 |
// are dequeued from the array of outgoing maps into this new forward request. |
| 911 |
assertEquals(0, browserChannel.outgoingMaps_.length); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
function testUndeliveredMaps_doesNotNotifyWhenSuccessful() { |
| 916 |
handler.channelClosed = function( |
| 917 |
channel, opt_pendingMaps, opt_undeliveredMaps) { |
| 918 |
if (opt_pendingMaps || opt_undeliveredMaps) { |
| 919 |
fail('No pending or undelivered maps should be reported.'); |
| 920 |
} |
| 921 |
}; |
| 922 |
|
| 923 |
connect(); |
| 924 |
sendMap('foo1', 'bar1'); |
| 925 |
responseVersion7(); |
| 926 |
sendMap('foo2', 'bar2'); |
| 927 |
responseVersion7(); |
| 928 |
disconnect(); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
function testUndeliveredMaps_doesNotNotifyIfNothingWasSent() { |
| 933 |
handler.channelClosed = function( |
| 934 |
channel, opt_pendingMaps, opt_undeliveredMaps) { |
| 935 |
if (opt_pendingMaps || opt_undeliveredMaps) { |
| 936 |
fail('No pending or undelivered maps should be reported.'); |
| 937 |
} |
| 938 |
}; |
| 939 |
|
| 940 |
connect(); |
| 941 |
mockClock.tick(ALL_DAY_MS); |
| 942 |
disconnect(); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
function testUndeliveredMaps_clearsPendingMapsAfterNotifying() { |
| 947 |
connect(); |
| 948 |
sendMap('foo1', 'bar1'); |
| 949 |
sendMap('foo2', 'bar2'); |
| 950 |
sendMap('foo3', 'bar3'); |
| 951 |
|
| 952 |
assertEquals(1, browserChannel.pendingMaps_.length); |
| 953 |
assertEquals(2, browserChannel.outgoingMaps_.length); |
| 954 |
|
| 955 |
disconnect(); |
| 956 |
|
| 957 |
assertEquals(0, browserChannel.pendingMaps_.length); |
| 958 |
assertEquals(0, browserChannel.outgoingMaps_.length); |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
function testUndeliveredMaps_notifiesWithContext() { |
| 963 |
connect(); |
| 964 |
|
| 965 |
// First send two messages that succeed. |
| 966 |
sendMap('foo1', 'bar1', 'context1'); |
| 967 |
responseVersion7(); |
| 968 |
sendMap('foo2', 'bar2', 'context2'); |
| 969 |
responseVersion7(); |
| 970 |
|
| 971 |
// Pretend the server hangs and no longer responds. |
| 972 |
sendMap('foo3', 'bar3', 'context3'); |
| 973 |
sendMap('foo4', 'bar4', 'context4'); |
| 974 |
sendMap('foo5', 'bar5', 'context5'); |
| 975 |
|
| 976 |
// Give up. |
| 977 |
disconnect(); |
| 978 |
|
| 979 |
// Assert that we are informed of any undelivered messages; both about |
| 980 |
// #3 that was sent but which we don't know if the server received, and |
| 981 |
// #4 and #5 which remain in the outgoing maps and have not yet been sent. |
| 982 |
assertEquals('foo3:bar3:context3', handler.pendingMapsString); |
| 983 |
assertEquals( |
| 984 |
'foo4:bar4:context4, foo5:bar5:context5', handler.undeliveredMapsString); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
function testUndeliveredMaps_serviceUnavailable() { |
| 989 |
// Send a few maps, and let one fail. |
| 990 |
connect(); |
| 991 |
sendMap('foo1', 'bar1'); |
| 992 |
responseVersion7(); |
| 993 |
sendMap('foo2', 'bar2'); |
| 994 |
responseRequestFailed(); |
| 995 |
|
| 996 |
// After a failure, the channel should be closed. |
| 997 |
disconnect(); |
| 998 |
|
| 999 |
assertEquals('foo2:bar2', handler.pendingMapsString); |
| 1000 |
assertEquals('', handler.undeliveredMapsString); |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
function testUndeliveredMaps_onPingTimeout() { |
| 1005 |
stubTmpnetwork(); |
| 1006 |
|
| 1007 |
connect(); |
| 1008 |
|
| 1009 |
// Send a message. |
| 1010 |
sendMap('foo1', 'bar1'); |
| 1011 |
|
| 1012 |
// Fake REQUEST_FAILED, triggering a ping to check the network. |
| 1013 |
responseRequestFailed(); |
| 1014 |
|
| 1015 |
// Let the ping time out, unsuccessfully. |
| 1016 |
mockClock.tick(goog.net.tmpnetwork.GOOGLECOM_TIMEOUT); |
| 1017 |
|
| 1018 |
// Assert channel is closed. |
| 1019 |
assertEquals(goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 1020 |
|
| 1021 |
// Assert that the handler is notified about the undelivered messages. |
| 1022 |
assertEquals('foo1:bar1', handler.pendingMapsString); |
| 1023 |
assertEquals('', handler.undeliveredMapsString); |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
function testResponseNoBackchannelPostNotBeforeBackchannel() { |
| 1028 |
connect(8); |
| 1029 |
sendMap('foo1', 'bar1'); |
| 1030 |
|
| 1031 |
mockClock.tick(10); |
| 1032 |
assertFalse( |
| 1033 |
browserChannel.backChannelRequest_.getRequestStartTime() < |
| 1034 |
browserChannel.forwardChannelRequest_.getRequestStartTime()); |
| 1035 |
responseNoBackchannel(); |
| 1036 |
assertNotEquals( |
| 1037 |
goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING, lastStatEvent); |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
function testResponseNoBackchannel() { |
| 1042 |
connect(8); |
| 1043 |
sendMap('foo1', 'bar1'); |
| 1044 |
response(-1, 0); |
| 1045 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE + 1); |
| 1046 |
sendMap('foo2', 'bar2'); |
| 1047 |
assertTrue( |
| 1048 |
browserChannel.backChannelRequest_.getRequestStartTime() + |
| 1049 |
goog.net.BrowserChannel.RTT_ESTIMATE < |
| 1050 |
browserChannel.forwardChannelRequest_.getRequestStartTime()); |
| 1051 |
responseNoBackchannel(); |
| 1052 |
assertEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING, lastStatEvent); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
function testResponseNoBackchannelWithNoBackchannel() { |
| 1057 |
connect(8); |
| 1058 |
sendMap('foo1', 'bar1'); |
| 1059 |
assertNull(browserChannel.backChannelTimerId_); |
| 1060 |
browserChannel.backChannelRequest_.cancel(); |
| 1061 |
browserChannel.backChannelRequest_ = null; |
| 1062 |
responseNoBackchannel(); |
| 1063 |
assertEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING, lastStatEvent); |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
function testResponseNoBackchannelWithStartTimer() { |
| 1068 |
connect(8); |
| 1069 |
sendMap('foo1', 'bar1'); |
| 1070 |
|
| 1071 |
browserChannel.backChannelRequest_.cancel(); |
| 1072 |
browserChannel.backChannelRequest_ = null; |
| 1073 |
browserChannel.backChannelTimerId_ = 123; |
| 1074 |
responseNoBackchannel(); |
| 1075 |
assertNotEquals( |
| 1076 |
goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING, lastStatEvent); |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
function testResponseWithNoArraySent() { |
| 1081 |
connect(8); |
| 1082 |
sendMap('foo1', 'bar1'); |
| 1083 |
|
| 1084 |
// Send a response as if the server hasn't sent down an array. |
| 1085 |
response(-1, 0); |
| 1086 |
|
| 1087 |
// POST response with an array ID lower than our last received is OK. |
| 1088 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1089 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
function testResponseWithArraysMissing() { |
| 1094 |
connect(8); |
| 1095 |
sendMap('foo1', 'bar1'); |
| 1096 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1097 |
|
| 1098 |
// Send a response as if the server has sent down seven arrays. |
| 1099 |
response(7, 111); |
| 1100 |
|
| 1101 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1102 |
assertEquals(7, browserChannel.lastPostResponseArrayId_); |
| 1103 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE * 2); |
| 1104 |
assertEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD, lastStatEvent); |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
function testMultipleResponsesWithArraysMissing() { |
| 1109 |
connect(8); |
| 1110 |
sendMap('foo1', 'bar1'); |
| 1111 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1112 |
|
| 1113 |
// Send a response as if the server has sent down seven arrays. |
| 1114 |
response(7, 111); |
| 1115 |
|
| 1116 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1117 |
assertEquals(7, browserChannel.lastPostResponseArrayId_); |
| 1118 |
sendMap('foo2', 'bar2'); |
| 1119 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE); |
| 1120 |
response(8, 119); |
| 1121 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE); |
| 1122 |
// The original timer should still fire. |
| 1123 |
assertEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD, lastStatEvent); |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
function testOnlyRetryOnceBasedOnResponse() { |
| 1128 |
connect(8); |
| 1129 |
sendMap('foo1', 'bar1'); |
| 1130 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1131 |
|
| 1132 |
// Send a response as if the server has sent down seven arrays. |
| 1133 |
response(7, 111); |
| 1134 |
|
| 1135 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1136 |
assertEquals(7, browserChannel.lastPostResponseArrayId_); |
| 1137 |
assertTrue(hasDeadBackChannelTimer()); |
| 1138 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE * 2); |
| 1139 |
assertEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD, lastStatEvent); |
| 1140 |
assertEquals(1, browserChannel.backChannelRetryCount_); |
| 1141 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE); |
| 1142 |
sendMap('foo2', 'bar2'); |
| 1143 |
assertFalse(hasDeadBackChannelTimer()); |
| 1144 |
response(8, 119); |
| 1145 |
assertFalse(hasDeadBackChannelTimer()); |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
function testResponseWithArraysMissingAndLiveChannel() { |
| 1150 |
connect(8); |
| 1151 |
sendMap('foo1', 'bar1'); |
| 1152 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1153 |
|
| 1154 |
// Send a response as if the server has sent down seven arrays. |
| 1155 |
response(7, 111); |
| 1156 |
|
| 1157 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1158 |
assertEquals(7, browserChannel.lastPostResponseArrayId_); |
| 1159 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE); |
| 1160 |
assertTrue(hasDeadBackChannelTimer()); |
| 1161 |
receive('["ack"]'); |
| 1162 |
assertFalse(hasDeadBackChannelTimer()); |
| 1163 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE); |
| 1164 |
assertNotEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD, lastStatEvent); |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
function testResponseWithBigOutstandingData() { |
| 1169 |
connect(8); |
| 1170 |
sendMap('foo1', 'bar1'); |
| 1171 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1172 |
|
| 1173 |
// Send a response as if the server has sent down seven arrays and 50kbytes. |
| 1174 |
response(7, 50000); |
| 1175 |
|
| 1176 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1177 |
assertEquals(7, browserChannel.lastPostResponseArrayId_); |
| 1178 |
assertFalse(hasDeadBackChannelTimer()); |
| 1179 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE * 2); |
| 1180 |
assertNotEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD, lastStatEvent); |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
function testResponseInBufferedMode() { |
| 1185 |
connect(8); |
| 1186 |
browserChannel.useChunked_ = false; |
| 1187 |
sendMap('foo1', 'bar1'); |
| 1188 |
assertEquals(-1, browserChannel.lastPostResponseArrayId_); |
| 1189 |
response(7, 111); |
| 1190 |
|
| 1191 |
assertEquals(1, browserChannel.lastArrayId_); |
| 1192 |
assertEquals(7, browserChannel.lastPostResponseArrayId_); |
| 1193 |
assertFalse(hasDeadBackChannelTimer()); |
| 1194 |
mockClock.tick(goog.net.BrowserChannel.RTT_ESTIMATE * 2); |
| 1195 |
assertNotEquals(goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD, lastStatEvent); |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
function testResponseWithGarbage() { |
| 1200 |
connect(8); |
| 1201 |
sendMap('foo1', 'bar1'); |
| 1202 |
browserChannel.onRequestData( |
| 1203 |
browserChannel.forwardChannelRequest_, 'garbage'); |
| 1204 |
assertEquals(goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
function testResponseWithGarbageInArray() { |
| 1209 |
connect(8); |
| 1210 |
sendMap('foo1', 'bar1'); |
| 1211 |
browserChannel.onRequestData( |
| 1212 |
browserChannel.forwardChannelRequest_, '["garbage"]'); |
| 1213 |
assertEquals(goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
function testResponseWithEvilData() { |
| 1218 |
connect(8); |
| 1219 |
sendMap('foo1', 'bar1'); |
| 1220 |
browserChannel.onRequestData( |
| 1221 |
browserChannel.forwardChannelRequest_, |
| 1222 |
goog.net.BrowserChannel.LAST_ARRAY_ID_RESPONSE_PREFIX + |
| 1223 |
'=<script>evil()\<\/script>&' + |
| 1224 |
goog.net.BrowserChannel.OUTSTANDING_DATA_RESPONSE_PREFIX + |
| 1225 |
'=<script>moreEvil()\<\/script>'); |
| 1226 |
assertEquals(goog.net.BrowserChannel.State.CLOSED, browserChannel.getState()); |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
function testPathAbsolute() { |
| 1231 |
connect(8, undefined, '/talkgadget'); |
| 1232 |
assertEquals( |
| 1233 |
browserChannel.backChannelUri_.getDomain(), window.location.hostname); |
| 1234 |
assertEquals( |
| 1235 |
browserChannel.forwardChannelUri_.getDomain(), window.location.hostname); |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
function testPathRelative() { |
| 1240 |
connect(8, undefined, 'talkgadget'); |
| 1241 |
assertEquals( |
| 1242 |
browserChannel.backChannelUri_.getDomain(), window.location.hostname); |
| 1243 |
assertEquals( |
| 1244 |
browserChannel.forwardChannelUri_.getDomain(), window.location.hostname); |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
function testPathWithHost() { |
| 1249 |
connect(8, undefined, 'https://example.com'); |
| 1250 |
assertEquals(browserChannel.backChannelUri_.getScheme(), 'https'); |
| 1251 |
assertEquals(browserChannel.backChannelUri_.getDomain(), 'example.com'); |
| 1252 |
assertEquals(browserChannel.forwardChannelUri_.getScheme(), 'https'); |
| 1253 |
assertEquals(browserChannel.forwardChannelUri_.getDomain(), 'example.com'); |
| 1254 |
} |
| 1255 |
|
| 1256 |
function testCreateXhrIo() { |
| 1257 |
var xhr = browserChannel.createXhrIo(null); |
| 1258 |
assertFalse(xhr.getWithCredentials()); |
| 1259 |
|
| 1260 |
assertThrows( |
| 1261 |
'Error connection to different host without CORS', |
| 1262 |
goog.bind(browserChannel.createXhrIo, browserChannel, 'some_host')); |
| 1263 |
|
| 1264 |
browserChannel.setSupportsCrossDomainXhrs(true); |
| 1265 |
|
| 1266 |
xhr = browserChannel.createXhrIo(null); |
| 1267 |
assertTrue(xhr.getWithCredentials()); |
| 1268 |
|
| 1269 |
xhr = browserChannel.createXhrIo('some_host'); |
| 1270 |
assertTrue(xhr.getWithCredentials()); |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
function testSetParser() { |
| 1275 |
var recordParse = goog.testing.recordFunction(JSON.parse); |
| 1276 |
var parser = {}; |
| 1277 |
parser.parse = recordParse; |
| 1278 |
browserChannel.setParser(parser); |
| 1279 |
|
| 1280 |
connect(); |
| 1281 |
assertEquals(3, recordParse.getCallCount()); |
| 1282 |
|
| 1283 |
var call3 = recordParse.popLastCall(); |
| 1284 |
var call2 = recordParse.popLastCall(); |
| 1285 |
var call1 = recordParse.popLastCall(); |
| 1286 |
|
| 1287 |
assertEquals(1, call1.getArguments().length); |
| 1288 |
assertEquals('["b"]', call1.getArgument(0)); |
| 1289 |
|
| 1290 |
assertEquals(1, call2.getArguments().length); |
| 1291 |
assertEquals('[[0,["c","1234567890ABCDEF",null]]]', call2.getArgument(0)); |
| 1292 |
|
| 1293 |
assertEquals(1, call3.getArguments().length); |
| 1294 |
assertEquals('[[1,["foo"]]]', call3.getArgument(0)); |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
function testAsyncTest() { |
| 1299 |
browserChannel.asyncTest_ = true; |
| 1300 |
browserChannel.connect('/test', '/bind'); |
| 1301 |
mockClock.tick(0); |
| 1302 |
|
| 1303 |
// We first establish the channel, assuming it is buffered. |
| 1304 |
assertNotNull(browserChannel.forwardChannelRequest_); |
| 1305 |
assertNull(browserChannel.connectionTest_); |
| 1306 |
assertTrue(browserChannel.isBuffered()); |
| 1307 |
|
| 1308 |
// Since we're assuming the channel is buferred, the "Close Immediately" flag |
| 1309 |
// should be set. |
| 1310 |
completeForwardChannel(); |
| 1311 |
assertEquals( |
| 1312 |
'1', browserChannel.backChannelRequest_.requestUri_.queryData_.get('CI')); |
| 1313 |
|
| 1314 |
mockClock.tick(100); |
| 1315 |
|
| 1316 |
// Now, we perform the test which reveals the channel is not buffered. |
| 1317 |
assertNotNull(browserChannel.connectionTest_); |
| 1318 |
completeForwardTestConnection(); |
| 1319 |
completeBackTestConnection(); |
| 1320 |
assertFalse(browserChannel.isBuffered()); |
| 1321 |
|
| 1322 |
// From now on, the "Close Immediately" flag should not be set. |
| 1323 |
completeBackChannel(); |
| 1324 |
assertEquals( |
| 1325 |
'0', browserChannel.backChannelRequest_.requestUri_.queryData_.get('CI')); |
| 1326 |
} |