Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit e27971e

Browse files
author
Ian Wehrman
committed
Add binary command response unit tests
1 parent 94d06f2 commit e27971e

2 files changed

Lines changed: 167 additions & 2 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, node: true */
25+
/*global */
26+
27+
(function () {
28+
"use strict";
29+
30+
/**
31+
* @private
32+
* @type {DomainManager}
33+
* The DomainManager passed in at init.
34+
*/
35+
var _domainManager = null;
36+
37+
38+
/**
39+
* @private
40+
* @type {Buffer}
41+
*/
42+
var _buffer = new Buffer(18);
43+
44+
// write some bytes into the buffer with varied alignments
45+
_buffer.writeUInt8(1, 0);
46+
_buffer.writeUInt32LE(Math.pow(2, 32) - 1, 1);
47+
_buffer.writeFloatBE(3.141592, 5);
48+
_buffer.writeDoubleLE(Number.MAX_VALUE, 9);
49+
_buffer.writeInt8(-128, 17);
50+
51+
/**
52+
* @private
53+
* @return {Buffer}
54+
*/
55+
function _getBufferSync() {
56+
return _buffer;
57+
}
58+
59+
/**
60+
* @private
61+
* @param {function(?string, Buffer=)} callback
62+
*/
63+
function _getBufferAsync(callback) {
64+
process.nextTick(function () {
65+
callback(null, _buffer);
66+
});
67+
}
68+
69+
/**
70+
* @param {DomainManager} DomainManager The DomainManager for the server
71+
*/
72+
function init(DomainManager) {
73+
_domainManager = DomainManager;
74+
if (!_domainManager.hasDomain("test")) {
75+
_domainManager.registerDomain("test", {major: 0, minor: 1});
76+
}
77+
_domainManager.registerCommand(
78+
"binaryTest",
79+
"getBufferSync",
80+
_getBufferSync,
81+
false,
82+
"Get a byte array synchronously",
83+
[],
84+
{name: "bytes", type: "Buffer"}
85+
);
86+
_domainManager.registerCommand(
87+
"binaryTest",
88+
"getBufferAsync",
89+
_getBufferAsync,
90+
true,
91+
"Get a byte array asynchronously",
92+
[],
93+
{name: "bytes", type: "Buffer"}
94+
);
95+
}
96+
97+
exports.init = init;
98+
99+
}());

test/spec/NodeConnection-test.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true,
2626
indent: 4, maxerr: 50 */
2727
/*global define, describe, it, xit, expect, beforeEach, afterEach, waits,
28-
waitsFor, runs, $, brackets, waitsForDone */
28+
waitsFor, runs, $, brackets, waitsForDone, ArrayBuffer, DataView */
2929

3030
define(function (require, exports, module) {
3131
"use strict";
@@ -399,5 +399,71 @@ define(function (require, exports, module) {
399399
});
400400

401401
});
402+
403+
it("should receive synchronous binary command responses", function () {
404+
var connection = createConnection();
405+
var commandDeferred = null;
406+
var result = null;
407+
runConnectAndWait(connection, false);
408+
runLoadDomainsAndWait(connection, ["BinaryTestCommands"], false);
409+
runs(function () {
410+
commandDeferred = connection.domains.binaryTest.getBufferSync();
411+
commandDeferred.done(function (response) {
412+
result = response;
413+
});
414+
});
415+
waitsFor(
416+
function () {
417+
return commandDeferred &&
418+
commandDeferred.state() === "resolved" &&
419+
result;
420+
},
421+
CONNECTION_TIMEOUT
422+
);
423+
runs(function () {
424+
var view = new DataView(result);
425+
426+
expect(result instanceof ArrayBuffer).toBe(true);
427+
expect(result.byteLength).toBe(18);
428+
expect(view.getUint8(0)).toBe(1);
429+
expect(view.getUint32(1)).toBe(4294967295);
430+
expect(view.getFloat32(5, false)).toBe(3.141592025756836);
431+
expect(view.getFloat64(9, true)).toBe(1.7976931348623157e+308);
432+
expect(view.getInt8(17)).toBe(-128);
433+
});
434+
});
435+
436+
it("should receive asynchronous binary command response", function () {
437+
var connection = createConnection();
438+
var commandDeferred = null;
439+
var result = null;
440+
runConnectAndWait(connection, false);
441+
runLoadDomainsAndWait(connection, ["BinaryTestCommands"], false);
442+
runs(function () {
443+
commandDeferred = connection.domains.binaryTest.getBufferAsync();
444+
commandDeferred.done(function (response) {
445+
result = response;
446+
});
447+
});
448+
waitsFor(
449+
function () {
450+
return commandDeferred &&
451+
commandDeferred.state() === "resolved" &&
452+
result;
453+
},
454+
CONNECTION_TIMEOUT
455+
);
456+
runs(function () {
457+
var view = new DataView(result);
458+
459+
expect(result instanceof ArrayBuffer).toBe(true);
460+
expect(result.byteLength).toBe(18);
461+
expect(view.getUint8(0)).toBe(1);
462+
expect(view.getUint32(1)).toBe(4294967295);
463+
expect(view.getFloat32(5, false)).toBe(3.141592025756836);
464+
expect(view.getFloat64(9, true)).toBe(1.7976931348623157e+308);
465+
expect(view.getInt8(17)).toBe(-128);
466+
});
467+
});
402468
});
403-
});
469+
});

0 commit comments

Comments
 (0)