Skip to content

Commit 8c7f9da

Browse files
addaleaxjasnell
authored andcommitted
test: improve async hooks test error messages
Improve error messages in the async hooks tests, mostly by removing unhelpful `message` parameters for assertions. PR-URL: #13243 Reviewed-By: Kunal Pathak <kunal.pathak@microsoft.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent 135f4e6 commit 8c7f9da

36 files changed

+234
-306
lines changed

test/async-hooks/init-hooks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22
// Flags: --expose-gc
33

4+
require('../common');
45
const assert = require('assert');
56
const async_hooks = require('async_hooks');
67
const util = require('util');
78
const print = process._rawDebug;
8-
require('../common');
99

1010
if (typeof global.gc === 'function') {
1111
(function exity(cntr) {
@@ -109,7 +109,7 @@ class ActivityCollector {
109109
}
110110
if (violations.length) {
111111
console.error(violations.join('\n'));
112-
assert.fail(violations.length, 0, 'Failed sanity check');
112+
assert.fail(violations.length, 0, `Failed sanity checks: ${violations}`);
113113
}
114114
}
115115

@@ -151,8 +151,8 @@ class ActivityCollector {
151151
this._activities.set(uid, stub);
152152
return stub;
153153
} else {
154-
const err = new Error('Found a handle who\'s ' + hook +
155-
' hook was invoked but not it\'s init hook');
154+
const err = new Error(`Found a handle whose ${hook}` +
155+
' hook was invoked but not its init hook');
156156
// Don't throw if we see invocations due to an assertion in a test
157157
// failing since we want to list the assertion failure instead
158158
if (/process\._fatalException/.test(err.stack)) return null;

test/async-hooks/test-connection.ssl.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,22 @@ function createServerConnection(
3838
const sc1 = createServerConnection(common.mustCall(onfirstHandShake));
3939

4040
let as = hooks.activitiesOfTypes('SSLCONNECTION');
41-
assert.strictEqual(as.length, 1,
42-
'one CONNECTION after first connection created');
41+
assert.strictEqual(as.length, 1);
4342
const f1 = as[0];
44-
assert.strictEqual(f1.type, 'SSLCONNECTION', 'connection');
45-
assert.strictEqual(typeof f1.uid, 'number', 'uid is a number');
46-
assert.strictEqual(typeof f1.triggerId, 'number', 'triggerId is a number');
43+
assert.strictEqual(f1.type, 'SSLCONNECTION');
44+
assert.strictEqual(typeof f1.uid, 'number');
45+
assert.strictEqual(typeof f1.triggerId, 'number');
4746
checkInvocations(f1, { init: 1 }, 'first connection, when first created');
4847

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

5251
as = hooks.activitiesOfTypes('SSLCONNECTION');
53-
assert.strictEqual(as.length, 2,
54-
'two SSLCONNECTIONs after second connection created');
52+
assert.strictEqual(as.length, 2);
5553
const f2 = as[1];
56-
assert.strictEqual(f2.type, 'SSLCONNECTION', 'connection');
57-
assert.strictEqual(typeof f2.uid, 'number', 'uid is a number');
58-
assert.strictEqual(typeof f2.triggerId, 'number', 'triggerId is a number');
54+
assert.strictEqual(f2.type, 'SSLCONNECTION');
55+
assert.strictEqual(typeof f2.uid, 'number');
56+
assert.strictEqual(typeof f2.triggerId, 'number');
5957
checkInvocations(f1, { init: 1 }, 'first connection, when second created');
6058
checkInvocations(f2, { init: 1 }, 'second connection, when second created');
6159

test/async-hooks/test-crypto-pbkdf2.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ function onexit() {
3131
hooks.sanityCheck('PBKDF2REQUEST');
3232

3333
const as = hooks.activitiesOfTypes('PBKDF2REQUEST');
34-
assert.strictEqual(as.length, 1, 'one activity');
34+
assert.strictEqual(as.length, 1);
3535

3636
const a = as[0];
37-
assert.strictEqual(a.type, 'PBKDF2REQUEST', 'random byte request');
38-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
39-
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
37+
assert.strictEqual(a.type, 'PBKDF2REQUEST');
38+
assert.strictEqual(typeof a.uid, 'number');
39+
assert.strictEqual(a.triggerId, 1);
4040
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
4141
'when process exits');
4242
}

test/async-hooks/test-crypto-randomBytes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ function onexit() {
3232
hooks.sanityCheck('RANDOMBYTESREQUEST');
3333

3434
const as = hooks.activitiesOfTypes('RANDOMBYTESREQUEST');
35-
assert.strictEqual(as.length, 1, 'one activity');
35+
assert.strictEqual(as.length, 1);
3636

3737
const a = as[0];
38-
assert.strictEqual(a.type, 'RANDOMBYTESREQUEST', 'random byte request');
39-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
40-
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
38+
assert.strictEqual(a.type, 'RANDOMBYTESREQUEST');
39+
assert.strictEqual(typeof a.uid, 'number');
40+
assert.strictEqual(a.triggerId, 1);
4141
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
4242
'when process exits');
4343
}

test/async-hooks/test-embedder.api.async-event.after-on-destroyed.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ if (process.argv[2] === 'child') {
3939
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });
4040

4141
child.on('close', common.mustCall((code) => {
42-
assert.strictEqual(code, 1, 'exit code 1');
42+
assert.strictEqual(code, 1);
4343
assert.ok(heartbeatMsg.test(outData.toString()),
44-
'did not crash until we reached offending line of code');
44+
'did not crash until we reached offending line of code ' +
45+
`(found ${outData})`);
4546
assert.ok(corruptedMsg.test(errData.toString()),
46-
'printed error contains corrupted message');
47+
'printed error contains corrupted message ' +
48+
`(found ${errData})`);
4749
}));
4850
}

test/async-hooks/test-embedder.api.async-event.before-on-destroyed.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ if (process.argv[2] === 'child') {
3939
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });
4040

4141
child.on('close', common.mustCall((code) => {
42-
assert.strictEqual(code, 1, 'exit code 1');
42+
assert.strictEqual(code, 1);
4343
assert.ok(heartbeatMsg.test(outData.toString()),
44-
'did not crash until we reached offending line of code');
44+
'did not crash until we reached offending line of code ' +
45+
`(found ${outData})`);
4546
assert.ok(corruptedMsg.test(errData.toString()),
46-
'printed error contains corrupted message');
47+
'printed error contains corrupted message ' +
48+
`(found ${errData})`);
4749
}));
4850
}

test/async-hooks/test-embedder.api.async-event.improper-order.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ if (process.argv[2] === 'child') {
3737
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });
3838

3939
child.on('close', common.mustCall((code) => {
40-
assert.strictEqual(code, 1, 'exit code 1');
40+
assert.strictEqual(code, 1);
4141
assert.ok(heartbeatMsg.test(outData.toString()),
42-
'did not crash until we reached offending line of code');
42+
'did not crash until we reached offending line of code ' +
43+
`(found ${outData})`);
4344
assert.ok(corruptedMsg.test(errData.toString()),
44-
'printed error contains corrupted message');
45+
'printed error contains corrupted message ' +
46+
`(found ${errData})`);
4547
}));
4648
}

test/async-hooks/test-embedder.api.async-event.improper-unwind.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ if (process.argv[2] === 'child') {
4646
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });
4747

4848
child.on('close', common.mustCall((code) => {
49-
assert.strictEqual(code, 1, 'exit code 1');
49+
assert.strictEqual(code, 1);
5050
assert.ok(heartbeatMsg.test(outData.toString()),
51-
'did not crash until we reached offending line of code');
51+
'did not crash until we reached offending line of code ' +
52+
`(found ${outData})`);
5253
assert.ok(corruptedMsg.test(errData.toString()),
53-
'printed error contains corrupted message');
54+
'printed error contains corrupted message ' +
55+
`(found ${errData})`);
5456
}));
5557
}

test/async-hooks/test-embedder.api.async-event.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ const alcaEvent = new AsyncResource('alcazares', alcaTriggerId);
1919
const alcazaresActivities = hooks.activitiesOfTypes([ 'alcazares' ]);
2020

2121
// alcazares event was constructed and thus only has an `init` call
22-
assert.strictEqual(alcazaresActivities.length, 1,
23-
'one alcazares activity after one was constructed');
22+
assert.strictEqual(alcazaresActivities.length, 1);
2423
const alcazares = alcazaresActivities[0];
25-
assert.strictEqual(alcazares.type, 'alcazares', 'alcazares');
26-
assert.strictEqual(typeof alcazares.uid, 'number', 'uid is a number');
27-
assert.strictEqual(alcazares.triggerId, alcaTriggerId,
28-
'triggerId is the one supplied');
24+
assert.strictEqual(alcazares.type, 'alcazares');
25+
assert.strictEqual(typeof alcazares.uid, 'number');
26+
assert.strictEqual(alcazares.triggerId, alcaTriggerId);
2927
checkInvocations(alcazares, { init: 1 }, 'alcazares constructed');
3028

3129
alcaEvent.emitBefore();
@@ -52,10 +50,9 @@ function tick1() {
5250
const pobEvent = new AsyncResource('poblado', pobTriggerId);
5351
const pobladoActivities = hooks.activitiesOfTypes([ 'poblado' ]);
5452
const poblado = pobladoActivities[0];
55-
assert.strictEqual(poblado.type, 'poblado', 'poblado');
56-
assert.strictEqual(typeof poblado.uid, 'number', 'uid is a number');
57-
assert.strictEqual(poblado.triggerId, pobTriggerId,
58-
'triggerId is the one supplied');
53+
assert.strictEqual(poblado.type, 'poblado');
54+
assert.strictEqual(typeof poblado.uid, 'number');
55+
assert.strictEqual(poblado.triggerId, pobTriggerId);
5956
checkInvocations(poblado, { init: 1 }, 'poblado constructed');
6057
pobEvent.emitBefore();
6158
checkInvocations(poblado, { init: 1, before: 1 },

test/async-hooks/test-enable-disable.js

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,17 @@ function onfirstImmediate() {
145145
const as1 = hook1.activitiesOfTypes(types);
146146
const as2 = hook2.activitiesOfTypes(types);
147147
const as3 = hook3.activitiesOfTypes(types);
148-
assert.strictEqual(as1.length, 1,
149-
'hook1 captured one immediate on first callback');
148+
assert.strictEqual(as1.length, 1);
150149
// hook2 was not enabled yet .. it is enabled after hook3's "before" completed
151-
assert.strictEqual(as2.length, 0,
152-
'hook2 captured no immediate on first callback');
153-
assert.strictEqual(as3.length, 1,
154-
'hook3 captured one immediate on first callback');
150+
assert.strictEqual(as2.length, 0);
151+
assert.strictEqual(as3.length, 1);
155152

156153
// Check that hook1 and hook3 captured the same Immediate and that it is valid
157154
const firstImmediate = as1[0];
158-
assert.strictEqual(as3[0].uid, as1[0].uid,
159-
'hook1 and hook3 captured same first immediate');
160-
assert.strictEqual(firstImmediate.type, 'Immediate', 'immediate');
161-
assert.strictEqual(typeof firstImmediate.uid, 'number', 'uid is a number');
162-
assert.strictEqual(typeof firstImmediate.triggerId,
163-
'number', 'triggerId is a number');
155+
assert.strictEqual(as3[0].uid, as1[0].uid);
156+
assert.strictEqual(firstImmediate.type, 'Immediate');
157+
assert.strictEqual(typeof firstImmediate.uid, 'number');
158+
assert.strictEqual(typeof firstImmediate.triggerId, 'number');
164159
checkInvocations(as1[0], { init: 1, before: 1 },
165160
'hook1[0]: on first immediate');
166161
checkInvocations(as3[0], { init: 1, before: 1 },
@@ -187,15 +182,9 @@ function onsecondImmediate() {
187182
const as1 = hook1.activitiesOfTypes(types);
188183
const as2 = hook2.activitiesOfTypes(types);
189184
const as3 = hook3.activitiesOfTypes(types);
190-
assert.strictEqual(
191-
as1.length, 2,
192-
'hook1 captured first and second immediate on second callback');
193-
assert.strictEqual(
194-
as2.length, 2,
195-
'hook2 captured first and second immediate on second callback');
196-
assert.strictEqual(
197-
as3.length, 2,
198-
'hook3 captured first and second immediate on second callback');
185+
assert.strictEqual(as1.length, 2);
186+
assert.strictEqual(as2.length, 2);
187+
assert.strictEqual(as3.length, 2);
199188

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

211200
// Check that all hooks captured the same Immediate and that it is valid
212201
const secondImmediate = hook1Second;
213-
assert.strictEqual(hook2Second.uid, hook3Second.uid,
214-
'hook2 and hook3 captured same second immediate');
215-
assert.strictEqual(hook1Second.uid, hook3Second.uid,
216-
'hook1 and hook3 captured same second immediate');
217-
assert.strictEqual(secondImmediate.type, 'Immediate', 'immediate');
218-
assert.strictEqual(typeof secondImmediate.uid, 'number', 'uid is a number');
219-
assert.strictEqual(typeof secondImmediate.triggerId, 'number',
220-
'triggerId is a number');
202+
assert.strictEqual(hook2Second.uid, hook3Second.uid);
203+
assert.strictEqual(hook1Second.uid, hook3Second.uid);
204+
assert.strictEqual(secondImmediate.type, 'Immediate');
205+
assert.strictEqual(typeof secondImmediate.uid, 'number');
206+
assert.strictEqual(typeof secondImmediate.triggerId, 'number');
221207

222208
checkInvocations(hook1First, { init: 1, before: 1, after: 1, destroy: 1 },
223209
'hook1First: on second immediate');

test/async-hooks/test-fseventwrap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ function onexit() {
2323
hooks.sanityCheck('FSEVENTWRAP');
2424

2525
const as = hooks.activitiesOfTypes('FSEVENTWRAP');
26-
assert.strictEqual(as.length, 1, 'one activity');
26+
assert.strictEqual(as.length, 1);
2727

2828
const a = as[0];
29-
assert.strictEqual(a.type, 'FSEVENTWRAP', 'fs event wrap');
30-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
31-
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
29+
assert.strictEqual(a.type, 'FSEVENTWRAP');
30+
assert.strictEqual(typeof a.uid, 'number');
31+
assert.strictEqual(a.triggerId, 1);
3232
checkInvocations(a, { init: 1, destroy: 1 }, 'when process exits');
3333
}

test/async-hooks/test-fsreqwrap-access.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function onexit() {
2727
hooks.sanityCheck('FSREQWRAP');
2828

2929
const as = hooks.activitiesOfTypes('FSREQWRAP');
30-
assert.strictEqual(as.length, 1, 'one activity');
30+
assert.strictEqual(as.length, 1);
3131

3232
const a = as[0];
33-
assert.strictEqual(a.type, 'FSREQWRAP', 'fs req wrap');
34-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
33+
assert.strictEqual(a.type, 'FSREQWRAP');
34+
assert.strictEqual(typeof a.uid, 'number');
3535
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
3636
'when process exits');
3737
}

test/async-hooks/test-fsreqwrap-readFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function onread() {
1717
let lastParent = 1;
1818
for (let i = 0; i < as.length; i++) {
1919
const a = as[i];
20-
assert.strictEqual(a.type, 'FSREQWRAP', 'fs req wrap');
21-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
22-
assert.strictEqual(a.triggerId, lastParent, 'parent uid 1');
20+
assert.strictEqual(a.type, 'FSREQWRAP');
21+
assert.strictEqual(typeof a.uid, 'number');
22+
assert.strictEqual(a.triggerId, lastParent);
2323
lastParent = a.uid;
2424
}
2525
checkInvocations(as[0], { init: 1, before: 1, after: 1, destroy: 1 },

test/async-hooks/test-getaddrinforeqwrap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function onlookup(err_, ip, family) {
1616
// tests to run offline (lookup will fail in that case and the err be set);
1717

1818
const as = hooks.activitiesOfTypes('GETADDRINFOREQWRAP');
19-
assert.strictEqual(as.length, 1, 'one activity');
19+
assert.strictEqual(as.length, 1);
2020

2121
const a = as[0];
22-
assert.strictEqual(a.type, 'GETADDRINFOREQWRAP', 'getaddrinforeq wrap');
23-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
24-
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
22+
assert.strictEqual(a.type, 'GETADDRINFOREQWRAP');
23+
assert.strictEqual(typeof a.uid, 'number');
24+
assert.strictEqual(a.triggerId, 1);
2525
checkInvocations(a, { init: 1, before: 1 }, 'while in onlookup callback');
2626
tick(2);
2727
}

test/async-hooks/test-getnameinforeqwrap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function onlookupService(err_, ip, family) {
1616
// tests to run offline (lookup will fail in that case and the err be set)
1717

1818
const as = hooks.activitiesOfTypes('GETNAMEINFOREQWRAP');
19-
assert.strictEqual(as.length, 1, 'one activity');
19+
assert.strictEqual(as.length, 1);
2020

2121
const a = as[0];
22-
assert.strictEqual(a.type, 'GETNAMEINFOREQWRAP', 'getnameinforeq wrap');
23-
assert.strictEqual(typeof a.uid, 'number', 'uid is a number');
24-
assert.strictEqual(a.triggerId, 1, 'parent uid 1');
22+
assert.strictEqual(a.type, 'GETNAMEINFOREQWRAP');
23+
assert.strictEqual(typeof a.uid, 'number');
24+
assert.strictEqual(a.triggerId, 1);
2525
checkInvocations(a, { init: 1, before: 1 },
2626
'while in onlookupService callback');
2727
tick(2);

test/async-hooks/test-httpparser.request.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ const parser = new HTTPParser(REQUEST);
2626
const as = hooks.activitiesOfTypes('HTTPPARSER');
2727
const httpparser = as[0];
2828

29-
assert.strictEqual(
30-
as.length, 1,
31-
'1 httpparser created synchronously when creating new httpparser');
32-
assert.strictEqual(typeof httpparser.uid, 'number', 'uid is a number');
33-
assert.strictEqual(typeof httpparser.triggerId,
34-
'number', 'triggerId is a number');
29+
assert.strictEqual(as.length, 1);
30+
assert.strictEqual(typeof httpparser.uid, 'number');
31+
assert.strictEqual(typeof httpparser.triggerId, 'number');
3532
checkInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser');
3633

3734
parser[kOnHeadersComplete] = common.mustCall(onheadersComplete);

test/async-hooks/test-httpparser.response.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ const parser = new HTTPParser(RESPONSE);
3030
const as = hooks.activitiesOfTypes('HTTPPARSER');
3131
const httpparser = as[0];
3232

33-
assert.strictEqual(
34-
as.length, 1,
35-
'1 httpparser created synchronously when creating new httpparser');
36-
assert.strictEqual(typeof httpparser.uid, 'number', 'uid is a number');
37-
assert.strictEqual(typeof httpparser.triggerId,
38-
'number', 'triggerId is a number');
33+
assert.strictEqual(as.length, 1);
34+
assert.strictEqual(typeof httpparser.uid, 'number');
35+
assert.strictEqual(typeof httpparser.triggerId, 'number');
3936
checkInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser');
4037

4138
parser[kOnHeadersComplete] = common.mustCall(onheadersComplete);

0 commit comments

Comments
 (0)