1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/xpcshell/node-http2/test/util.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +var path = require('path'); 1.5 +var fs = require('fs'); 1.6 +var spawn = require('child_process').spawn; 1.7 + 1.8 +function noop() {} 1.9 +exports.noop = noop; 1.10 + 1.11 +if (process.env.HTTP2_LOG) { 1.12 + var logOutput = process.stderr; 1.13 + if (process.stderr.isTTY) { 1.14 + var bin = path.resolve(path.dirname(require.resolve('bunyan')), '..', 'bin', 'bunyan'); 1.15 + if(bin && fs.existsSync(bin)) { 1.16 + logOutput = spawn(bin, ['-o', 'short'], { 1.17 + stdio: [null, process.stderr, process.stderr] 1.18 + }).stdin; 1.19 + } 1.20 + } 1.21 + exports.createLogger = function(name) { 1.22 + return require('bunyan').createLogger({ 1.23 + name: name, 1.24 + stream: logOutput, 1.25 + level: process.env.HTTP2_LOG, 1.26 + serializers: require('../lib/http').serializers 1.27 + }); 1.28 + }; 1.29 + exports.log = exports.createLogger('test'); 1.30 +} else { 1.31 + exports.createLogger = function() { 1.32 + return exports.log; 1.33 + }; 1.34 + exports.log = { 1.35 + fatal: noop, 1.36 + error: noop, 1.37 + warn : noop, 1.38 + info : noop, 1.39 + debug: noop, 1.40 + trace: noop, 1.41 + 1.42 + child: function() { return this; } 1.43 + }; 1.44 +} 1.45 + 1.46 +exports.callNTimes = function callNTimes(limit, done) { 1.47 + if (limit === 0) { 1.48 + done(); 1.49 + } else { 1.50 + var i = 0; 1.51 + return function() { 1.52 + i += 1; 1.53 + if (i === limit) { 1.54 + done(); 1.55 + } 1.56 + }; 1.57 + } 1.58 +}; 1.59 + 1.60 +// Concatenate an array of buffers into a new buffer 1.61 +exports.concat = function concat(buffers) { 1.62 + var size = 0; 1.63 + for (var i = 0; i < buffers.length; i++) { 1.64 + size += buffers[i].length; 1.65 + } 1.66 + 1.67 + var concatenated = new Buffer(size); 1.68 + for (var cursor = 0, j = 0; j < buffers.length; cursor += buffers[j].length, j++) { 1.69 + buffers[j].copy(concatenated, cursor); 1.70 + } 1.71 + 1.72 + return concatenated; 1.73 +}; 1.74 + 1.75 +exports.random = function random(min, max) { 1.76 + return min + Math.floor(Math.random() * (max - min + 1)); 1.77 +}; 1.78 + 1.79 +// Concatenate an array of buffers and then cut them into random size buffers 1.80 +exports.shuffleBuffers = function shuffleBuffers(buffers) { 1.81 + var concatenated = exports.concat(buffers), output = [], written = 0; 1.82 + 1.83 + while (written < concatenated.length) { 1.84 + var chunk_size = Math.min(concatenated.length - written, Math.ceil(Math.random()*20)); 1.85 + output.push(concatenated.slice(written, written + chunk_size)); 1.86 + written += chunk_size; 1.87 + } 1.88 + 1.89 + return output; 1.90 +}