| 1 |
// Copyright 2013 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 |
/** |
| 16 |
* @fileoverview Unit tests for goog.labs.net.webChannel.ChannelRequest. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.labs.net.webChannel.channelRequestTest'); |
| 22 |
|
| 23 |
goog.require('goog.Uri'); |
| 24 |
goog.require('goog.functions'); |
| 25 |
goog.require('goog.labs.net.webChannel.ChannelRequest'); |
| 26 |
goog.require('goog.labs.net.webChannel.WebChannelDebug'); |
| 27 |
goog.require('goog.labs.net.webChannel.requestStats'); |
| 28 |
goog.require('goog.labs.net.webChannel.requestStats.ServerReachability'); |
| 29 |
goog.require('goog.testing.MockClock'); |
| 30 |
goog.require('goog.testing.PropertyReplacer'); |
| 31 |
goog.require('goog.testing.jsunit'); |
| 32 |
goog.require('goog.testing.net.XhrIo'); |
| 33 |
goog.require('goog.testing.recordFunction'); |
| 34 |
|
| 35 |
goog.setTestOnly('goog.labs.net.webChannel.channelRequestTest'); |
| 36 |
|
| 37 |
|
| 38 |
var channelRequest; |
| 39 |
var mockChannel; |
| 40 |
var mockClock; |
| 41 |
var stubs; |
| 42 |
var xhrIo; |
| 43 |
var reachabilityEvents; |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Time to wait for a network request to time out, before aborting. |
| 48 |
*/ |
| 49 |
var WATCHDOG_TIME = 2000; |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Time to throttle readystatechange events. |
| 54 |
*/ |
| 55 |
var THROTTLE_TIME = 500; |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* A really long time - used to make sure no more timeouts will fire. |
| 60 |
*/ |
| 61 |
var ALL_DAY_MS = 1000 * 60 * 60 * 24; |
| 62 |
|
| 63 |
|
| 64 |
function shouldRunTests() { |
| 65 |
return goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
function setUp() { |
| 70 |
mockClock = new goog.testing.MockClock(); |
| 71 |
mockClock.install(); |
| 72 |
reachabilityEvents = {}; |
| 73 |
stubs = new goog.testing.PropertyReplacer(); |
| 74 |
|
| 75 |
// Mock out the stat notification code. |
| 76 |
var notifyServerReachabilityEvent = function(reachabilityType) { |
| 77 |
if (!reachabilityEvents[reachabilityType]) { |
| 78 |
reachabilityEvents[reachabilityType] = 0; |
| 79 |
} |
| 80 |
reachabilityEvents[reachabilityType]++; |
| 81 |
}; |
| 82 |
stubs.set( |
| 83 |
goog.labs.net.webChannel.requestStats, 'notifyServerReachabilityEvent', |
| 84 |
notifyServerReachabilityEvent); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
function tearDown() { |
| 89 |
stubs.reset(); |
| 90 |
mockClock.uninstall(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Constructs a duck-type WebChannelBase that tracks the completed requests. |
| 97 |
* @constructor |
| 98 |
* @struct |
| 99 |
* @final |
| 100 |
*/ |
| 101 |
function MockWebChannelBase() { |
| 102 |
this.isClosed = function() { return false; }; |
| 103 |
this.isActive = function() { return true; }; |
| 104 |
this.shouldUseSecondaryDomains = function() { return false; }; |
| 105 |
this.completedRequests = []; |
| 106 |
this.onRequestComplete = function(request) { |
| 107 |
this.completedRequests.push(request); |
| 108 |
}; |
| 109 |
this.onRequestData = function(request, data) {}; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/** |
| 114 |
* Creates a real ChannelRequest object, with some modifications for |
| 115 |
* testability: |
| 116 |
* <ul> |
| 117 |
* <li>The channel is a mock channel. |
| 118 |
* <li>The new watchdogTimeoutCallCount property tracks onWatchDogTimeout_() |
| 119 |
* calls. |
| 120 |
* <li>The timeout is set to WATCHDOG_TIME. |
| 121 |
* </ul> |
| 122 |
*/ |
| 123 |
function createChannelRequest() { |
| 124 |
xhrIo = new goog.testing.net.XhrIo(); |
| 125 |
xhrIo.abort = xhrIo.abort || function() { this.active_ = false; }; |
| 126 |
|
| 127 |
// Install mock channel and no-op debug logger. |
| 128 |
mockChannel = new MockWebChannelBase(); |
| 129 |
channelRequest = new goog.labs.net.webChannel.ChannelRequest( |
| 130 |
mockChannel, new goog.labs.net.webChannel.WebChannelDebug()); |
| 131 |
|
| 132 |
// Install test XhrIo. |
| 133 |
mockChannel.createXhrIo = function() { return xhrIo; }; |
| 134 |
|
| 135 |
// Install watchdogTimeoutCallCount. |
| 136 |
channelRequest.watchdogTimeoutCallCount = 0; |
| 137 |
channelRequest.originalOnWatchDogTimeout = channelRequest.onWatchDogTimeout_; |
| 138 |
channelRequest.onWatchDogTimeout_ = function() { |
| 139 |
channelRequest.watchdogTimeoutCallCount++; |
| 140 |
return channelRequest.originalOnWatchDogTimeout(); |
| 141 |
}; |
| 142 |
|
| 143 |
channelRequest.setTimeout(WATCHDOG_TIME); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Run through the lifecycle of a long lived request, checking that the right |
| 149 |
* network events are reported. |
| 150 |
*/ |
| 151 |
function testNetworkEvents() { |
| 152 |
createChannelRequest(); |
| 153 |
|
| 154 |
channelRequest.xmlHttpPost(new goog.Uri('some_uri'), 'some_postdata', true); |
| 155 |
checkReachabilityEvents(1, 0, 0, 0); |
| 156 |
if (goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming()) { |
| 157 |
xhrIo.simulatePartialResponse('17\nI am a BC Message'); |
| 158 |
checkReachabilityEvents(1, 0, 0, 1); |
| 159 |
xhrIo.simulatePartialResponse('23\nI am another BC Message'); |
| 160 |
checkReachabilityEvents(1, 0, 0, 2); |
| 161 |
xhrIo.simulateResponse(200, '16\Final BC Message'); |
| 162 |
checkReachabilityEvents(1, 1, 0, 2); |
| 163 |
} else { |
| 164 |
xhrIo.simulateResponse(200, '16\Final BC Message'); |
| 165 |
checkReachabilityEvents(1, 1, 0, 0); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Test throttling of readystatechange events. |
| 172 |
*/ |
| 173 |
function testNetworkEvents_throttleReadyStateChange() { |
| 174 |
createChannelRequest(); |
| 175 |
channelRequest.setReadyStateChangeThrottle(THROTTLE_TIME); |
| 176 |
|
| 177 |
var recordedHandler = |
| 178 |
goog.testing.recordFunction(channelRequest.xmlHttpHandler_); |
| 179 |
stubs.set(channelRequest, 'xmlHttpHandler_', recordedHandler); |
| 180 |
|
| 181 |
channelRequest.xmlHttpPost(new goog.Uri('some_uri'), 'some_postdata', true); |
| 182 |
assertEquals(1, recordedHandler.getCallCount()); |
| 183 |
|
| 184 |
checkReachabilityEvents(1, 0, 0, 0); |
| 185 |
if (goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming()) { |
| 186 |
xhrIo.simulatePartialResponse('17\nI am a BC Message'); |
| 187 |
checkReachabilityEvents(1, 0, 0, 1); |
| 188 |
assertEquals(3, recordedHandler.getCallCount()); |
| 189 |
|
| 190 |
// Second event should be throttled |
| 191 |
xhrIo.simulatePartialResponse('23\nI am another BC Message'); |
| 192 |
assertEquals(3, recordedHandler.getCallCount()); |
| 193 |
|
| 194 |
xhrIo.simulatePartialResponse('27\nI am yet another BC Message'); |
| 195 |
assertEquals(3, recordedHandler.getCallCount()); |
| 196 |
mockClock.tick(THROTTLE_TIME); |
| 197 |
|
| 198 |
checkReachabilityEvents(1, 0, 0, 3); |
| 199 |
// Only one more call because of throttling. |
| 200 |
assertEquals(4, recordedHandler.getCallCount()); |
| 201 |
|
| 202 |
xhrIo.simulateResponse(200, '16\Final BC Message'); |
| 203 |
checkReachabilityEvents(1, 1, 0, 3); |
| 204 |
assertEquals(5, recordedHandler.getCallCount()); |
| 205 |
} else { |
| 206 |
xhrIo.simulateResponse(200, '16\Final BC Message'); |
| 207 |
checkReachabilityEvents(1, 1, 0, 0); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/** |
| 213 |
* Make sure that the request "completes" with an error when the timeout |
| 214 |
* expires. |
| 215 |
*/ |
| 216 |
function testRequestTimeout() { |
| 217 |
createChannelRequest(); |
| 218 |
|
| 219 |
channelRequest.xmlHttpPost(new goog.Uri('some_uri'), 'some_postdata', true); |
| 220 |
assertEquals(0, channelRequest.watchdogTimeoutCallCount); |
| 221 |
assertEquals(0, channelRequest.channel_.completedRequests.length); |
| 222 |
|
| 223 |
// Watchdog timeout. |
| 224 |
mockClock.tick(WATCHDOG_TIME); |
| 225 |
assertEquals(1, channelRequest.watchdogTimeoutCallCount); |
| 226 |
assertEquals(1, channelRequest.channel_.completedRequests.length); |
| 227 |
assertFalse(channelRequest.getSuccess()); |
| 228 |
|
| 229 |
// Make sure no more timers are firing. |
| 230 |
mockClock.tick(ALL_DAY_MS); |
| 231 |
assertEquals(1, channelRequest.watchdogTimeoutCallCount); |
| 232 |
assertEquals(1, channelRequest.channel_.completedRequests.length); |
| 233 |
|
| 234 |
checkReachabilityEvents(1, 0, 1, 0); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
function testRequestTimeoutWithUnexpectedException() { |
| 239 |
createChannelRequest(); |
| 240 |
channelRequest.channel_.createXhrIo = goog.functions.error('Weird error'); |
| 241 |
|
| 242 |
try { |
| 243 |
channelRequest.xmlHttpGet(new goog.Uri('some_uri'), true, null); |
| 244 |
fail('Expected error'); |
| 245 |
} catch (e) { |
| 246 |
assertEquals('Weird error', e.message); |
| 247 |
} |
| 248 |
|
| 249 |
assertEquals(0, channelRequest.watchdogTimeoutCallCount); |
| 250 |
assertEquals(0, channelRequest.channel_.completedRequests.length); |
| 251 |
|
| 252 |
// Watchdog timeout. |
| 253 |
mockClock.tick(WATCHDOG_TIME); |
| 254 |
assertEquals(1, channelRequest.watchdogTimeoutCallCount); |
| 255 |
assertEquals(1, channelRequest.channel_.completedRequests.length); |
| 256 |
assertFalse(channelRequest.getSuccess()); |
| 257 |
|
| 258 |
// Make sure no more timers are firing. |
| 259 |
mockClock.tick(ALL_DAY_MS); |
| 260 |
assertEquals(1, channelRequest.watchdogTimeoutCallCount); |
| 261 |
assertEquals(1, channelRequest.channel_.completedRequests.length); |
| 262 |
|
| 263 |
checkReachabilityEvents(0, 0, 1, 0); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
function checkReachabilityEvents(reqMade, reqSucceeded, reqFail, backChannel) { |
| 268 |
var Reachability = goog.labs.net.webChannel.requestStats.ServerReachability; |
| 269 |
assertEquals(reqMade, reachabilityEvents[Reachability.REQUEST_MADE] || 0); |
| 270 |
assertEquals( |
| 271 |
reqSucceeded, reachabilityEvents[Reachability.REQUEST_SUCCEEDED] || 0); |
| 272 |
assertEquals(reqFail, reachabilityEvents[Reachability.REQUEST_FAILED] || 0); |
| 273 |
assertEquals( |
| 274 |
backChannel, reachabilityEvents[Reachability.BACK_CHANNEL_ACTIVITY] || 0); |
| 275 |
} |