]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.websock.js
Merge pull request #488 from kanaka/feature/more-perf-improvements
[mirror_novnc.git] / tests / test.websock.js
1 // requires local modules: websock, util
2 // requires test modules: fake.websocket, assertions
3 /* jshint expr: true */
4 var assert = chai.assert;
5 var expect = chai.expect;
6
7 describe('Websock', function() {
8 "use strict";
9
10 describe('Queue methods', function () {
11 var sock;
12 var RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
13
14 beforeEach(function () {
15 sock = new Websock();
16 // skip init
17 sock._allocate_buffers();
18 sock._rQ.set(RQ_TEMPLATE);
19 sock._rQlen = RQ_TEMPLATE.length;
20 });
21 describe('rQlen', function () {
22 it('should return the length of the receive queue', function () {
23 sock.set_rQi(0);
24
25 expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length);
26 });
27
28 it("should return the proper length if we read some from the receive queue", function () {
29 sock.set_rQi(1);
30
31 expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length - 1);
32 });
33 });
34
35 describe('rQpeek8', function () {
36 it('should peek at the next byte without poping it off the queue', function () {
37 var bef_len = sock.rQlen();
38 var peek = sock.rQpeek8();
39 expect(sock.rQpeek8()).to.equal(peek);
40 expect(sock.rQlen()).to.equal(bef_len);
41 });
42 });
43
44 describe('rQshift8', function () {
45 it('should pop a single byte from the receive queue', function () {
46 var peek = sock.rQpeek8();
47 var bef_len = sock.rQlen();
48 expect(sock.rQshift8()).to.equal(peek);
49 expect(sock.rQlen()).to.equal(bef_len - 1);
50 });
51 });
52
53 describe('rQshift16', function () {
54 it('should pop two bytes from the receive queue and return a single number', function () {
55 var bef_len = sock.rQlen();
56 var expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1];
57 expect(sock.rQshift16()).to.equal(expected);
58 expect(sock.rQlen()).to.equal(bef_len - 2);
59 });
60 });
61
62 describe('rQshift32', function () {
63 it('should pop four bytes from the receive queue and return a single number', function () {
64 var bef_len = sock.rQlen();
65 var expected = (RQ_TEMPLATE[0] << 24) +
66 (RQ_TEMPLATE[1] << 16) +
67 (RQ_TEMPLATE[2] << 8) +
68 RQ_TEMPLATE[3];
69 expect(sock.rQshift32()).to.equal(expected);
70 expect(sock.rQlen()).to.equal(bef_len - 4);
71 });
72 });
73
74 describe('rQshiftStr', function () {
75 it('should shift the given number of bytes off of the receive queue and return a string', function () {
76 var bef_len = sock.rQlen();
77 var bef_rQi = sock.get_rQi();
78 var shifted = sock.rQshiftStr(3);
79 expect(shifted).to.be.a('string');
80 expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3))));
81 expect(sock.rQlen()).to.equal(bef_len - 3);
82 });
83
84 it('should shift the entire rest of the queue off if no length is given', function () {
85 sock.rQshiftStr();
86 expect(sock.rQlen()).to.equal(0);
87 });
88 });
89
90 describe('rQshiftBytes', function () {
91 it('should shift the given number of bytes of the receive queue and return an array', function () {
92 var bef_len = sock.rQlen();
93 var bef_rQi = sock.get_rQi();
94 var shifted = sock.rQshiftBytes(3);
95 expect(shifted).to.be.an.instanceof(Uint8Array);
96 expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3));
97 expect(sock.rQlen()).to.equal(bef_len - 3);
98 });
99
100 it('should shift the entire rest of the queue off if no length is given', function () {
101 sock.rQshiftBytes();
102 expect(sock.rQlen()).to.equal(0);
103 });
104 });
105
106 describe('rQslice', function () {
107 beforeEach(function () {
108 sock.set_rQi(0);
109 });
110
111 it('should not modify the receive queue', function () {
112 var bef_len = sock.rQlen();
113 sock.rQslice(0, 2);
114 expect(sock.rQlen()).to.equal(bef_len);
115 });
116
117 it('should return an array containing the given slice of the receive queue', function () {
118 var sl = sock.rQslice(0, 2);
119 expect(sl).to.be.an.instanceof(Uint8Array);
120 expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
121 });
122
123 it('should use the rest of the receive queue if no end is given', function () {
124 var sl = sock.rQslice(1);
125 expect(sl).to.have.length(RQ_TEMPLATE.length - 1);
126 expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1));
127 });
128
129 it('should take the current rQi in to account', function () {
130 sock.set_rQi(1);
131 expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
132 });
133 });
134
135 describe('rQwait', function () {
136 beforeEach(function () {
137 sock.set_rQi(0);
138 });
139
140 it('should return true if there are not enough bytes in the receive queue', function () {
141 expect(sock.rQwait('hi', RQ_TEMPLATE.length + 1)).to.be.true;
142 });
143
144 it('should return false if there are enough bytes in the receive queue', function () {
145 expect(sock.rQwait('hi', RQ_TEMPLATE.length)).to.be.false;
146 });
147
148 it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
149 sock.set_rQi(5);
150 expect(sock.rQwait('hi', RQ_TEMPLATE.length, 4)).to.be.true;
151 expect(sock.get_rQi()).to.equal(1);
152 });
153
154 it('should raise an error if we try to go back more than possible', function () {
155 sock.set_rQi(5);
156 expect(function () { sock.rQwait('hi', RQ_TEMPLATE.length, 6); }).to.throw(Error);
157 });
158
159 it('should not reduce rQi if there are enough bytes', function () {
160 sock.set_rQi(5);
161 sock.rQwait('hi', 1, 6);
162 expect(sock.get_rQi()).to.equal(5);
163 });
164 });
165
166 describe('flush', function () {
167 beforeEach(function () {
168 sock._websocket = {
169 send: sinon.spy()
170 };
171 });
172
173 it('should actually send on the websocket if the websocket does not have too much buffered', function () {
174 sock.maxBufferedAmount = 10;
175 sock._websocket.bufferedAmount = 8;
176 sock._sQ = new Uint8Array([1, 2, 3]);
177 sock._sQlen = 3;
178 var encoded = sock._encode_message();
179
180 sock.flush();
181 expect(sock._websocket.send).to.have.been.calledOnce;
182 expect(sock._websocket.send).to.have.been.calledWith(encoded);
183 });
184
185 it('should return true if the websocket did not have too much buffered', function () {
186 sock.maxBufferedAmount = 10;
187 sock._websocket.bufferedAmount = 8;
188
189 expect(sock.flush()).to.be.true;
190 });
191
192 it('should not call send if we do not have anything queued up', function () {
193 sock._sQlen = 0;
194 sock.maxBufferedAmount = 10;
195 sock._websocket.bufferedAmount = 8;
196
197 sock.flush();
198
199 expect(sock._websocket.send).not.to.have.been.called;
200 });
201
202 it('should not send and return false if the websocket has too much buffered', function () {
203 sock.maxBufferedAmount = 10;
204 sock._websocket.bufferedAmount = 12;
205
206 expect(sock.flush()).to.be.false;
207 expect(sock._websocket.send).to.not.have.been.called;
208 });
209 });
210
211 describe('send', function () {
212 beforeEach(function () {
213 sock.flush = sinon.spy();
214 });
215
216 it('should add to the send queue', function () {
217 sock.send([1, 2, 3]);
218 var sq = sock.get_sQ();
219 expect(new Uint8Array(sq.buffer, sock._sQlen - 3, 3)).to.array.equal(new Uint8Array([1, 2, 3]));
220 });
221
222 it('should call flush', function () {
223 sock.send([1, 2, 3]);
224 expect(sock.flush).to.have.been.calledOnce;
225 });
226 });
227
228 describe('send_string', function () {
229 beforeEach(function () {
230 sock.send = sinon.spy();
231 });
232
233 it('should call send after converting the string to an array', function () {
234 sock.send_string("\x01\x02\x03");
235 expect(sock.send).to.have.been.calledWith([1, 2, 3]);
236 });
237 });
238 });
239
240 describe('lifecycle methods', function () {
241 var old_WS;
242 before(function () {
243 old_WS = WebSocket;
244 });
245
246 var sock;
247 beforeEach(function () {
248 sock = new Websock();
249 WebSocket = sinon.spy();
250 WebSocket.OPEN = old_WS.OPEN;
251 WebSocket.CONNECTING = old_WS.CONNECTING;
252 WebSocket.CLOSING = old_WS.CLOSING;
253 WebSocket.CLOSED = old_WS.CLOSED;
254
255 WebSocket.prototype.binaryType = 'arraybuffer';
256 });
257
258 describe('opening', function () {
259 it('should pick the correct protocols if none are given' , function () {
260
261 });
262
263 it('should open the actual websocket', function () {
264 sock.open('ws://localhost:8675', 'binary');
265 expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary');
266 });
267
268 it('should fail if we specify a protocol besides binary', function () {
269 expect(function () { sock.open('ws:///', 'base64'); }).to.throw(Error);
270 });
271
272 // it('should initialize the event handlers')?
273 });
274
275 describe('closing', function () {
276 beforeEach(function () {
277 sock.open('ws://');
278 sock._websocket.close = sinon.spy();
279 });
280
281 it('should close the actual websocket if it is open', function () {
282 sock._websocket.readyState = WebSocket.OPEN;
283 sock.close();
284 expect(sock._websocket.close).to.have.been.calledOnce;
285 });
286
287 it('should close the actual websocket if it is connecting', function () {
288 sock._websocket.readyState = WebSocket.CONNECTING;
289 sock.close();
290 expect(sock._websocket.close).to.have.been.calledOnce;
291 });
292
293 it('should not try to close the actual websocket if closing', function () {
294 sock._websocket.readyState = WebSocket.CLOSING;
295 sock.close();
296 expect(sock._websocket.close).not.to.have.been.called;
297 });
298
299 it('should not try to close the actual websocket if closed', function () {
300 sock._websocket.readyState = WebSocket.CLOSED;
301 sock.close();
302 expect(sock._websocket.close).not.to.have.been.called;
303 });
304
305 it('should reset onmessage to not call _recv_message', function () {
306 sinon.spy(sock, '_recv_message');
307 sock.close();
308 sock._websocket.onmessage(null);
309 try {
310 expect(sock._recv_message).not.to.have.been.called;
311 } finally {
312 sock._recv_message.restore();
313 }
314 });
315 });
316
317 describe('event handlers', function () {
318 beforeEach(function () {
319 sock._recv_message = sinon.spy();
320 sock.on('open', sinon.spy());
321 sock.on('close', sinon.spy());
322 sock.on('error', sinon.spy());
323 sock.open('ws://');
324 });
325
326 it('should call _recv_message on a message', function () {
327 sock._websocket.onmessage(null);
328 expect(sock._recv_message).to.have.been.calledOnce;
329 });
330
331 it('should fail if a protocol besides binary is requested', function () {
332 sock._websocket.protocol = 'base64';
333 expect(sock._websocket.onopen).to.throw(Error);
334 });
335
336 it('should assume binary if no protocol was available on opening', function () {
337 sock._websocket.protocol = null;
338 sock._websocket.onopen();
339 expect(sock._mode).to.equal('binary');
340 });
341
342 it('should call the open event handler on opening', function () {
343 sock._websocket.onopen();
344 expect(sock._eventHandlers.open).to.have.been.calledOnce;
345 });
346
347 it('should call the close event handler on closing', function () {
348 sock._websocket.onclose();
349 expect(sock._eventHandlers.close).to.have.been.calledOnce;
350 });
351
352 it('should call the error event handler on error', function () {
353 sock._websocket.onerror();
354 expect(sock._eventHandlers.error).to.have.been.calledOnce;
355 });
356 });
357
358 after(function () {
359 WebSocket = old_WS;
360 });
361 });
362
363 describe('WebSocket Receiving', function () {
364 var sock;
365 beforeEach(function () {
366 sock = new Websock();
367 sock._allocate_buffers();
368 });
369
370 it('should support adding binary Uint8Array data to the receive queue', function () {
371 var msg = { data: new Uint8Array([1, 2, 3]) };
372 sock._mode = 'binary';
373 sock._recv_message(msg);
374 expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
375 });
376
377 it('should call the message event handler if present', function () {
378 sock._eventHandlers.message = sinon.spy();
379 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
380 sock._mode = 'binary';
381 sock._recv_message(msg);
382 expect(sock._eventHandlers.message).to.have.been.calledOnce;
383 });
384
385 it('should not call the message event handler if there is nothing in the receive queue', function () {
386 sock._eventHandlers.message = sinon.spy();
387 var msg = { data: new Uint8Array([]).buffer };
388 sock._mode = 'binary';
389 sock._recv_message(msg);
390 expect(sock._eventHandlers.message).not.to.have.been.called;
391 });
392
393 it('should compact the receive queue', function () {
394 // NB(sross): while this is an internal implementation detail, it's important to
395 // test, otherwise the receive queue could become very large very quickly
396 sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
397 sock._rQlen = 6;
398 sock.set_rQi(6);
399 sock._rQmax = 3;
400 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
401 sock._mode = 'binary';
402 sock._recv_message(msg);
403 expect(sock._rQlen).to.equal(3);
404 expect(sock.get_rQi()).to.equal(0);
405 });
406
407 it('should call the error event handler on an exception', function () {
408 sock._eventHandlers.error = sinon.spy();
409 sock._eventHandlers.message = sinon.stub().throws();
410 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
411 sock._mode = 'binary';
412 sock._recv_message(msg);
413 expect(sock._eventHandlers.error).to.have.been.calledOnce;
414 });
415 });
416
417 describe('Data encoding', function () {
418 before(function () { FakeWebSocket.replace(); });
419 after(function () { FakeWebSocket.restore(); });
420
421 describe('as binary data', function () {
422 var sock;
423 beforeEach(function () {
424 sock = new Websock();
425 sock.open('ws://', 'binary');
426 sock._websocket._open();
427 });
428
429 it('should only send the send queue up to the send queue length', function () {
430 sock._sQ = new Uint8Array([1, 2, 3, 4, 5]);
431 sock._sQlen = 3;
432 var res = sock._encode_message();
433 expect(res).to.array.equal(new Uint8Array([1, 2, 3]));
434 });
435
436 it('should properly pass the encoded data off to the actual WebSocket', function () {
437 sock.send([1, 2, 3]);
438 expect(sock._websocket._get_sent_data()).to.array.equal(new Uint8Array([1, 2, 3]));
439 });
440 });
441 });
442 });