Skip to content

Commit

Permalink
[test] add immediate and interval exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Sep 13, 2016
1 parent 5d7695a commit 3cc5607
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/test-immediate-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

if (process.env.hasOwnProperty('ASYNC_HOOK_TEST_CHILD')) {
const asyncHook = require('../');

asyncHook.enable();

setImmediate(function () {
throw new Error('test error');
});
} else {
const spawn = require('child_process').spawn;
const endpoint = require('endpoint');

const child = spawn(process.execPath, [__filename], {
env: Object.assign({ ASYNC_HOOK_TEST_CHILD: '' }, process.env),
stdio: ['ignore', 1, 'pipe']
});

let stderr = null;
child.stderr.pipe(endpoint(function (err, _stderr) {
if (err) throw err;
stderr = _stderr;
}));

child.once('close', function (statusCode) {
if (statusCode !== 1 || stderr.toString().indexOf('test error') === -1) {
process.stderr.write(stderr);
process.exit(statusCode);
}
});
}
32 changes: 32 additions & 0 deletions test/test-interval-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

if (process.env.hasOwnProperty('ASYNC_HOOK_TEST_CHILD')) {
const asyncHook = require('../');

asyncHook.enable();

setInterval(function () {
throw new Error('test error');
});
} else {
const spawn = require('child_process').spawn;
const endpoint = require('endpoint');

const child = spawn(process.execPath, [__filename], {
env: Object.assign({ ASYNC_HOOK_TEST_CHILD: '' }, process.env),
stdio: ['ignore', 1, 'pipe']
});

let stderr = null;
child.stderr.pipe(endpoint(function (err, _stderr) {
if (err) throw err;
stderr = _stderr;
}));

child.once('close', function (statusCode) {
if (statusCode !== 1 || stderr.toString().indexOf('test error') === -1) {
process.stderr.write(stderr);
process.exit(statusCode);
}
});
}

0 comments on commit 3cc5607

Please sign in to comment.