forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_terminating_environment.js
99 lines (80 loc) · 2.64 KB
/
error_terminating_environment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const assert = require('assert');
const { whichBuildType } = require('./common');
// These tests ensure that Error types can be used in a terminating
// environment without triggering any fatal errors.
if (process.argv[2] === 'runInChildProcess') {
const bindingPath = process.argv[3];
const indexForTestCase = Number(process.argv[4]);
const binding = require(bindingPath);
// Use C++ promises to ensure the worker thread is terminated right
// before running the testable code in the binding.
binding.error.resetPromises();
const { Worker } = require('worker_threads');
const worker = new Worker(
__filename,
{
argv: [
'runInWorkerThread',
bindingPath,
indexForTestCase
]
}
);
binding.error.waitForWorkerThread();
worker.terminate();
binding.error.releaseWorkerThread();
} else {
if (process.argv[2] === 'runInWorkerThread') {
const bindingPath = process.argv[3];
const indexForTestCase = Number(process.argv[4]);
const binding = require(bindingPath);
switch (indexForTestCase) {
case 0:
binding.error.throwJSError('test', true);
break;
case 1:
binding.error.throwTypeError('test', true);
break;
case 2:
binding.error.throwRangeError('test', true);
break;
case 3:
binding.error.throwDefaultError(false, true);
break;
case 4:
binding.error.throwDefaultError(true, true);
break;
default: assert.fail('Invalid index');
}
assert.fail('This should not be reachable');
}
wrapTest();
async function wrapTest () {
const buildType = await whichBuildType();
test(`./build/${buildType}/binding.node`, true);
test(`./build/${buildType}/binding_noexcept.node`, true);
test(`./build/${buildType}/binding_swallowexcept.node`, false);
test(`./build/${buildType}/binding_swallowexcept_noexcept.node`, false);
test(`./build/${buildType}/binding_custom_namespace.node`, true);
}
function test (bindingPath, processShouldAbort) {
const numberOfTestCases = 5;
for (let i = 0; i < numberOfTestCases; ++i) {
const childProcess = require('./napi_child').spawnSync(
process.execPath,
[
__filename,
'runInChildProcess',
bindingPath,
i
]
);
if (processShouldAbort) {
assert(childProcess.status !== 0, `Test case ${bindingPath} ${i} failed: Process exited with status code 0.`);
} else {
assert(childProcess.status === 0, `Test case ${bindingPath} ${i} failed: Process status ${childProcess.status} is non-zero`);
}
}
}
}