| 1 |
// Copyright 2011 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.WebSocketTest'); |
| 16 |
goog.setTestOnly('goog.net.WebSocketTest'); |
| 17 |
|
| 18 |
goog.require('goog.debug.EntryPointMonitor'); |
| 19 |
goog.require('goog.debug.ErrorHandler'); |
| 20 |
goog.require('goog.debug.entryPointRegistry'); |
| 21 |
goog.require('goog.events'); |
| 22 |
goog.require('goog.functions'); |
| 23 |
goog.require('goog.net.WebSocket'); |
| 24 |
goog.require('goog.testing.MockClock'); |
| 25 |
goog.require('goog.testing.PropertyReplacer'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.recordFunction'); |
| 28 |
|
| 29 |
var webSocket; |
| 30 |
var mockClock; |
| 31 |
var pr; |
| 32 |
var testUrl; |
| 33 |
|
| 34 |
var originalOnOpen = goog.net.WebSocket.prototype.onOpen_; |
| 35 |
var originalOnClose = goog.net.WebSocket.prototype.onClose_; |
| 36 |
var originalOnMessage = goog.net.WebSocket.prototype.onMessage_; |
| 37 |
var originalOnError = goog.net.WebSocket.prototype.onError_; |
| 38 |
|
| 39 |
function setUp() { |
| 40 |
pr = new goog.testing.PropertyReplacer(); |
| 41 |
pr.set(goog.global, 'WebSocket', MockWebSocket); |
| 42 |
mockClock = new goog.testing.MockClock(true); |
| 43 |
testUrl = 'ws://127.0.0.1:4200'; |
| 44 |
testProtocol = 'xmpp'; |
| 45 |
} |
| 46 |
|
| 47 |
function tearDown() { |
| 48 |
pr.reset(); |
| 49 |
goog.net.WebSocket.prototype.onOpen_ = originalOnOpen; |
| 50 |
goog.net.WebSocket.prototype.onClose_ = originalOnClose; |
| 51 |
goog.net.WebSocket.prototype.onMessage_ = originalOnMessage; |
| 52 |
goog.net.WebSocket.prototype.onError_ = originalOnError; |
| 53 |
goog.dispose(mockClock); |
| 54 |
goog.dispose(webSocket); |
| 55 |
} |
| 56 |
|
| 57 |
function testOpenInUnsupportingBrowserThrowsException() { |
| 58 |
// Null out WebSocket to simulate lack of support. |
| 59 |
if (goog.global.WebSocket) { |
| 60 |
goog.global.WebSocket = null; |
| 61 |
} |
| 62 |
|
| 63 |
webSocket = new goog.net.WebSocket(); |
| 64 |
assertThrows('Open should fail if WebSocket is not defined.', function() { |
| 65 |
webSocket.open(testUrl); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
function testOpenTwiceThrowsException() { |
| 70 |
webSocket = new goog.net.WebSocket(); |
| 71 |
webSocket.open(testUrl); |
| 72 |
simulateOpenEvent(webSocket.webSocket_); |
| 73 |
|
| 74 |
assertThrows('Attempting to open a second time should fail.', function() { |
| 75 |
webSocket.open(testUrl); |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
function testSendWithoutOpeningThrowsException() { |
| 80 |
webSocket = new goog.net.WebSocket(); |
| 81 |
|
| 82 |
assertThrows( |
| 83 |
'Send should fail if the web socket was not first opened.', |
| 84 |
function() { webSocket.send('test message'); }); |
| 85 |
} |
| 86 |
|
| 87 |
function testOpenWithProtocol() { |
| 88 |
webSocket = new goog.net.WebSocket(); |
| 89 |
webSocket.open(testUrl, testProtocol); |
| 90 |
var ws = webSocket.webSocket_; |
| 91 |
simulateOpenEvent(ws); |
| 92 |
assertEquals(testUrl, ws.url); |
| 93 |
assertEquals(testProtocol, ws.protocol); |
| 94 |
} |
| 95 |
|
| 96 |
function testOpenAndClose() { |
| 97 |
webSocket = new goog.net.WebSocket(); |
| 98 |
assertFalse(webSocket.isOpen()); |
| 99 |
webSocket.open(testUrl); |
| 100 |
var ws = webSocket.webSocket_; |
| 101 |
simulateOpenEvent(ws); |
| 102 |
assertTrue(webSocket.isOpen()); |
| 103 |
assertEquals(testUrl, ws.url); |
| 104 |
webSocket.close(); |
| 105 |
simulateCloseEvent(ws); |
| 106 |
assertFalse(webSocket.isOpen()); |
| 107 |
} |
| 108 |
|
| 109 |
function testOpenAndCloseWithOptions() { |
| 110 |
webSocket = new goog.net.WebSocket({ |
| 111 |
autoReconnect: true, |
| 112 |
getNextReconnect: linearBackOff, |
| 113 |
binaryType: goog.net.WebSocket.BinaryType.ARRAY_BUFFER |
| 114 |
}); |
| 115 |
assertFalse(webSocket.isOpen()); |
| 116 |
webSocket.open(testUrl); |
| 117 |
var ws = webSocket.webSocket_; |
| 118 |
simulateOpenEvent(ws); |
| 119 |
assertTrue(webSocket.isOpen()); |
| 120 |
assertEquals(testUrl, ws.url); |
| 121 |
webSocket.close(); |
| 122 |
simulateCloseEvent(ws); |
| 123 |
assertFalse(webSocket.isOpen()); |
| 124 |
} |
| 125 |
|
| 126 |
function testReconnectionDisabled() { |
| 127 |
// Construct the web socket and disable reconnection. |
| 128 |
webSocket = new goog.net.WebSocket(false); |
| 129 |
|
| 130 |
// Record how many times open is called. |
| 131 |
pr.set(webSocket, 'open', goog.testing.recordFunction(webSocket.open)); |
| 132 |
|
| 133 |
// Open the web socket. |
| 134 |
webSocket.open(testUrl); |
| 135 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 136 |
assertEquals(1, webSocket.open.getCallCount()); |
| 137 |
assertFalse(webSocket.isOpen()); |
| 138 |
|
| 139 |
// Simulate failure. |
| 140 |
var ws = webSocket.webSocket_; |
| 141 |
simulateCloseEvent(ws); |
| 142 |
assertFalse(webSocket.isOpen()); |
| 143 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 144 |
assertEquals(1, webSocket.open.getCallCount()); |
| 145 |
|
| 146 |
// Make sure a reconnection doesn't happen. |
| 147 |
mockClock.tick(100000); |
| 148 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 149 |
assertEquals(1, webSocket.open.getCallCount()); |
| 150 |
} |
| 151 |
|
| 152 |
function testReconnectionWithFailureOnFirstOpen() { |
| 153 |
// Construct the web socket with a linear back-off. |
| 154 |
webSocket = new goog.net.WebSocket(true, linearBackOff); |
| 155 |
|
| 156 |
// Record how many times open is called. |
| 157 |
pr.set(webSocket, 'open', goog.testing.recordFunction(webSocket.open)); |
| 158 |
|
| 159 |
// Open the web socket. |
| 160 |
webSocket.open(testUrl, testProtocol); |
| 161 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 162 |
assertEquals(1, webSocket.open.getCallCount()); |
| 163 |
assertFalse(webSocket.isOpen()); |
| 164 |
|
| 165 |
// Simulate failure. |
| 166 |
var ws = webSocket.webSocket_; |
| 167 |
simulateCloseEvent(ws); |
| 168 |
assertFalse(webSocket.isOpen()); |
| 169 |
assertEquals(1, webSocket.reconnectAttempt_); |
| 170 |
assertEquals(1, webSocket.open.getCallCount()); |
| 171 |
|
| 172 |
// Make sure the reconnect doesn't happen before it should. |
| 173 |
mockClock.tick(linearBackOff(0) - 1); |
| 174 |
assertEquals(1, webSocket.open.getCallCount()); |
| 175 |
mockClock.tick(1); |
| 176 |
assertEquals(2, webSocket.open.getCallCount()); |
| 177 |
|
| 178 |
// Simulate another failure. |
| 179 |
simulateCloseEvent(ws); |
| 180 |
assertFalse(webSocket.isOpen()); |
| 181 |
assertEquals(2, webSocket.reconnectAttempt_); |
| 182 |
assertEquals(2, webSocket.open.getCallCount()); |
| 183 |
|
| 184 |
// Make sure the reconnect doesn't happen before it should. |
| 185 |
mockClock.tick(linearBackOff(1) - 1); |
| 186 |
assertEquals(2, webSocket.open.getCallCount()); |
| 187 |
mockClock.tick(1); |
| 188 |
assertEquals(3, webSocket.open.getCallCount()); |
| 189 |
|
| 190 |
// Simulate connection success. |
| 191 |
simulateOpenEvent(ws); |
| 192 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 193 |
assertEquals(3, webSocket.open.getCallCount()); |
| 194 |
|
| 195 |
// Make sure the reconnection has the same url and protocol. |
| 196 |
assertEquals(testUrl, ws.url); |
| 197 |
assertEquals(testProtocol, ws.protocol); |
| 198 |
|
| 199 |
// Ensure no further calls to open are made. |
| 200 |
mockClock.tick(linearBackOff(10)); |
| 201 |
assertEquals(3, webSocket.open.getCallCount()); |
| 202 |
} |
| 203 |
|
| 204 |
function testReconnectionWithFailureAfterOpen() { |
| 205 |
// Construct the web socket with a linear back-off. |
| 206 |
webSocket = new goog.net.WebSocket(true, fibonacciBackOff); |
| 207 |
|
| 208 |
// Record how many times open is called. |
| 209 |
pr.set(webSocket, 'open', goog.testing.recordFunction(webSocket.open)); |
| 210 |
|
| 211 |
// Open the web socket. |
| 212 |
webSocket.open(testUrl); |
| 213 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 214 |
assertEquals(1, webSocket.open.getCallCount()); |
| 215 |
assertFalse(webSocket.isOpen()); |
| 216 |
|
| 217 |
// Simulate connection success. |
| 218 |
var ws = webSocket.webSocket_; |
| 219 |
simulateOpenEvent(ws); |
| 220 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 221 |
assertEquals(1, webSocket.open.getCallCount()); |
| 222 |
|
| 223 |
// Let some time pass, then fail the connection. |
| 224 |
mockClock.tick(100000); |
| 225 |
simulateCloseEvent(ws); |
| 226 |
assertFalse(webSocket.isOpen()); |
| 227 |
assertEquals(1, webSocket.reconnectAttempt_); |
| 228 |
assertEquals(1, webSocket.open.getCallCount()); |
| 229 |
|
| 230 |
// Make sure the reconnect doesn't happen before it should. |
| 231 |
mockClock.tick(fibonacciBackOff(0) - 1); |
| 232 |
assertEquals(1, webSocket.open.getCallCount()); |
| 233 |
mockClock.tick(1); |
| 234 |
assertEquals(2, webSocket.open.getCallCount()); |
| 235 |
|
| 236 |
// Simulate connection success. |
| 237 |
ws = webSocket.webSocket_; |
| 238 |
simulateOpenEvent(ws); |
| 239 |
assertEquals(0, webSocket.reconnectAttempt_); |
| 240 |
assertEquals(2, webSocket.open.getCallCount()); |
| 241 |
|
| 242 |
// Ensure no further calls to open are made. |
| 243 |
mockClock.tick(fibonacciBackOff(10)); |
| 244 |
assertEquals(2, webSocket.open.getCallCount()); |
| 245 |
} |
| 246 |
|
| 247 |
function testExponentialBackOff() { |
| 248 |
assertEquals(1000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(0)); |
| 249 |
assertEquals(2000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(1)); |
| 250 |
assertEquals(4000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(2)); |
| 251 |
assertEquals(60000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(6)); |
| 252 |
assertEquals(60000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(7)); |
| 253 |
} |
| 254 |
|
| 255 |
function testEntryPointRegistry() { |
| 256 |
var monitor = new goog.debug.EntryPointMonitor(); |
| 257 |
var replacement = function() {}; |
| 258 |
monitor.wrap = |
| 259 |
goog.testing.recordFunction(goog.functions.constant(replacement)); |
| 260 |
|
| 261 |
goog.debug.entryPointRegistry.monitorAll(monitor); |
| 262 |
assertTrue(monitor.wrap.getCallCount() >= 1); |
| 263 |
assertEquals(replacement, goog.net.WebSocket.prototype.onOpen_); |
| 264 |
assertEquals(replacement, goog.net.WebSocket.prototype.onClose_); |
| 265 |
assertEquals(replacement, goog.net.WebSocket.prototype.onMessage_); |
| 266 |
assertEquals(replacement, goog.net.WebSocket.prototype.onError_); |
| 267 |
} |
| 268 |
|
| 269 |
function testErrorHandlerCalled() { |
| 270 |
var errorHandlerCalled = false; |
| 271 |
var errorHandler = |
| 272 |
new goog.debug.ErrorHandler(function() { errorHandlerCalled = true; }); |
| 273 |
goog.net.WebSocket.protectEntryPoints(errorHandler); |
| 274 |
|
| 275 |
webSocket = new goog.net.WebSocket(); |
| 276 |
goog.events.listenOnce( |
| 277 |
webSocket, goog.net.WebSocket.EventType.OPENED, function() { |
| 278 |
throw new Error(); |
| 279 |
}); |
| 280 |
|
| 281 |
webSocket.open(testUrl); |
| 282 |
var ws = webSocket.webSocket_; |
| 283 |
assertThrows(function() { simulateOpenEvent(ws); }); |
| 284 |
|
| 285 |
assertTrue( |
| 286 |
'Error handler callback should be called when registered as ' + |
| 287 |
'protecting the entry points.', |
| 288 |
errorHandlerCalled); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Simulates the browser firing the open event for the given web socket. |
| 294 |
* @param {MockWebSocket} ws The mock web socket. |
| 295 |
*/ |
| 296 |
function simulateOpenEvent(ws) { |
| 297 |
ws.readyState = goog.net.WebSocket.ReadyState_.OPEN; |
| 298 |
ws.onopen(); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
/** |
| 303 |
* Simulates the browser firing the close event for the given web socket. |
| 304 |
* @param {MockWebSocket} ws The mock web socket. |
| 305 |
*/ |
| 306 |
function simulateCloseEvent(ws) { |
| 307 |
ws.readyState = goog.net.WebSocket.ReadyState_.CLOSED; |
| 308 |
ws.onclose({data: 'mock close event'}); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/** |
| 313 |
* Strategy for reconnection that backs off linearly with a 1 second offset. |
| 314 |
* @param {number} attempt The number of reconnects since the last connection. |
| 315 |
* @return {number} The amount of time to the next reconnect, in milliseconds. |
| 316 |
*/ |
| 317 |
function linearBackOff(attempt) { |
| 318 |
return (attempt * 1000) + 1000; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/** |
| 323 |
* Strategy for reconnection that backs off with the fibonacci pattern. It is |
| 324 |
* offset by 5 seconds so the first attempt will happen after 5 seconds. |
| 325 |
* @param {number} attempt The number of reconnects since the last connection. |
| 326 |
* @return {number} The amount of time to the next reconnect, in milliseconds. |
| 327 |
*/ |
| 328 |
function fibonacciBackOff(attempt) { |
| 329 |
return (fibonacci(attempt) * 1000) + 5000; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* Computes the desired fibonacci number. |
| 335 |
* @param {number} n The nth desired fibonacci number. |
| 336 |
* @return {number} The nth fibonacci number. |
| 337 |
*/ |
| 338 |
function fibonacci(n) { |
| 339 |
if (n == 0) { |
| 340 |
return 0; |
| 341 |
} else if (n == 1) { |
| 342 |
return 1; |
| 343 |
} else { |
| 344 |
return fibonacci(n - 2) + fibonacci(n - 1); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
/** |
| 351 |
* Mock WebSocket constructor. |
| 352 |
* @param {string} url The url to the web socket server. |
| 353 |
* @param {string} protocol The protocol to use. |
| 354 |
* @constructor |
| 355 |
*/ |
| 356 |
MockWebSocket = function(url, protocol) { |
| 357 |
this.url = url; |
| 358 |
this.protocol = protocol; |
| 359 |
this.readyState = goog.net.WebSocket.ReadyState_.CONNECTING; |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
* Mocks out the close method of the WebSocket. |
| 365 |
*/ |
| 366 |
MockWebSocket.prototype.close = function() { |
| 367 |
this.readyState = goog.net.WebSocket.ReadyState_.CLOSING; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
/** |
| 372 |
* Mocks out the send method of the WebSocket. |
| 373 |
*/ |
| 374 |
MockWebSocket.prototype.send = function() { |
| 375 |
// Nothing to do here. |
| 376 |
}; |