Skip to content

Commit 5d36882

Browse files
author
Gabriel Schulhof
committed
test: string tests together
* Use promises and async/await to ensure that one test finishes before another one starts. This prevents errors thrown in one test from appearing to originate from another test. * Perform garbage collection consistently, via `testUtil.runGCTests()`, to ensure that it is performed correctly and that its results are awaited.
1 parent 5af645f commit 5d36882

24 files changed

+380
-315
lines changed

test/addon_data.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,37 @@ const { spawn } = require('child_process');
55
const readline = require('readline');
66
const path = require('path');
77

8-
test(path.resolve(__dirname, `./build/${buildType}/binding.node`));
9-
test(path.resolve(__dirname, `./build/${buildType}/binding_noexcept.node`));
8+
module.exports =
9+
test(path.resolve(__dirname, `./build/${buildType}/binding.node`))
10+
.then(() =>
11+
test(path.resolve(__dirname,
12+
`./build/${buildType}/binding_noexcept.node`)));
1013

1114
// Make sure the instance data finalizer is called at process exit. If the hint
1215
// is non-zero, it will be printed out by the child process.
1316
function testFinalizer(bindingName, hint, expected) {
14-
bindingName = bindingName.split('\\').join('\\\\');
15-
const child = spawn(process.execPath, [
16-
'-e',
17-
`require('${bindingName}').addon_data(${hint}).verbose = true;`
18-
]);
19-
const actual = [];
20-
readline
21-
.createInterface({ input: child.stderr })
22-
.on('line', (line) => {
23-
if (expected.indexOf(line) >= 0) {
24-
actual.push(line);
25-
}
26-
})
27-
.on('close', () => assert.deepStrictEqual(expected, actual));
17+
return new Promise((resolve) => {
18+
bindingName = bindingName.split('\\').join('\\\\');
19+
const child = spawn(process.execPath, [
20+
'-e',
21+
`require('${bindingName}').addon_data(${hint}).verbose = true;`
22+
]);
23+
const actual = [];
24+
readline
25+
.createInterface({ input: child.stderr })
26+
.on('line', (line) => {
27+
if (expected.indexOf(line) >= 0) {
28+
actual.push(line);
29+
}
30+
})
31+
.on('close', () => {
32+
assert.deepStrictEqual(expected, actual);
33+
resolve();
34+
});
35+
});
2836
}
2937

30-
function test(bindingName) {
38+
async function test(bindingName) {
3139
const binding = require(bindingName).addon_data(0);
3240

3341
// Make sure it is possible to get/set instance data.
@@ -37,6 +45,7 @@ function test(bindingName) {
3745
binding.verbose = false;
3846
assert.strictEqual(binding.verbose.verbose, false);
3947

40-
testFinalizer(bindingName, 0, ['addon_data: Addon::~Addon']);
41-
testFinalizer(bindingName, 42, ['addon_data: Addon::~Addon', 'hint: 42']);
48+
await testFinalizer(bindingName, 0, ['addon_data: Addon::~Addon']);
49+
await testFinalizer(bindingName, 42,
50+
['addon_data: Addon::~Addon', 'hint: 42']);
4251
}

test/arraybuffer.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const buildType = process.config.target_defaults.default_configuration;
33
const assert = require('assert');
44
const testUtil = require('./testUtil');
55

6-
test(require(`./build/${buildType}/binding.node`));
7-
test(require(`./build/${buildType}/binding_noexcept.node`));
6+
module.exports = test(require(`./build/${buildType}/binding.node`))
7+
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
88

9-
function test(binding) {
10-
testUtil.runGCTests([
9+
async function test(binding) {
10+
await testUtil.runGCTests([
1111
'Internal ArrayBuffer',
1212
() => {
1313
const test = binding.arraybuffer.createBuffer();
@@ -25,10 +25,8 @@ function test(binding) {
2525
assert.ok(test instanceof ArrayBuffer);
2626
assert.strictEqual(0, binding.arraybuffer.getFinalizeCount());
2727
},
28-
() => {
29-
global.gc();
30-
assert.strictEqual(0, binding.arraybuffer.getFinalizeCount());
31-
},
28+
29+
() => assert.strictEqual(0, binding.arraybuffer.getFinalizeCount()),
3230

3331
'External ArrayBuffer with finalizer',
3432
() => {
@@ -37,12 +35,8 @@ function test(binding) {
3735
assert.ok(test instanceof ArrayBuffer);
3836
assert.strictEqual(0, binding.arraybuffer.getFinalizeCount());
3937
},
40-
() => {
41-
global.gc();
42-
},
43-
() => {
44-
assert.strictEqual(1, binding.arraybuffer.getFinalizeCount());
45-
},
38+
39+
() => assert.strictEqual(1, binding.arraybuffer.getFinalizeCount()),
4640

4741
'External ArrayBuffer with finalizer hint',
4842
() => {
@@ -51,12 +45,8 @@ function test(binding) {
5145
assert.ok(test instanceof ArrayBuffer);
5246
assert.strictEqual(0, binding.arraybuffer.getFinalizeCount());
5347
},
54-
() => {
55-
global.gc();
56-
},
57-
() => {
58-
assert.strictEqual(1, binding.arraybuffer.getFinalizeCount());
59-
},
48+
49+
() => assert.strictEqual(1, binding.arraybuffer.getFinalizeCount()),
6050

6151
'ArrayBuffer with constructor',
6252
() => {

test/asyncprogressqueueworker.js

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,67 @@ const common = require('./common')
44
const assert = require('assert');
55
const os = require('os');
66

7-
test(require(`./build/${buildType}/binding.node`));
8-
test(require(`./build/${buildType}/binding_noexcept.node`));
7+
module.exports = test(require(`./build/${buildType}/binding.node`))
8+
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
99

10-
function test({ asyncprogressqueueworker }) {
11-
success(asyncprogressqueueworker);
12-
fail(asyncprogressqueueworker);
13-
cancel(asyncprogressqueueworker);
14-
return;
10+
async function test({ asyncprogressqueueworker }) {
11+
await success(asyncprogressqueueworker);
12+
await fail(asyncprogressqueueworker);
13+
await cancel(asyncprogressqueueworker);
1514
}
1615

1716
function success(binding) {
18-
const expected = [0, 1, 2, 3];
19-
const actual = [];
20-
const worker = binding.createWork(expected.length,
21-
common.mustCall((err) => {
22-
if (err) {
23-
assert.fail(err);
24-
}
25-
// All queued items shall be invoked before complete callback.
26-
assert.deepEqual(actual, expected);
27-
}),
28-
common.mustCall((_progress) => {
29-
actual.push(_progress);
30-
}, expected.length)
31-
);
32-
binding.queueWork(worker);
17+
return new Promise((resolve, reject) => {
18+
const expected = [0, 1, 2, 3];
19+
const actual = [];
20+
const worker = binding.createWork(expected.length,
21+
common.mustCall((err) => {
22+
if (err) {
23+
reject(err);
24+
} else {
25+
// All queued items shall be invoked before complete callback.
26+
assert.deepEqual(actual, expected);
27+
resolve();
28+
}
29+
}),
30+
common.mustCall((_progress) => {
31+
actual.push(_progress);
32+
}, expected.length)
33+
);
34+
binding.queueWork(worker);
35+
});
3336
}
3437

3538
function fail(binding) {
36-
const worker = binding.createWork(-1,
37-
common.mustCall((err) => {
38-
assert.throws(() => { throw err }, /test error/)
39-
}),
40-
() => {
41-
assert.fail('unexpected progress report');
42-
}
43-
);
44-
binding.queueWork(worker);
39+
return new Promise((resolve, reject) => {
40+
const worker = binding.createWork(-1,
41+
common.mustCall((err) => {
42+
assert.throws(() => { throw err }, /test error/);
43+
resolve();
44+
}),
45+
common.mustNotCall()
46+
);
47+
binding.queueWork(worker);
48+
});
4549
}
4650

4751
function cancel(binding) {
48-
// make sure the work we are going to cancel will not be
49-
// able to start by using all the threads in the pool.
50-
for (let i = 0; i < os.cpus().length; ++i) {
51-
const worker = binding.createWork(-1, () => {}, () => {});
52-
binding.queueWork(worker);
53-
}
54-
const worker = binding.createWork(-1,
55-
() => {
56-
assert.fail('unexpected callback');
57-
},
58-
() => {
59-
assert.fail('unexpected progress report');
52+
return new Promise((resolve, reject) => {
53+
// make sure the work we are going to cancel will not be
54+
// able to start by using all the threads in the pool.
55+
for (let i = 0; i < os.cpus().length; ++i) {
56+
const worker = binding.createWork(-1, () => {}, () => {});
57+
binding.queueWork(worker);
6058
}
61-
);
62-
binding.cancelWork(worker);
59+
const worker = binding.createWork(-1,
60+
() => {
61+
assert.fail('unexpected callback');
62+
},
63+
() => {
64+
assert.fail('unexpected progress report');
65+
}
66+
);
67+
binding.cancelWork(worker);
68+
resolve();
69+
});
6370
}

test/asyncprogressworker.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,43 @@ const buildType = process.config.target_defaults.default_configuration;
33
const common = require('./common')
44
const assert = require('assert');
55

6-
test(require(`./build/${buildType}/binding.node`));
7-
test(require(`./build/${buildType}/binding_noexcept.node`));
6+
module.exports = test(require(`./build/${buildType}/binding.node`))
7+
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
88

9-
function test({ asyncprogressworker }) {
10-
success(asyncprogressworker);
11-
fail(asyncprogressworker);
12-
return;
9+
async function test({ asyncprogressworker }) {
10+
await success(asyncprogressworker);
11+
await fail(asyncprogressworker);
1312
}
1413

1514
function success(binding) {
16-
const expected = [0, 1, 2, 3];
17-
const actual = [];
18-
binding.doWork(expected.length,
19-
common.mustCall((err) => {
20-
if (err) {
21-
assert.fail(err);
22-
}
23-
}),
24-
common.mustCall((_progress) => {
25-
actual.push(_progress);
26-
if (actual.length === expected.length) {
27-
assert.deepEqual(actual, expected);
28-
}
29-
}, expected.length)
30-
);
15+
return new Promise((resolve, reject) => {
16+
const expected = [0, 1, 2, 3];
17+
const actual = [];
18+
binding.doWork(expected.length,
19+
common.mustCall((err) => {
20+
if (err) {
21+
reject(err);
22+
}
23+
}),
24+
common.mustCall((_progress) => {
25+
actual.push(_progress);
26+
if (actual.length === expected.length) {
27+
assert.deepEqual(actual, expected);
28+
resolve();
29+
}
30+
}, expected.length)
31+
);
32+
});
3133
}
3234

3335
function fail(binding) {
34-
binding.doWork(-1,
35-
common.mustCall((err) => {
36-
assert.throws(() => { throw err }, /test error/)
37-
}),
38-
() => {
39-
assert.fail('unexpected progress report');
40-
}
41-
);
36+
return new Promise((resolve, reject) => {
37+
binding.doWork(-1,
38+
common.mustCall((err) => {
39+
assert.throws(() => { throw err }, /test error/)
40+
resolve();
41+
}),
42+
common.mustNotCall()
43+
);
44+
});
4245
}

test/asyncworker-nocallback.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
const buildType = process.config.target_defaults.default_configuration;
33
const common = require('./common');
44

5-
test(require(`./build/${buildType}/binding.node`));
6-
test(require(`./build/${buildType}/binding_noexcept.node`));
5+
module.exports = test(require(`./build/${buildType}/binding.node`))
6+
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
77

8-
function test(binding) {
9-
const resolving = binding.asyncworker.doWorkNoCallback(true, {});
10-
resolving.then(common.mustCall()).catch(common.mustNotCall());
8+
async function test(binding) {
9+
await binding.asyncworker.doWorkNoCallback(true, {})
10+
.then(common.mustCall()).catch(common.mustNotCall());
1111

12-
const rejecting = binding.asyncworker.doWorkNoCallback(false, {});
13-
rejecting.then(common.mustNotCall()).catch(common.mustCall());
14-
return;
15-
}
12+
await binding.asyncworker.doWorkNoCallback(false, {})
13+
.then(common.mustNotCall()).catch(common.mustCall());
14+
}

test/asyncworker-persistent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function test(binding, succeed) {
2121
}));
2222
}
2323

24-
test(binding.persistentasyncworker, false)
24+
module.exports = test(binding.persistentasyncworker, false)
2525
.then(() => test(binding.persistentasyncworker, true))
2626
.then(() => test(noexceptBinding.persistentasyncworker, false))
2727
.then(() => test(noexceptBinding.persistentasyncworker, true));

0 commit comments

Comments
 (0)