Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 6e8c2249 authored by Christophe Guillon's avatar Christophe Guillon
Browse files

Add exemple of sequential mode with util.promisify

parent f14232ee
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,15 @@ Test with: ...@@ -29,6 +29,15 @@ Test with:
TEST: TEST2: TIMEOUT (2): timeout: after 1000 msecs TEST: TEST2: TIMEOUT (2): timeout: after 1000 msecs
TEST: TEST1: TIMEOUT (2): timeout: after 1000 msecs TEST: TEST1: TIMEOUT (2): timeout: after 1000 msecs
Or in sequential mode with the use of promise:
env AWAIT=1 ./test-tasks.js
TEST: TEST1: TIMEOUT (2): timeout: after 1000 msecs
TEST: TEST2: TIMEOUT (2): timeout: after 1000 msecs
TEST: TEST3: SUCCESS: result: 746450240
TEST: TEST4: SUCCESS: result: 2336000
TEST: TEST5: ERROR (1): ReferenceError: test_bogus_function_result is not defined
TEST: TEST6: ERROR (4): unknown error
TEST: TEST7: SUCCESS: result: undefined
Licence Licence
------- -------
......
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
* Execute with: * Execute with:
* $ node test-workers.js * $ node test-workers.js
* *
* Tested on nodejs v12.22.12 and v20.4.0, does not work on v10.x * Or in sequential mode with the use of promise:
* $ env AWAIT=1 ./test-tasks.js
*
*
* Tested on nodejs >= v12.x, does not work on v10.x
*/ */
"use strict"; "use strict";
...@@ -24,7 +28,11 @@ console.setDebug(false) // for no output with console.debug (the default) ...@@ -24,7 +28,11 @@ console.setDebug(false) // for no output with console.debug (the default)
// Use the same file and dispatch to worker if in a Task // Use the same file and dispatch to worker if in a Task
// Refer to runTask() below where __filename is passed // Refer to runTask() below where __filename is passed
if (!isTask) { if (!isTask) {
main(); if (process.env.AWAIT) {
async_main();
} else {
main();
}
} else { } else {
task_dispatch(); task_dispatch();
} }
...@@ -56,6 +64,33 @@ function task_dispatch() { ...@@ -56,6 +64,33 @@ function task_dispatch() {
// TEST: TEST1: TIMEOUT (2): timeout: after 1000 msecs // TEST: TEST1: TIMEOUT (2): timeout: after 1000 msecs
// TEST: TEST2: TIMEOUT (2): timeout: after 1000 msecs // TEST: TEST2: TIMEOUT (2): timeout: after 1000 msecs
// //
// Tests args list
function getTestsList() {
const tests = [
["TEST1", "test_long_function", [10_000_000_000]],
["TEST2", "test_long_function", [10_000_000_000]],
["TEST3", "test_long_function", [1_000_000]],
["TEST4", "test_long_function", [2_000_000]],
["TEST5", "test_bogus_function", []],
["TEST6", "test_exit_function", [4]],
["TEST7", "test_exit_function", [0]],
];
return tests
}
// Additional default arguments for all tests
function getDefaults() {
// Run with default timeout to 1 sec and callback above as a nodeback(err, res)
// Note that we give __filename (i.e. the current file) as the filename to execute
// though the code for the tasks may be in another file
const defaults = [__filename, { timeout: 1000 }];
return defaults;
}
function main() { function main() {
assert.ok(isMain); assert.ok(isMain);
const nodeback = (err, res) => { const nodeback = (err, res) => {
...@@ -70,23 +105,36 @@ function main() { ...@@ -70,23 +105,36 @@ function main() {
} }
}; };
console.debug("START: main") console.debug("START: main")
const defaults = getDefaults();
// Run with default timeout to 1 sec and callback above as a nodeback(err, res) for (const testArgs of getTestsList()) {
// Note that we give __filename (i.e. the current file) as the filename to execute runTask(...testArgs, ...defaults, nodeback)
// though the code for the tasks may be in another file }
const defaults = [__filename, { timeout: 1000 }, nodeback];
runTask("TEST1", "test_long_function", [10_000_000_000], ...defaults)
runTask("TEST2", "test_long_function", [10_000_000_000], ...defaults)
runTask("TEST3", "test_long_function", [1_000_000], ...defaults)
runTask("TEST4", "test_long_function", [2_000_000], ...defaults)
runTask("TEST5", "test_bogus_function", [], ...defaults)
runTask("TEST6", "test_exit_function", [4], ...defaults)
runTask("TEST7", "test_exit_function", [0], ...defaults)
console.debug("END: main") console.debug("END: main")
} }
// Main program code serialized with promise and await
// Simply use util.promisify(runTask) to make it a promise
async function async_main() {
assert.ok(isMain);
const resolved = (res) => {
console.log(`TEST: ${res.taskname}: SUCCESS: result: ${JSON.stringify(res.result)}`);
};
const rejected = (err) => {
if (err.timeout) {
console.error(`TEST: ${err.taskname}: TIMEOUT (${err.exit_code}): ${err.error}`);
} else {
console.error(`TEST: ${err.taskname}: ERROR (${err.exit_code}): ${err.error}`);
}
};
console.debug("START: async_main")
const defaults = getDefaults();
for (const testArgs of getTestsList()) {
await util.promisify(runTask)(...testArgs, ...defaults).then(resolved, rejected);
}
console.debug("END: async_main")
}
// Just exists early // Just exists early
function test_exit_function(code) { function test_exit_function(code) {
process.exit(code); // just exit early process.exit(code); // just exit early
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment