testing/xpcshell/node-http2/test/util.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 var path = require('path');
michael@0 2 var fs = require('fs');
michael@0 3 var spawn = require('child_process').spawn;
michael@0 4
michael@0 5 function noop() {}
michael@0 6 exports.noop = noop;
michael@0 7
michael@0 8 if (process.env.HTTP2_LOG) {
michael@0 9 var logOutput = process.stderr;
michael@0 10 if (process.stderr.isTTY) {
michael@0 11 var bin = path.resolve(path.dirname(require.resolve('bunyan')), '..', 'bin', 'bunyan');
michael@0 12 if(bin && fs.existsSync(bin)) {
michael@0 13 logOutput = spawn(bin, ['-o', 'short'], {
michael@0 14 stdio: [null, process.stderr, process.stderr]
michael@0 15 }).stdin;
michael@0 16 }
michael@0 17 }
michael@0 18 exports.createLogger = function(name) {
michael@0 19 return require('bunyan').createLogger({
michael@0 20 name: name,
michael@0 21 stream: logOutput,
michael@0 22 level: process.env.HTTP2_LOG,
michael@0 23 serializers: require('../lib/http').serializers
michael@0 24 });
michael@0 25 };
michael@0 26 exports.log = exports.createLogger('test');
michael@0 27 } else {
michael@0 28 exports.createLogger = function() {
michael@0 29 return exports.log;
michael@0 30 };
michael@0 31 exports.log = {
michael@0 32 fatal: noop,
michael@0 33 error: noop,
michael@0 34 warn : noop,
michael@0 35 info : noop,
michael@0 36 debug: noop,
michael@0 37 trace: noop,
michael@0 38
michael@0 39 child: function() { return this; }
michael@0 40 };
michael@0 41 }
michael@0 42
michael@0 43 exports.callNTimes = function callNTimes(limit, done) {
michael@0 44 if (limit === 0) {
michael@0 45 done();
michael@0 46 } else {
michael@0 47 var i = 0;
michael@0 48 return function() {
michael@0 49 i += 1;
michael@0 50 if (i === limit) {
michael@0 51 done();
michael@0 52 }
michael@0 53 };
michael@0 54 }
michael@0 55 };
michael@0 56
michael@0 57 // Concatenate an array of buffers into a new buffer
michael@0 58 exports.concat = function concat(buffers) {
michael@0 59 var size = 0;
michael@0 60 for (var i = 0; i < buffers.length; i++) {
michael@0 61 size += buffers[i].length;
michael@0 62 }
michael@0 63
michael@0 64 var concatenated = new Buffer(size);
michael@0 65 for (var cursor = 0, j = 0; j < buffers.length; cursor += buffers[j].length, j++) {
michael@0 66 buffers[j].copy(concatenated, cursor);
michael@0 67 }
michael@0 68
michael@0 69 return concatenated;
michael@0 70 };
michael@0 71
michael@0 72 exports.random = function random(min, max) {
michael@0 73 return min + Math.floor(Math.random() * (max - min + 1));
michael@0 74 };
michael@0 75
michael@0 76 // Concatenate an array of buffers and then cut them into random size buffers
michael@0 77 exports.shuffleBuffers = function shuffleBuffers(buffers) {
michael@0 78 var concatenated = exports.concat(buffers), output = [], written = 0;
michael@0 79
michael@0 80 while (written < concatenated.length) {
michael@0 81 var chunk_size = Math.min(concatenated.length - written, Math.ceil(Math.random()*20));
michael@0 82 output.push(concatenated.slice(written, written + chunk_size));
michael@0 83 written += chunk_size;
michael@0 84 }
michael@0 85
michael@0 86 return output;
michael@0 87 }

mercurial