[Pkg-javascript-commits] [sockjs-client] 147/350: Batch and amplify tests
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:03:56 UTC 2016
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.
commit 1d386023a80104d9276cae7d7636606fe3a3868f
Author: Bryce Kahle <bkahle at gmail.com>
Date: Wed Oct 15 18:21:32 2014 -0400
Batch and amplify tests
---
tests/html/lib/tests.js | 168 --------------------------------------------
tests/html/lib/testutils.js | 48 -------------
tests/lib/batch-tests.js | 64 +++++++++++++++++
tests/lib/echo-tests.js | 19 +++--
tests/lib/transports.js | 70 +++++++++++++++---
5 files changed, 135 insertions(+), 234 deletions(-)
diff --git a/tests/html/lib/tests.js b/tests/html/lib/tests.js
deleted file mode 100644
index 8a5eae9..0000000
--- a/tests/html/lib/tests.js
+++ /dev/null
@@ -1,168 +0,0 @@
-'use strict';
-/* global suite, test, client_opts */
-var arrIndexOf, batch_factory_factory, batch_factory_factory_amp, echo_factory_factory, escapable, factor_batch_large, factor_batch_large_amp, factor_echo_basic, factor_echo_from_child, factor_echo_large_message, factor_echo_rich, factor_echo_special_chars, factor_echo_unicode, factor_echo_utf_encoding, factor_echo_utf_encoding_simple, factor_server_close, factor_user_close, generate_killer_string, test_protocol_messages;
-
-var assert = require('assert');
-var u = require('../../../lib/utils');
-var testutils = require('./testutils');
-var protocols = require('../../../lib/protocols');
-
-var TIMEOUT_MS = 10000;
-
-batch_factory_factory = function(protocol, messages) {
- return function(done) {
- this.timeout(TIMEOUT_MS);
- this.runnable().globals(['_sockjs_global', '_jp', '_send_form', '_send_area']);
- var counter, r;
- //expect(3 + messages.length);
- r = testutils.newSockJS('/echo', protocol);
- assert.ok(r);
- counter = 0;
- r.onopen = function(e) {
- var msg, _i, _len;
- assert.ok(true);
- for (_i = 0, _len = messages.length; _i < _len; _i++) {
- msg = messages[_i];
- r.send(msg);
- }
- };
- r.onmessage = function(e) {
- assert.equal(e.data, messages[counter]);
- counter += 1;
- if (counter === messages.length) r.close();
- };
- r.onclose = function(e) {
- if (counter !== messages.length) {
- assert.ok(false, "Transport closed prematurely. " + e);
- } else {
- assert.ok(true);
- }
- done();
- };
- };
-};
-
-factor_batch_large = function(protocol) {
- var messages;
- messages = [new Array(Math.pow(2, 1)).join('x'), new Array(Math.pow(2, 2)).join('x'), new Array(Math.pow(2, 4)).join('x'), new Array(Math.pow(2, 8)).join('x'), new Array(Math.pow(2, 13)).join('x'), new Array(Math.pow(2, 13)).join('x')];
- return batch_factory_factory(protocol, messages);
-};
-
-batch_factory_factory_amp = function(protocol, messages) {
- return function(done) {
- this.timeout(TIMEOUT_MS);
- this.runnable().globals(['_sockjs_global', '_jp', '_send_form', '_send_area']);
- var counter, r;
- //expect(3 + messages.length);
- r = testutils.newSockJS('/amplify', protocol);
- assert.ok(r);
- counter = 0;
- r.onopen = function(e) {
- var msg, _i, _len;
- assert.ok(true);
- for (_i = 0, _len = messages.length; _i < _len; _i++) {
- msg = messages[_i];
- r.send('' + msg);
- }
- };
- r.onmessage = function(e) {
- assert.equal(e.data.length, Math.pow(2, messages[counter]), e.data);
- counter += 1;
- if (counter === messages.length) r.close();
- };
- r.onclose = function(e) {
- if (counter !== messages.length) {
- assert.ok(false, "Transport closed prematurely. " + e);
- } else {
- assert.ok(true);
- }
- done();
- };
- };
-};
-
-factor_batch_large_amp = function(protocol) {
- var messages;
- messages = [1, 2, 4, 8, 13, 15, 15];
- return batch_factory_factory_amp(protocol, messages);
-};
-
-
-
-factor_user_close = function(protocol) {
- return function(done) {
- this.runnable().globals(['_sockjs_global', '_jp', '_send_form', '_send_area']);
- var counter, r;
- //expect(5);
- r = testutils.newSockJS('/echo', protocol);
- assert.ok(r);
- counter = 0;
- r.onopen = function(e) {
- counter += 1;
- assert.ok(counter === 1);
- r.close(3000, "User message");
- assert.ok(counter === 1);
- };
- r.onmessage = function() {
- assert.ok(false);
- counter += 1;
- };
- r.onclose = function(e) {
- counter += 1;
- u.log('user_close ' + e.code + ' ' + e.reason);
- assert.equal(e.wasClean, true);
- assert.ok(counter === 2);
- done();
- };
- };
-};
-
-factor_server_close = function(protocol) {
- return function(done) {
- this.runnable().globals(['_sockjs_global', '_jp', '_send_form', '_send_area']);
- var r;
- //expect(5);
- r = testutils.newSockJS('/close', protocol);
- assert.ok(r);
- r.onopen = function(e) {
- assert.ok(true);
- };
- r.onmessage = function(e) {
- assert.ok(false);
- };
- r.onclose = function(e) {
- assert.equal(e.code, 3000);
- assert.equal(e.reason, "Go away!");
- assert.equal(e.wasClean, true);
- done();
- };
- };
-};
-
-test_protocol_messages = function(protocol) {
- suite(protocol);
- if (client_opts.disabled_transports && arrIndexOf(client_opts.disabled_transports, protocol) !== -1) {
- test("[disabled by config]", function() {
- assert.ok(true, 'Disabled by config: "' + protocol + '"');
- });
- } else {
- test("echo1", factor_echo_basic(protocol));
- test("echo2", factor_echo_rich(protocol));
- test("echo from child", factor_echo_from_child(protocol));
- test("unicode", factor_echo_unicode(protocol));
- test("utf encoding 0x00-0xFF", factor_echo_utf_encoding_simple(protocol));
- test("utf encoding killer message", factor_echo_utf_encoding(protocol));
- test("special_chars", factor_echo_special_chars(protocol));
- test("large message (ping-pong)", factor_echo_large_message(protocol));
- test("large message (batch)", factor_batch_large(protocol));
- test("large download", factor_batch_large_amp(protocol));
- test("user close", factor_user_close(protocol));
- test("server close", factor_server_close(protocol));
- }
-};
-
-var validProtocols = protocols(client_opts.url, [], { cookie_needed: false, null_origin: false });
-for (var i = 0; i < validProtocols.length; i++) {
- var Protocol = validProtocols[i];
- test_protocol_messages(Protocol.transportName);
-}
diff --git a/tests/html/lib/testutils.js b/tests/html/lib/testutils.js
deleted file mode 100644
index a13bb2a..0000000
--- a/tests/html/lib/testutils.js
+++ /dev/null
@@ -1,48 +0,0 @@
-'use strict';
-/* global jQuery, client_opts, SockJS */
-
-var random = require('../../../lib/utils/random');
-
-// May be used by htmlfile jsonp and transports.
-var MPrefix = '_sockjs_global';
-var createHook = function() {
- var windowId = 'a' + random.string(8);
- if (!(MPrefix in window)) {
- var map = {};
- window[MPrefix] = function(windowId) {
- if (!(windowId in map)) {
- map[windowId] = {
- id: windowId,
- del: function() {delete map[windowId];}
- };
- }
- return map[windowId];
- };
- }
- return window[MPrefix](windowId);
-};
-
-module.exports = {
-
- newIframe: function(path) {
- var err, hook;
- if (!path) {
- path = '/iframe.html';
- }
- hook = createHook();
- err = function() {
- return u.log('iframe error. bad.');
- };
- hook.iobj = u.createIframe(path + '?a=' + Math.random() + '#' + hook.id, err);
- return hook;
- },
-
- newSockJS: function(path, protocol) {
- var options, url;
- url = /^http/.test(path) ? path : client_opts.url + path;
- options = jQuery.extend({}, client_opts.sockjs_opts);
- if (protocol) options.protocols_whitelist = [protocol];
- return new SockJS(url, null, options);
- }
-
-};
\ No newline at end of file
diff --git a/tests/lib/batch-tests.js b/tests/lib/batch-tests.js
new file mode 100644
index 0000000..66ce6f3
--- /dev/null
+++ b/tests/lib/batch-tests.js
@@ -0,0 +1,64 @@
+'use strict';
+var expect = require('expect.js')
+ , testUtils = require('./test-utils')
+ ;
+
+function batchFactory(transport, messages) {
+ return function(done) {
+ this.timeout(10000);
+ var sjs = testUtils.newSockJs('/echo', transport);
+ var counter = 0;
+ sjs.onopen = function () {
+ messages.forEach(function (m) {
+ sjs.send(m);
+ });
+ };
+ sjs.onmessage = function (e) {
+ expect(e.data).to.eql(messages[counter]);
+ counter++;
+ if (counter === messages.length) {
+ sjs.close();
+ }
+ };
+ sjs.onclose = function (e) {
+ expect(e.code).to.equal(1000);
+ expect(counter).to.equal(messages.length);
+ done();
+ };
+ };
+}
+
+module.exports.largeMessage = function(transport) {
+ var messages = [new Array(Math.pow(2, 1)).join('x'), new Array(Math.pow(2, 2)).join('x'), new Array(Math.pow(2, 4)).join('x'), new Array(Math.pow(2, 8)).join('x'), new Array(Math.pow(2, 13)).join('x'), new Array(Math.pow(2, 13)).join('x')];
+ it('large message (batch)', batchFactory(transport, messages));
+};
+
+function amplifyFactory(transport, messages) {
+ return function(done) {
+ this.timeout(10000);
+ var sjs = testUtils.newSockJs('/amplify', transport);
+ var counter = 0;
+ sjs.onopen = function () {
+ messages.forEach(function (m) {
+ sjs.send(m);
+ });
+ };
+ sjs.onmessage = function (e) {
+ expect(e.data).to.have.length(Math.pow(2, messages[counter]));
+ counter++;
+ if (counter === messages.length) {
+ sjs.close();
+ }
+ };
+ sjs.onclose = function (e) {
+ expect(e.code).to.equal(1000);
+ expect(counter).to.equal(messages.length);
+ done();
+ };
+ };
+}
+
+module.exports.largeDownload = function(transport) {
+ var messages = [1, 2, 4, 8, 13, 15, 15];
+ it('large download', amplifyFactory(transport, messages));
+};
diff --git a/tests/lib/echo-tests.js b/tests/lib/echo-tests.js
index 41e4c58..fb364aa 100644
--- a/tests/lib/echo-tests.js
+++ b/tests/lib/echo-tests.js
@@ -3,8 +3,7 @@ var expect = require('expect.js')
, testUtils = require('./test-utils')
;
-
-module.exports.echoFactory = function echoFactory(transport, messages) {
+function echoFactory(transport, messages) {
return function (done) {
this.timeout(10000);
var msgs = messages.slice(0);
@@ -29,11 +28,11 @@ module.exports.echoFactory = function echoFactory(transport, messages) {
done();
};
};
-};
+}
module.exports.echoBasic = function echoBasic(transport) {
var messages = ['data'];
- it('echo basic', module.exports.echoFactory(transport, messages));
+ it('echo basic', echoFactory(transport, messages));
};
module.exports.echoRich = function echoRich(transport) {
@@ -43,22 +42,22 @@ module.exports.echoRich = function echoRich(transport) {
b: 2
}
];
- it('echo rich', module.exports.echoFactory(transport, messages));
+ it('echo rich', echoFactory(transport, messages));
};
module.exports.echoUnicode = function echoUnicode(transport) {
var messages = ["Τη γλώσσα μου έδωσαν ελληνική το σπίτι φτωχικό στις αμμουδιές του ", "ღმერთსი შემვედრე, ნუთუ კვლა დამხსნას სოფლისა შრომასა, ცეცხლს, წყალს", "⠊⠀⠉⠁⠝⠀⠑⠁⠞⠀⠛⠇⠁⠎⠎⠀⠁⠝⠙⠀⠊⠞⠀⠙⠕⠑⠎⠝⠞⠀⠓⠥⠗⠞⠀⠍⠑", "Би шил идэй чадна, надад хортой биш", "을", "나는 유리를 먹을 수 있어요. 그래도 아프지 않아요", "ฉันกินกระจกได้ แต่มันไม่ทำให้ฉันเจ็บฉันกินกระจกได้ แต่มันไม่ทำให้ฉันเจ็บ", "Ég get etið gler án þess að meiða mig.", "Mogę jeść szkło, i mi nie szkodzi.", "\ufffd\u10102\u2f877", "Начало музыкальной карьеры\nБритни [...]
- it('unicode', module.exports.echoFactory(transport, messages));
+ it('unicode', echoFactory(transport, messages));
};
module.exports.echoSpecialChars = function echoSpecialChars(transport) {
var messages = [" ", "\u0000", "\xff", "\xff\x00", "\x00\xff", " \r ", " \n ", " \r\n ", "\r\n", "", "message\t", "\tmessage", "message ", " message", "message\r", "\rmessage", "message\n", "\nmessage", "message\xff", "\xffmessage", "A", "b", "c", "d", "e", "\ufffd", "\ufffd\u0000", "message\ufffd", "\ufffdmessage"];
- it('special chars', module.exports.echoFactory(transport, messages));
+ it('special chars', echoFactory(transport, messages));
};
module.exports.echoLargeMessage = function echoLargeMessage(transport) {
var messages = [new Array(Math.pow(2, 1)).join('x'), new Array(Math.pow(2, 2)).join('x'), new Array(Math.pow(2, 4)).join('x'), new Array(Math.pow(2, 8)).join('x'), new Array(Math.pow(2, 13)).join('x'), new Array(Math.pow(2, 13)).join('x')];
- it('large message (ping-pong)', module.exports.echoFactory(transport, messages));
+ it('large message (ping-pong)', echoFactory(transport, messages));
};
module.exports.echoUtfEncodingSimple = function echoUtfEncodingSimple(transport) {
@@ -66,7 +65,7 @@ module.exports.echoUtfEncodingSimple = function echoUtfEncodingSimple(transport)
for (var i = 0; i <= 256; i++) {
chars.push(String.fromCharCode(i));
}
- it('echo utf encoding 0x00-0xFF', module.exports.echoFactory(transport, [chars.join('')] ));
+ it('echo utf encoding 0x00-0xFF', echoFactory(transport, [chars.join('')] ));
};
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u2000-\u20ff\ufeff\ufff0-\uffff\x00-\x1f\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5 [...]
@@ -80,7 +79,7 @@ module.exports.echoUtfEncoding = function echoUtfEncoding(transport) {
message.push(a);
return '';
});
- it('echo utf encoding killer message', module.exports.echoFactory(transport, [message.join('')] ));
+ it('echo utf encoding killer message', echoFactory(transport, [message.join('')] ));
};
module.exports.echoFromChild = function echoFromChild(transport) {
diff --git a/tests/lib/transports.js b/tests/lib/transports.js
index 63b3b06..2887b95 100644
--- a/tests/lib/transports.js
+++ b/tests/lib/transports.js
@@ -5,6 +5,7 @@ var expect = require('expect.js')
, transportList = require('../../lib/transport-list')
, testUtils = require('./test-utils')
, echoTests = require('./echo-tests')
+ , batchTests = require('./batch-tests')
;
describe('Transports', function () {
@@ -37,14 +38,67 @@ describe('Transports', function () {
return;
}
- echoTests.echoBasic(Trans.transportName);
- echoTests.echoRich(Trans.transportName);
- echoTests.echoUnicode(Trans.transportName);
- echoTests.echoSpecialChars(Trans.transportName);
- echoTests.echoLargeMessage(Trans.transportName);
- echoTests.echoUtfEncodingSimple(Trans.transportName);
- echoTests.echoUtfEncoding(Trans.transportName);
- echoTests.echoFromChild(Trans.transportName);
+ var transport = Trans.transportName;
+ echoTests.echoBasic(transport);
+ echoTests.echoRich(transport);
+ echoTests.echoUnicode(transport);
+ echoTests.echoSpecialChars(transport);
+ echoTests.echoLargeMessage(transport);
+ echoTests.echoUtfEncodingSimple(transport);
+ echoTests.echoUtfEncoding(transport);
+ echoTests.echoFromChild(transport);
+
+ batchTests.largeMessage(transport);
+ batchTests.largeDownload(transport);
+
+ userClose(transport);
+ serverClose(transport);
});
});
});
+
+function userClose(transport) {
+ it('user close', function (done) {
+ var sjs = testUtils.newSockJs('/echo', transport);
+ expect(sjs).to.be.ok();
+ var counter = 0;
+
+ sjs.onopen = function() {
+ counter++;
+ expect(counter).to.equal(1);
+ sjs.close(3000, 'User message');
+ expect(counter).to.equal(1);
+ };
+ sjs.onmessage = function() {
+ expect().fail();
+ counter++;
+ };
+ sjs.onclose = function(e) {
+ counter++;
+ expect(e.wasClean).to.equal(true);
+ expect(counter).to.equal(2);
+ done();
+ };
+ });
+}
+
+function serverClose(transport) {
+ it('server close', function (done) {
+ var sjs = testUtils.newSockJs('/close', transport);
+ expect(sjs).to.be.ok();
+ var i = 0;
+ sjs.onopen = function() {
+ i++;
+ };
+ sjs.onmessage = function() {
+ expect().fail();
+ };
+ sjs.onclose = function(e) {
+ expect(i).to.equal(1);
+ expect(e.code).to.equal(3000);
+ expect(e.reason).to.equal('Go away!');
+ expect(e.wasClean).to.equal(true);
+ done();
+ };
+ });
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git
More information about the Pkg-javascript-commits
mailing list