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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial