diff --git a/README.md b/README.md index 9533d64afd4f11a419be0a9f171289fca6ced8c5..960c8a0e4ba1948a65f2f1a7460e006cf9c0b877 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,15 @@ Test with: TEST: TEST2: 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 ------- diff --git a/test-tasks.js b/test-tasks.js index 43cd944ce0e87dff7e3fcc6adc8d94a853eac4cf..bb3bff509b4b51178d04dd129fb1beff7ad226e8 100644 --- a/test-tasks.js +++ b/test-tasks.js @@ -4,7 +4,11 @@ * Execute with: * $ 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"; @@ -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 // Refer to runTask() below where __filename is passed if (!isTask) { - main(); + if (process.env.AWAIT) { + async_main(); + } else { + main(); + } } else { task_dispatch(); } @@ -56,6 +64,33 @@ function task_dispatch() { // TEST: TEST1: 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() { assert.ok(isMain); const nodeback = (err, res) => { @@ -70,23 +105,36 @@ function main() { } }; console.debug("START: main") - - // 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 }, 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) - + const defaults = getDefaults(); + for (const testArgs of getTestsList()) { + runTask(...testArgs, ...defaults, nodeback) + } 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 function test_exit_function(code) { process.exit(code); // just exit early