github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/sockjs-client-1.1.0/tests/lib/batch-tests.js (about) 1 'use strict'; 2 var expect = require('expect.js') 3 , testUtils = require('./test-utils') 4 , debug = require('debug')('sockjs-client:tests:batch') 5 ; 6 7 function batchFactory(transport, messages, url) { 8 return function(done) { 9 var test = this.runnable(); 10 var title = test.fullTitle(); 11 debug('start', title); 12 this.timeout(10000); 13 var sjs = testUtils.newSockJs(url + '/echo', transport); 14 var counter = 0; 15 sjs.onopen = function () { 16 messages.forEach(function (m) { 17 sjs.send(m); 18 }); 19 }; 20 sjs.onmessage = function (e) { 21 try { 22 expect(e.data).to.eql(messages[counter]); 23 } catch (err) { 24 done(err); 25 sjs.close(); 26 return; 27 } 28 29 counter++; 30 if (counter === messages.length) { 31 sjs.close(); 32 } 33 }; 34 sjs.onclose = function (e) { 35 if (test.timedOut || test.duration) { 36 return; 37 } 38 39 try { 40 expect(e.code).to.equal(1000); 41 expect(counter).to.equal(messages.length); 42 } catch (err) { 43 done(err); 44 return; 45 } 46 done(); 47 debug('end', title); 48 }; 49 }; 50 } 51 52 module.exports.largeMessage = function(url, transport) { 53 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')]; 54 it('large message (batch)', batchFactory(transport, messages, url)); 55 }; 56 57 function amplifyFactory(transport, messages, url) { 58 return function(done) { 59 var test = this.runnable(); 60 var title = test.fullTitle(); 61 debug('start', title); 62 this.timeout(10000); 63 var sjs = testUtils.newSockJs(url + '/amplify', transport); 64 var counter = 0; 65 sjs.onopen = function () { 66 messages.forEach(function (m) { 67 sjs.send(m); 68 }); 69 }; 70 sjs.onmessage = function (e) { 71 try { 72 expect(e.data).to.have.length(Math.pow(2, messages[counter])); 73 } catch (err) { 74 done(err); 75 sjs.close(); 76 return; 77 } 78 counter++; 79 if (counter === messages.length) { 80 sjs.close(); 81 } 82 }; 83 sjs.onclose = function (e) { 84 if (test.timedOut || test.duration) { 85 return; 86 } 87 88 try { 89 expect(e.code).to.equal(1000); 90 expect(counter).to.equal(messages.length); 91 } catch (err) { 92 done(err); 93 return; 94 } 95 96 done(); 97 debug('end', title); 98 }; 99 }; 100 } 101 102 module.exports.largeDownload = function(url, transport) { 103 var messages = [1, 2, 4, 8, 13, 15, 15]; 104 it('large download', amplifyFactory(transport, messages, url)); 105 };