summaryrefslogtreecommitdiffstats
path: root/testing/xpcshell/node-http2/test/util.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:44:51 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:44:51 +0000
commit9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /testing/xpcshell/node-http2/test/util.js
parentInitial commit. (diff)
downloadthunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.tar.xz
thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/xpcshell/node-http2/test/util.js')
-rw-r--r--testing/xpcshell/node-http2/test/util.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/testing/xpcshell/node-http2/test/util.js b/testing/xpcshell/node-http2/test/util.js
new file mode 100644
index 0000000000..c612e38b14
--- /dev/null
+++ b/testing/xpcshell/node-http2/test/util.js
@@ -0,0 +1,89 @@
+var path = require('path');
+var fs = require('fs');
+var spawn = require('child_process').spawn;
+
+function noop() {}
+exports.noop = noop;
+
+if (process.env.HTTP2_LOG) {
+ var logOutput = process.stderr;
+ if (process.stderr.isTTY) {
+ var bin = path.resolve(path.dirname(require.resolve('bunyan')), '..', 'bin', 'bunyan');
+ if(bin && fs.existsSync(bin)) {
+ logOutput = spawn(bin, ['-o', 'short'], {
+ stdio: [null, process.stderr, process.stderr]
+ }).stdin;
+ }
+ }
+ exports.createLogger = function(name) {
+ return require('bunyan').createLogger({
+ name: name,
+ stream: logOutput,
+ level: process.env.HTTP2_LOG,
+ serializers: require('../lib/http').serializers
+ });
+ };
+ exports.log = exports.createLogger('test');
+ exports.clientLog = exports.createLogger('client');
+ exports.serverLog = exports.createLogger('server');
+} else {
+ exports.createLogger = function() {
+ return exports.log;
+ };
+ exports.log = exports.clientLog = exports.serverLog = {
+ fatal: noop,
+ error: noop,
+ warn : noop,
+ info : noop,
+ debug: noop,
+ trace: noop,
+
+ child: function() { return this; }
+ };
+}
+
+exports.callNTimes = function callNTimes(limit, done) {
+ if (limit === 0) {
+ done();
+ } else {
+ var i = 0;
+ return function() {
+ i += 1;
+ if (i === limit) {
+ done();
+ }
+ };
+ }
+};
+
+// Concatenate an array of buffers into a new buffer
+exports.concat = function concat(buffers) {
+ var size = 0;
+ for (var i = 0; i < buffers.length; i++) {
+ size += buffers[i].length;
+ }
+
+ var concatenated = Buffer.alloc(size);
+ for (var cursor = 0, j = 0; j < buffers.length; cursor += buffers[j].length, j++) {
+ buffers[j].copy(concatenated, cursor);
+ }
+
+ return concatenated;
+};
+
+exports.random = function random(min, max) {
+ return min + Math.floor(Math.random() * (max - min + 1));
+};
+
+// Concatenate an array of buffers and then cut them into random size buffers
+exports.shuffleBuffers = function shuffleBuffers(buffers) {
+ var concatenated = exports.concat(buffers), output = [], written = 0;
+
+ while (written < concatenated.length) {
+ var chunk_size = Math.min(concatenated.length - written, Math.ceil(Math.random()*20));
+ output.push(concatenated.slice(written, written + chunk_size));
+ written += chunk_size;
+ }
+
+ return output;
+};