Skip to content

Commit

Permalink
test: improve async hooks test error messages
Browse files Browse the repository at this point in the history
Improve error messages in the async hooks tests, mostly by removing
unhelpful `message` parameters for assertions.

PR-URL: #13243
Reviewed-By: Kunal Pathak <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
  • Loading branch information
addaleax authored and jasnell committed Jun 5, 2017
1 parent 135f4e6 commit 8c7f9da
Show file tree
Hide file tree
Showing 36 changed files with 234 additions and 306 deletions.
8 changes: 4 additions & 4 deletions test/async-hooks/init-hooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
// Flags: --expose-gc

require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const util = require('util');
const print = process._rawDebug;
require('../common');

if (typeof global.gc === 'function') {
(function exity(cntr) {
Expand Down Expand Up @@ -109,7 +109,7 @@ class ActivityCollector {
}
if (violations.length) {
console.error(violations.join('\n'));
assert.fail(violations.length, 0, 'Failed sanity check');
assert.fail(violations.length, 0, `Failed sanity checks: ${violations}`);
}
}

Expand Down Expand Up @@ -151,8 +151,8 @@ class ActivityCollector {
this._activities.set(uid, stub);
return stub;
} else {
const err = new Error('Found a handle who\'s ' + hook +
' hook was invoked but not it\'s init hook');
const err = new Error(`Found a handle whose ${hook}` +
' hook was invoked but not its init hook');
// Don't throw if we see invocations due to an assertion in a test
// failing since we want to list the assertion failure instead
if (/process\._fatalException/.test(err.stack)) return null;
Expand Down
18 changes: 8 additions & 10 deletions test/async-hooks/test-connection.ssl.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,22 @@ function createServerConnection(
const sc1 = createServerConnection(common.mustCall(onfirstHandShake));

let as = hooks.activitiesOfTypes('SSLCONNECTION');
assert.strictEqual(as.length, 1,
'one CONNECTION after first connection created');
assert.strictEqual(as.length, 1);
const f1 = as[0];
assert.strictEqual(f1.type, 'SSLCONNECTION', 'connection');
assert.strictEqual(typeof f1.uid, 'number', 'uid is a number');
assert.strictEqual(typeof f1.triggerId, 'number', 'triggerId is a number');
assert.strictEqual(f1.type, 'SSLCONNECTION');
assert.strictEqual(typeof f1.uid, 'number');
assert.strictEqual(typeof f1.triggerId, 'number');
checkInvocations(f1, { init: 1 }, 'first connection, when first created');

// creating second server connection
const sc2 = createServerConnection(common.mustCall(onsecondHandShake));

as = hooks.activitiesOfTypes('SSLCONNECTION');
assert.strictEqual(as.length, 2,
'two SSLCONNECTIONs after second connection created');
assert.strictEqual(as.length, 2);
const f2 = as[1];
assert.strictEqual(f2.type, 'SSLCONNECTION', 'connection');
assert.strictEqual(typeof f2.uid, 'number', 'uid is a number');
assert.strictEqual(typeof f2.triggerId, 'number', 'triggerId is a number');
assert.strictEqual(f2.type, 'SSLCONNECTION');
assert.strictEqual(typeof f2.uid, 'number');
assert.strictEqual(typeof f2.triggerId, 'number');
checkInvocations(f1, { init: 1 }, 'first connection, when second created');
checkInvocations(f2, { init: 1 }, 'second connection, when second created');

Expand Down
8 changes: 4 additions & 4 deletions test/async-hooks/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function onexit() {
hooks.sanityCheck('PBKDF2REQUEST');

const as = hooks.activitiesOfTypes('PBKDF2REQUEST');
assert.strictEqual(as.length, 1, 'one activity');
assert.strictEqual(as.length, 1);

const a = as[0];
assert.strictEqual(a.type, 'PBKDF2REQUEST', 'random byte request');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
assert.strictEqual(a.type, 'PBKDF2REQUEST');
assert.strictEqual(typeof a.uid, 'number');
assert.strictEqual(a.triggerId, 1);
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
'when process exits');
}
8 changes: 4 additions & 4 deletions test/async-hooks/test-crypto-randomBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ function onexit() {
hooks.sanityCheck('RANDOMBYTESREQUEST');

const as = hooks.activitiesOfTypes('RANDOMBYTESREQUEST');
assert.strictEqual(as.length, 1, 'one activity');
assert.strictEqual(as.length, 1);

const a = as[0];
assert.strictEqual(a.type, 'RANDOMBYTESREQUEST', 'random byte request');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
assert.strictEqual(a.type, 'RANDOMBYTESREQUEST');
assert.strictEqual(typeof a.uid, 'number');
assert.strictEqual(a.triggerId, 1);
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
'when process exits');
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ if (process.argv[2] === 'child') {
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });

child.on('close', common.mustCall((code) => {
assert.strictEqual(code, 1, 'exit code 1');
assert.strictEqual(code, 1);
assert.ok(heartbeatMsg.test(outData.toString()),
'did not crash until we reached offending line of code');
'did not crash until we reached offending line of code ' +
`(found ${outData})`);
assert.ok(corruptedMsg.test(errData.toString()),
'printed error contains corrupted message');
'printed error contains corrupted message ' +
`(found ${errData})`);
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ if (process.argv[2] === 'child') {
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });

child.on('close', common.mustCall((code) => {
assert.strictEqual(code, 1, 'exit code 1');
assert.strictEqual(code, 1);
assert.ok(heartbeatMsg.test(outData.toString()),
'did not crash until we reached offending line of code');
'did not crash until we reached offending line of code ' +
`(found ${outData})`);
assert.ok(corruptedMsg.test(errData.toString()),
'printed error contains corrupted message');
'printed error contains corrupted message ' +
`(found ${errData})`);
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ if (process.argv[2] === 'child') {
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });

child.on('close', common.mustCall((code) => {
assert.strictEqual(code, 1, 'exit code 1');
assert.strictEqual(code, 1);
assert.ok(heartbeatMsg.test(outData.toString()),
'did not crash until we reached offending line of code');
'did not crash until we reached offending line of code ' +
`(found ${outData})`);
assert.ok(corruptedMsg.test(errData.toString()),
'printed error contains corrupted message');
'printed error contains corrupted message ' +
`(found ${errData})`);
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ if (process.argv[2] === 'child') {
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });

child.on('close', common.mustCall((code) => {
assert.strictEqual(code, 1, 'exit code 1');
assert.strictEqual(code, 1);
assert.ok(heartbeatMsg.test(outData.toString()),
'did not crash until we reached offending line of code');
'did not crash until we reached offending line of code ' +
`(found ${outData})`);
assert.ok(corruptedMsg.test(errData.toString()),
'printed error contains corrupted message');
'printed error contains corrupted message ' +
`(found ${errData})`);
}));
}
17 changes: 7 additions & 10 deletions test/async-hooks/test-embedder.api.async-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ const alcaEvent = new AsyncResource('alcazares', alcaTriggerId);
const alcazaresActivities = hooks.activitiesOfTypes([ 'alcazares' ]);

// alcazares event was constructed and thus only has an `init` call
assert.strictEqual(alcazaresActivities.length, 1,
'one alcazares activity after one was constructed');
assert.strictEqual(alcazaresActivities.length, 1);
const alcazares = alcazaresActivities[0];
assert.strictEqual(alcazares.type, 'alcazares', 'alcazares');
assert.strictEqual(typeof alcazares.uid, 'number', 'uid is a number');
assert.strictEqual(alcazares.triggerId, alcaTriggerId,
'triggerId is the one supplied');
assert.strictEqual(alcazares.type, 'alcazares');
assert.strictEqual(typeof alcazares.uid, 'number');
assert.strictEqual(alcazares.triggerId, alcaTriggerId);
checkInvocations(alcazares, { init: 1 }, 'alcazares constructed');

alcaEvent.emitBefore();
Expand All @@ -52,10 +50,9 @@ function tick1() {
const pobEvent = new AsyncResource('poblado', pobTriggerId);
const pobladoActivities = hooks.activitiesOfTypes([ 'poblado' ]);
const poblado = pobladoActivities[0];
assert.strictEqual(poblado.type, 'poblado', 'poblado');
assert.strictEqual(typeof poblado.uid, 'number', 'uid is a number');
assert.strictEqual(poblado.triggerId, pobTriggerId,
'triggerId is the one supplied');
assert.strictEqual(poblado.type, 'poblado');
assert.strictEqual(typeof poblado.uid, 'number');
assert.strictEqual(poblado.triggerId, pobTriggerId);
checkInvocations(poblado, { init: 1 }, 'poblado constructed');
pobEvent.emitBefore();
checkInvocations(poblado, { init: 1, before: 1 },
Expand Down
44 changes: 15 additions & 29 deletions test/async-hooks/test-enable-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,17 @@ function onfirstImmediate() {
const as1 = hook1.activitiesOfTypes(types);
const as2 = hook2.activitiesOfTypes(types);
const as3 = hook3.activitiesOfTypes(types);
assert.strictEqual(as1.length, 1,
'hook1 captured one immediate on first callback');
assert.strictEqual(as1.length, 1);
// hook2 was not enabled yet .. it is enabled after hook3's "before" completed
assert.strictEqual(as2.length, 0,
'hook2 captured no immediate on first callback');
assert.strictEqual(as3.length, 1,
'hook3 captured one immediate on first callback');
assert.strictEqual(as2.length, 0);
assert.strictEqual(as3.length, 1);

// Check that hook1 and hook3 captured the same Immediate and that it is valid
const firstImmediate = as1[0];
assert.strictEqual(as3[0].uid, as1[0].uid,
'hook1 and hook3 captured same first immediate');
assert.strictEqual(firstImmediate.type, 'Immediate', 'immediate');
assert.strictEqual(typeof firstImmediate.uid, 'number', 'uid is a number');
assert.strictEqual(typeof firstImmediate.triggerId,
'number', 'triggerId is a number');
assert.strictEqual(as3[0].uid, as1[0].uid);
assert.strictEqual(firstImmediate.type, 'Immediate');
assert.strictEqual(typeof firstImmediate.uid, 'number');
assert.strictEqual(typeof firstImmediate.triggerId, 'number');
checkInvocations(as1[0], { init: 1, before: 1 },
'hook1[0]: on first immediate');
checkInvocations(as3[0], { init: 1, before: 1 },
Expand All @@ -187,15 +182,9 @@ function onsecondImmediate() {
const as1 = hook1.activitiesOfTypes(types);
const as2 = hook2.activitiesOfTypes(types);
const as3 = hook3.activitiesOfTypes(types);
assert.strictEqual(
as1.length, 2,
'hook1 captured first and second immediate on second callback');
assert.strictEqual(
as2.length, 2,
'hook2 captured first and second immediate on second callback');
assert.strictEqual(
as3.length, 2,
'hook3 captured first and second immediate on second callback');
assert.strictEqual(as1.length, 2);
assert.strictEqual(as2.length, 2);
assert.strictEqual(as3.length, 2);

// Assign the info collected by each hook for each immediate for easier
// reference.
Expand All @@ -210,14 +199,11 @@ function onsecondImmediate() {

// Check that all hooks captured the same Immediate and that it is valid
const secondImmediate = hook1Second;
assert.strictEqual(hook2Second.uid, hook3Second.uid,
'hook2 and hook3 captured same second immediate');
assert.strictEqual(hook1Second.uid, hook3Second.uid,
'hook1 and hook3 captured same second immediate');
assert.strictEqual(secondImmediate.type, 'Immediate', 'immediate');
assert.strictEqual(typeof secondImmediate.uid, 'number', 'uid is a number');
assert.strictEqual(typeof secondImmediate.triggerId, 'number',
'triggerId is a number');
assert.strictEqual(hook2Second.uid, hook3Second.uid);
assert.strictEqual(hook1Second.uid, hook3Second.uid);
assert.strictEqual(secondImmediate.type, 'Immediate');
assert.strictEqual(typeof secondImmediate.uid, 'number');
assert.strictEqual(typeof secondImmediate.triggerId, 'number');

checkInvocations(hook1First, { init: 1, before: 1, after: 1, destroy: 1 },
'hook1First: on second immediate');
Expand Down
8 changes: 4 additions & 4 deletions test/async-hooks/test-fseventwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ function onexit() {
hooks.sanityCheck('FSEVENTWRAP');

const as = hooks.activitiesOfTypes('FSEVENTWRAP');
assert.strictEqual(as.length, 1, 'one activity');
assert.strictEqual(as.length, 1);

const a = as[0];
assert.strictEqual(a.type, 'FSEVENTWRAP', 'fs event wrap');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
assert.strictEqual(a.type, 'FSEVENTWRAP');
assert.strictEqual(typeof a.uid, 'number');
assert.strictEqual(a.triggerId, 1);
checkInvocations(a, { init: 1, destroy: 1 }, 'when process exits');
}
6 changes: 3 additions & 3 deletions test/async-hooks/test-fsreqwrap-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function onexit() {
hooks.sanityCheck('FSREQWRAP');

const as = hooks.activitiesOfTypes('FSREQWRAP');
assert.strictEqual(as.length, 1, 'one activity');
assert.strictEqual(as.length, 1);

const a = as[0];
assert.strictEqual(a.type, 'FSREQWRAP', 'fs req wrap');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.type, 'FSREQWRAP');
assert.strictEqual(typeof a.uid, 'number');
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
'when process exits');
}
6 changes: 3 additions & 3 deletions test/async-hooks/test-fsreqwrap-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function onread() {
let lastParent = 1;
for (let i = 0; i < as.length; i++) {
const a = as[i];
assert.strictEqual(a.type, 'FSREQWRAP', 'fs req wrap');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.triggerId, lastParent, 'parent uid 1');
assert.strictEqual(a.type, 'FSREQWRAP');
assert.strictEqual(typeof a.uid, 'number');
assert.strictEqual(a.triggerId, lastParent);
lastParent = a.uid;
}
checkInvocations(as[0], { init: 1, before: 1, after: 1, destroy: 1 },
Expand Down
8 changes: 4 additions & 4 deletions test/async-hooks/test-getaddrinforeqwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function onlookup(err_, ip, family) {
// tests to run offline (lookup will fail in that case and the err be set);

const as = hooks.activitiesOfTypes('GETADDRINFOREQWRAP');
assert.strictEqual(as.length, 1, 'one activity');
assert.strictEqual(as.length, 1);

const a = as[0];
assert.strictEqual(a.type, 'GETADDRINFOREQWRAP', 'getaddrinforeq wrap');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
assert.strictEqual(a.type, 'GETADDRINFOREQWRAP');
assert.strictEqual(typeof a.uid, 'number');
assert.strictEqual(a.triggerId, 1);
checkInvocations(a, { init: 1, before: 1 }, 'while in onlookup callback');
tick(2);
}
Expand Down
8 changes: 4 additions & 4 deletions test/async-hooks/test-getnameinforeqwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function onlookupService(err_, ip, family) {
// tests to run offline (lookup will fail in that case and the err be set)

const as = hooks.activitiesOfTypes('GETNAMEINFOREQWRAP');
assert.strictEqual(as.length, 1, 'one activity');
assert.strictEqual(as.length, 1);

const a = as[0];
assert.strictEqual(a.type, 'GETNAMEINFOREQWRAP', 'getnameinforeq wrap');
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
assert.strictEqual(a.type, 'GETNAMEINFOREQWRAP');
assert.strictEqual(typeof a.uid, 'number');
assert.strictEqual(a.triggerId, 1);
checkInvocations(a, { init: 1, before: 1 },
'while in onlookupService callback');
tick(2);
Expand Down
9 changes: 3 additions & 6 deletions test/async-hooks/test-httpparser.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ const parser = new HTTPParser(REQUEST);
const as = hooks.activitiesOfTypes('HTTPPARSER');
const httpparser = as[0];

assert.strictEqual(
as.length, 1,
'1 httpparser created synchronously when creating new httpparser');
assert.strictEqual(typeof httpparser.uid, 'number', 'uid is a number');
assert.strictEqual(typeof httpparser.triggerId,
'number', 'triggerId is a number');
assert.strictEqual(as.length, 1);
assert.strictEqual(typeof httpparser.uid, 'number');
assert.strictEqual(typeof httpparser.triggerId, 'number');
checkInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser');

parser[kOnHeadersComplete] = common.mustCall(onheadersComplete);
Expand Down
9 changes: 3 additions & 6 deletions test/async-hooks/test-httpparser.response.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ const parser = new HTTPParser(RESPONSE);
const as = hooks.activitiesOfTypes('HTTPPARSER');
const httpparser = as[0];

assert.strictEqual(
as.length, 1,
'1 httpparser created synchronously when creating new httpparser');
assert.strictEqual(typeof httpparser.uid, 'number', 'uid is a number');
assert.strictEqual(typeof httpparser.triggerId,
'number', 'triggerId is a number');
assert.strictEqual(as.length, 1);
assert.strictEqual(typeof httpparser.uid, 'number');
assert.strictEqual(typeof httpparser.triggerId, 'number');
checkInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser');

parser[kOnHeadersComplete] = common.mustCall(onheadersComplete);
Expand Down
Loading

0 comments on commit 8c7f9da

Please sign in to comment.