From 0fac2c18598c9e7b2c3c3aa9af77720b13b7ea93 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sat, 23 Nov 2024 15:06:39 +0300 Subject: [PATCH 01/17] fix(#368): added utility function for elapsed time tracking --- src/commands/assemble.js | 11 ++++-- src/commands/java/compile.js | 4 ++ src/elapsed.js | 69 ++++++++++++++++++++++++++++++++ test/test_elapsed.js | 76 ++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 src/elapsed.js create mode 100644 test/test_elapsed.js diff --git a/src/commands/assemble.js b/src/commands/assemble.js index 216f34d..5d752d0 100644 --- a/src/commands/assemble.js +++ b/src/commands/assemble.js @@ -25,6 +25,7 @@ const rel = require('relative'); const path = require('path'); const {mvnw, flags} = require('../mvnw'); +const {elapsed} = require('../elapsed'); /** * Command to assemble .XMIR files. @@ -33,8 +34,10 @@ const {mvnw, flags} = require('../mvnw'); */ module.exports = function(opts) { const target = path.resolve(opts.target); - return mvnw(['eo:assemble'].concat(flags(opts)), opts.target, opts.batch).then((r) => { - console.info('EO program assembled in %s', rel(target)); - return r; + return elapsed((tracked) => { + return mvnw(['eo:assemble'].concat(flags(opts)), opts.target, opts.batch).then((r) => { + tracked.print('EO program assembled in %s', rel(target)); + return r; + }); }); -}; +}; \ No newline at end of file diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index aba2816..92b86fe 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -33,6 +33,10 @@ const path = require('path'); */ module.exports = function(opts) { const target = path.resolve(opts.target); + /** + * @todo #368 + * It is necessary to use 'elapsed' in all logging cases that require output of elapsed time + */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { console.info('Java .class files compiled into %s', rel(target)); return r; diff --git a/src/elapsed.js b/src/elapsed.js new file mode 100644 index 0000000..5ead22d --- /dev/null +++ b/src/elapsed.js @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * @todo #368 + * Consider if this method belong is in the right place. + * It might belong in a utility module. + * For now, it remains here. + * + * Also, review whether the test file for this method is located appropriately. + * It’s unclear if its current location is the best fit. + */ + +/** + * A utility function to measure the elapsed time of a task and provide + * detailed timing information. + * + * This function wraps a given task (callback function) and provides it with + * a `tracked` object that includes a `print` method. The `print` method can + * be used within the task to log messages along with the elapsed time + * since the task started execution. The elapsed time is formatted in milliseconds, + * seconds, or minutes, based on the duration. + * + * @param {Function} task - A callback function to be measured. The function + * is invoked with a `tracked` object as an argument. + * @return {*} Result of the wrapped callback function. The result of the + * `task` callback will be returned unchanged. + */ +module.exports.elapsed = function elapsed(task) { + const startTime = Date.now(); + const tracked = { + print: (message) => { + const duration = Date.now() - startTime; + let extended; + if (duration < 1000) { + extended = `${duration}ms`; + } else if (duration < 60 * 1000) { + extended = `${Math.ceil(duration / 1000)}s`; + } else { + extended = `${Math.ceil(duration / 3600000)}min`; + } + let msg = `${message} in ${extended}`; + console.info(msg); + return msg; + } + } + return task(tracked); +} diff --git a/test/test_elapsed.js b/test/test_elapsed.js new file mode 100644 index 0000000..a6e8ba5 --- /dev/null +++ b/test/test_elapsed.js @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +const {elapsed} = require('../src/elapsed') +const assert = require("assert"); + +describe('elapsed', function(){ + const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); + it('measures time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(300); + return tracked.print("task"); + }).then( + (actual)=> assert( + /task in 30\d+ms/.test(actual), + `Expected "${actual}" to match /task in 30\\d+ms/` + ) + ) + }); + + it('measures short time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(10); + return tracked.print("short task"); + }).then( + (actual) => assert( + /short task in 1\d+ms/.test(actual), + `Expected "${actual}" to match /short task in 1\\d+ms/` + ) + ); + }); + + it('measures long time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(1200); + return tracked.print("long task"); + }).then( + (actual) => assert( + /long task in 2s/.test(actual), + `Expected "${actual}" to match /long task in 2s/` + ) + ); + }); + + it('handles errors in task correctly', async () => { + try { + await elapsed(async (tracked) => { + throw new Error("task error"); + }); + assert.fail("Expected an error to be thrown"); + } catch (error) { + assert.equal(error.message, "task error"); + } + }); +}) From 920198b584dc5088316f8b451d1af7570ac5f5eb Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sat, 23 Nov 2024 19:23:37 +0300 Subject: [PATCH 02/17] fix(#368): simplify logic and format code for elapsed --- src/commands/assemble.js | 2 +- src/commands/java/compile.js | 4 ++-- src/elapsed.js | 14 ++++++-------- test/test_elapsed.js | 15 ++++++--------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/commands/assemble.js b/src/commands/assemble.js index 5d752d0..c64c91d 100644 --- a/src/commands/assemble.js +++ b/src/commands/assemble.js @@ -40,4 +40,4 @@ module.exports = function(opts) { return r; }); }); -}; \ No newline at end of file +}; diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index 92b86fe..cbc2fcd 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -34,8 +34,8 @@ const path = require('path'); module.exports = function(opts) { const target = path.resolve(opts.target); /** - * @todo #368 - * It is necessary to use 'elapsed' in all logging cases that require output of elapsed time + * @todo #368 Wrap logs in 'elapsed' + * - It is necessary to use 'elapsed' in all logging cases that require output of elapsed time */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { console.info('Java .class files compiled into %s', rel(target)); diff --git a/src/elapsed.js b/src/elapsed.js index 5ead22d..4991911 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -23,13 +23,12 @@ */ /** - * @todo #368 - * Consider if this method belong is in the right place. + * @todo #368:30min Decide the proper location for the `elapsed` utility function. + * - Consider if this method belong is in the right place. * It might belong in a utility module. * For now, it remains here. * - * Also, review whether the test file for this method is located appropriately. - * It’s unclear if its current location is the best fit. + * - Review if the test file for this method is appropriately located, as its current location might not be ideal. */ /** @@ -49,7 +48,7 @@ */ module.exports.elapsed = function elapsed(task) { const startTime = Date.now(); - const tracked = { + return task({ print: (message) => { const duration = Date.now() - startTime; let extended; @@ -60,10 +59,9 @@ module.exports.elapsed = function elapsed(task) { } else { extended = `${Math.ceil(duration / 3600000)}min`; } - let msg = `${message} in ${extended}`; + const msg = `${message} in ${extended}`; console.info(msg); return msg; } - } - return task(tracked); + }); } diff --git a/test/test_elapsed.js b/test/test_elapsed.js index a6e8ba5..ac33272 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -26,43 +26,40 @@ const {elapsed} = require('../src/elapsed') const assert = require("assert"); describe('elapsed', function(){ - const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); + const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); it('measures time correctly', async () => { return elapsed(async (tracked) => { await snooze(300); return tracked.print("task"); - }).then( + }) .then( (actual)=> assert( /task in 30\d+ms/.test(actual), `Expected "${actual}" to match /task in 30\\d+ms/` ) ) }); - it('measures short time correctly', async () => { return elapsed(async (tracked) => { await snooze(10); return tracked.print("short task"); - }).then( + }) .then( (actual) => assert( /short task in 1\d+ms/.test(actual), `Expected "${actual}" to match /short task in 1\\d+ms/` ) ); }); - it('measures long time correctly', async () => { return elapsed(async (tracked) => { await snooze(1200); return tracked.print("long task"); - }).then( + }) .then( (actual) => assert( /long task in 2s/.test(actual), `Expected "${actual}" to match /long task in 2s/` ) ); }); - it('handles errors in task correctly', async () => { try { await elapsed(async (tracked) => { @@ -70,7 +67,7 @@ describe('elapsed', function(){ }); assert.fail("Expected an error to be thrown"); } catch (error) { - assert.equal(error.message, "task error"); + assert.throws(() => { throw error }, /task error/); } }); -}) +}) \ No newline at end of file From 32e0759f316bfd432b4c9d4e5a89a890238260a0 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sat, 23 Nov 2024 19:30:39 +0300 Subject: [PATCH 03/17] fix(#368): fix up todo needful time --- src/commands/java/compile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index cbc2fcd..2a3016c 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -34,7 +34,7 @@ const path = require('path'); module.exports = function(opts) { const target = path.resolve(opts.target); /** - * @todo #368 Wrap logs in 'elapsed' + * @todo #368:30min Wrap logs in 'elapsed' * - It is necessary to use 'elapsed' in all logging cases that require output of elapsed time */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { From 80ee2ec48a8f1f6f2bb30ec4a5c9d897e2deb3b5 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sun, 24 Nov 2024 17:58:43 +0300 Subject: [PATCH 04/17] fix(#368): formatted, simplified --- src/elapsed.js | 45 ++++++++++---------- test/test_elapsed.js | 97 ++++++++++++++++++++++---------------------- 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/elapsed.js b/src/elapsed.js index 4991911..af1585f 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -25,14 +25,13 @@ /** * @todo #368:30min Decide the proper location for the `elapsed` utility function. * - Consider if this method belong is in the right place. - * It might belong in a utility module. - * For now, it remains here. - * - * - Review if the test file for this method is appropriately located, as its current location might not be ideal. + * It might belong in a utility module. For now, it remains here. + * Review if the test file for this method is appropriately located, + * as its current location might not be ideal. */ /** - * A utility function to measure the elapsed time of a task and provide + * A utility function to measure the elapsed time of a task and provideÏ * detailed timing information. * * This function wraps a given task (callback function) and provides it with @@ -47,21 +46,21 @@ * `task` callback will be returned unchanged. */ module.exports.elapsed = function elapsed(task) { - const startTime = Date.now(); - return task({ - print: (message) => { - const duration = Date.now() - startTime; - let extended; - if (duration < 1000) { - extended = `${duration}ms`; - } else if (duration < 60 * 1000) { - extended = `${Math.ceil(duration / 1000)}s`; - } else { - extended = `${Math.ceil(duration / 3600000)}min`; - } - const msg = `${message} in ${extended}`; - console.info(msg); - return msg; - } - }); -} + const startTime = Date.now(); + return task({ + print: (message) => { + const duration = Date.now() - startTime; + let extended; + if (duration < 1000) { + extended = `${duration}ms`; + } else if (duration < 60 * 1000) { + extended = `${Math.ceil(duration / 1000)}s`; + } else { + extended = `${Math.ceil(duration / 3600000)}min`; + } + const msg = `${message} in ${extended}`; + console.info(msg); + return msg; + } + }); +}; diff --git a/test/test_elapsed.js b/test/test_elapsed.js index ac33272..25146fc 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -22,52 +22,53 @@ * SOFTWARE. */ -const {elapsed} = require('../src/elapsed') -const assert = require("assert"); +const {elapsed} = require('../src/elapsed'); +const assert = require('assert'); -describe('elapsed', function(){ - const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); - it('measures time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(300); - return tracked.print("task"); - }) .then( - (actual)=> assert( - /task in 30\d+ms/.test(actual), - `Expected "${actual}" to match /task in 30\\d+ms/` - ) - ) - }); - it('measures short time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(10); - return tracked.print("short task"); - }) .then( - (actual) => assert( - /short task in 1\d+ms/.test(actual), - `Expected "${actual}" to match /short task in 1\\d+ms/` - ) - ); - }); - it('measures long time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(1200); - return tracked.print("long task"); - }) .then( - (actual) => assert( - /long task in 2s/.test(actual), - `Expected "${actual}" to match /long task in 2s/` - ) - ); - }); - it('handles errors in task correctly', async () => { - try { - await elapsed(async (tracked) => { - throw new Error("task error"); - }); - assert.fail("Expected an error to be thrown"); - } catch (error) { - assert.throws(() => { throw error }, /task error/); - } - }); -}) \ No newline at end of file +describe('elapsed', function() { + const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + it('measures time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(300); + return tracked.print('task'); + }) .then( + (actual)=> assert( + /task in 30\d+ms/.test(actual), + `Expected "${actual}" to match /task in 30\\d+ms/` + ) + ); + }); + it('measures short time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(10); + return tracked.print('short task'); + }) .then( + (actual) => assert( + /short task in 1\d+ms/.test(actual), + `Expected "${actual}" to match /short task in 1\\d+ms/` + ) + ); + }); + it('measures long time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(1200); + return tracked.print('long task'); + }) .then( + (actual) => assert( + /long task in 2s/.test(actual), + `Expected "${actual}" to match /long task in 2s/` + ) + ); + }); + it('handles errors in task correctly', async () => { + await assert.rejects( + elapsed(async () => { + throw new Error("task error"); + }), + (error) => { + assert.throws(() => { throw error }, /task error/); + return true; + } + ); + }); +}); From 4efeacbce4abfba6476a399cc114c773b199d8a1 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Mon, 25 Nov 2024 20:14:54 +0300 Subject: [PATCH 05/17] fix(#368): formatted --- test/test_elapsed.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_elapsed.js b/test/test_elapsed.js index 25146fc..c6123f2 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -63,10 +63,12 @@ describe('elapsed', function() { it('handles errors in task correctly', async () => { await assert.rejects( elapsed(async () => { - throw new Error("task error"); + throw new Error('task error'); }), (error) => { - assert.throws(() => { throw error }, /task error/); + assert.throws(() => { + throw error; + }, /task error/); return true; } ); From 0911954ae51d3dd977588974c1b33c3ef74a8be3 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sat, 23 Nov 2024 15:06:39 +0300 Subject: [PATCH 06/17] fix(#368): added utility function for elapsed time tracking --- src/commands/assemble.js | 11 ++++-- src/commands/java/compile.js | 4 ++ src/elapsed.js | 69 ++++++++++++++++++++++++++++++++ test/test_elapsed.js | 76 ++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 src/elapsed.js create mode 100644 test/test_elapsed.js diff --git a/src/commands/assemble.js b/src/commands/assemble.js index 216f34d..5d752d0 100644 --- a/src/commands/assemble.js +++ b/src/commands/assemble.js @@ -25,6 +25,7 @@ const rel = require('relative'); const path = require('path'); const {mvnw, flags} = require('../mvnw'); +const {elapsed} = require('../elapsed'); /** * Command to assemble .XMIR files. @@ -33,8 +34,10 @@ const {mvnw, flags} = require('../mvnw'); */ module.exports = function(opts) { const target = path.resolve(opts.target); - return mvnw(['eo:assemble'].concat(flags(opts)), opts.target, opts.batch).then((r) => { - console.info('EO program assembled in %s', rel(target)); - return r; + return elapsed((tracked) => { + return mvnw(['eo:assemble'].concat(flags(opts)), opts.target, opts.batch).then((r) => { + tracked.print('EO program assembled in %s', rel(target)); + return r; + }); }); -}; +}; \ No newline at end of file diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index aba2816..92b86fe 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -33,6 +33,10 @@ const path = require('path'); */ module.exports = function(opts) { const target = path.resolve(opts.target); + /** + * @todo #368 + * It is necessary to use 'elapsed' in all logging cases that require output of elapsed time + */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { console.info('Java .class files compiled into %s', rel(target)); return r; diff --git a/src/elapsed.js b/src/elapsed.js new file mode 100644 index 0000000..5ead22d --- /dev/null +++ b/src/elapsed.js @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * @todo #368 + * Consider if this method belong is in the right place. + * It might belong in a utility module. + * For now, it remains here. + * + * Also, review whether the test file for this method is located appropriately. + * It’s unclear if its current location is the best fit. + */ + +/** + * A utility function to measure the elapsed time of a task and provide + * detailed timing information. + * + * This function wraps a given task (callback function) and provides it with + * a `tracked` object that includes a `print` method. The `print` method can + * be used within the task to log messages along with the elapsed time + * since the task started execution. The elapsed time is formatted in milliseconds, + * seconds, or minutes, based on the duration. + * + * @param {Function} task - A callback function to be measured. The function + * is invoked with a `tracked` object as an argument. + * @return {*} Result of the wrapped callback function. The result of the + * `task` callback will be returned unchanged. + */ +module.exports.elapsed = function elapsed(task) { + const startTime = Date.now(); + const tracked = { + print: (message) => { + const duration = Date.now() - startTime; + let extended; + if (duration < 1000) { + extended = `${duration}ms`; + } else if (duration < 60 * 1000) { + extended = `${Math.ceil(duration / 1000)}s`; + } else { + extended = `${Math.ceil(duration / 3600000)}min`; + } + let msg = `${message} in ${extended}`; + console.info(msg); + return msg; + } + } + return task(tracked); +} diff --git a/test/test_elapsed.js b/test/test_elapsed.js new file mode 100644 index 0000000..a6e8ba5 --- /dev/null +++ b/test/test_elapsed.js @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +const {elapsed} = require('../src/elapsed') +const assert = require("assert"); + +describe('elapsed', function(){ + const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); + it('measures time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(300); + return tracked.print("task"); + }).then( + (actual)=> assert( + /task in 30\d+ms/.test(actual), + `Expected "${actual}" to match /task in 30\\d+ms/` + ) + ) + }); + + it('measures short time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(10); + return tracked.print("short task"); + }).then( + (actual) => assert( + /short task in 1\d+ms/.test(actual), + `Expected "${actual}" to match /short task in 1\\d+ms/` + ) + ); + }); + + it('measures long time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(1200); + return tracked.print("long task"); + }).then( + (actual) => assert( + /long task in 2s/.test(actual), + `Expected "${actual}" to match /long task in 2s/` + ) + ); + }); + + it('handles errors in task correctly', async () => { + try { + await elapsed(async (tracked) => { + throw new Error("task error"); + }); + assert.fail("Expected an error to be thrown"); + } catch (error) { + assert.equal(error.message, "task error"); + } + }); +}) From 62c60f3ef49888909c170c5c333abc65b69da6eb Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sat, 23 Nov 2024 19:23:37 +0300 Subject: [PATCH 07/17] fix(#368): simplify logic and format code for elapsed --- src/commands/assemble.js | 2 +- src/commands/java/compile.js | 4 ++-- src/elapsed.js | 14 ++++++-------- test/test_elapsed.js | 15 ++++++--------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/commands/assemble.js b/src/commands/assemble.js index 5d752d0..c64c91d 100644 --- a/src/commands/assemble.js +++ b/src/commands/assemble.js @@ -40,4 +40,4 @@ module.exports = function(opts) { return r; }); }); -}; \ No newline at end of file +}; diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index 92b86fe..cbc2fcd 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -34,8 +34,8 @@ const path = require('path'); module.exports = function(opts) { const target = path.resolve(opts.target); /** - * @todo #368 - * It is necessary to use 'elapsed' in all logging cases that require output of elapsed time + * @todo #368 Wrap logs in 'elapsed' + * - It is necessary to use 'elapsed' in all logging cases that require output of elapsed time */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { console.info('Java .class files compiled into %s', rel(target)); diff --git a/src/elapsed.js b/src/elapsed.js index 5ead22d..4991911 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -23,13 +23,12 @@ */ /** - * @todo #368 - * Consider if this method belong is in the right place. + * @todo #368:30min Decide the proper location for the `elapsed` utility function. + * - Consider if this method belong is in the right place. * It might belong in a utility module. * For now, it remains here. * - * Also, review whether the test file for this method is located appropriately. - * It’s unclear if its current location is the best fit. + * - Review if the test file for this method is appropriately located, as its current location might not be ideal. */ /** @@ -49,7 +48,7 @@ */ module.exports.elapsed = function elapsed(task) { const startTime = Date.now(); - const tracked = { + return task({ print: (message) => { const duration = Date.now() - startTime; let extended; @@ -60,10 +59,9 @@ module.exports.elapsed = function elapsed(task) { } else { extended = `${Math.ceil(duration / 3600000)}min`; } - let msg = `${message} in ${extended}`; + const msg = `${message} in ${extended}`; console.info(msg); return msg; } - } - return task(tracked); + }); } diff --git a/test/test_elapsed.js b/test/test_elapsed.js index a6e8ba5..ac33272 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -26,43 +26,40 @@ const {elapsed} = require('../src/elapsed') const assert = require("assert"); describe('elapsed', function(){ - const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); + const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); it('measures time correctly', async () => { return elapsed(async (tracked) => { await snooze(300); return tracked.print("task"); - }).then( + }) .then( (actual)=> assert( /task in 30\d+ms/.test(actual), `Expected "${actual}" to match /task in 30\\d+ms/` ) ) }); - it('measures short time correctly', async () => { return elapsed(async (tracked) => { await snooze(10); return tracked.print("short task"); - }).then( + }) .then( (actual) => assert( /short task in 1\d+ms/.test(actual), `Expected "${actual}" to match /short task in 1\\d+ms/` ) ); }); - it('measures long time correctly', async () => { return elapsed(async (tracked) => { await snooze(1200); return tracked.print("long task"); - }).then( + }) .then( (actual) => assert( /long task in 2s/.test(actual), `Expected "${actual}" to match /long task in 2s/` ) ); }); - it('handles errors in task correctly', async () => { try { await elapsed(async (tracked) => { @@ -70,7 +67,7 @@ describe('elapsed', function(){ }); assert.fail("Expected an error to be thrown"); } catch (error) { - assert.equal(error.message, "task error"); + assert.throws(() => { throw error }, /task error/); } }); -}) +}) \ No newline at end of file From 42858f35c3f654448658b46de1b5eaa5fc34d6f4 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sat, 23 Nov 2024 19:30:39 +0300 Subject: [PATCH 08/17] fix(#368): fix up todo needful time --- src/commands/java/compile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index cbc2fcd..2a3016c 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -34,7 +34,7 @@ const path = require('path'); module.exports = function(opts) { const target = path.resolve(opts.target); /** - * @todo #368 Wrap logs in 'elapsed' + * @todo #368:30min Wrap logs in 'elapsed' * - It is necessary to use 'elapsed' in all logging cases that require output of elapsed time */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { From 357691e017dfdd151742597a9d3d0d8c7fc7089a Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Sun, 24 Nov 2024 17:58:43 +0300 Subject: [PATCH 09/17] fix(#368): formatted, simplified --- src/elapsed.js | 45 ++++++++++---------- test/test_elapsed.js | 97 ++++++++++++++++++++++---------------------- 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/elapsed.js b/src/elapsed.js index 4991911..af1585f 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -25,14 +25,13 @@ /** * @todo #368:30min Decide the proper location for the `elapsed` utility function. * - Consider if this method belong is in the right place. - * It might belong in a utility module. - * For now, it remains here. - * - * - Review if the test file for this method is appropriately located, as its current location might not be ideal. + * It might belong in a utility module. For now, it remains here. + * Review if the test file for this method is appropriately located, + * as its current location might not be ideal. */ /** - * A utility function to measure the elapsed time of a task and provide + * A utility function to measure the elapsed time of a task and provideÏ * detailed timing information. * * This function wraps a given task (callback function) and provides it with @@ -47,21 +46,21 @@ * `task` callback will be returned unchanged. */ module.exports.elapsed = function elapsed(task) { - const startTime = Date.now(); - return task({ - print: (message) => { - const duration = Date.now() - startTime; - let extended; - if (duration < 1000) { - extended = `${duration}ms`; - } else if (duration < 60 * 1000) { - extended = `${Math.ceil(duration / 1000)}s`; - } else { - extended = `${Math.ceil(duration / 3600000)}min`; - } - const msg = `${message} in ${extended}`; - console.info(msg); - return msg; - } - }); -} + const startTime = Date.now(); + return task({ + print: (message) => { + const duration = Date.now() - startTime; + let extended; + if (duration < 1000) { + extended = `${duration}ms`; + } else if (duration < 60 * 1000) { + extended = `${Math.ceil(duration / 1000)}s`; + } else { + extended = `${Math.ceil(duration / 3600000)}min`; + } + const msg = `${message} in ${extended}`; + console.info(msg); + return msg; + } + }); +}; diff --git a/test/test_elapsed.js b/test/test_elapsed.js index ac33272..25146fc 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -22,52 +22,53 @@ * SOFTWARE. */ -const {elapsed} = require('../src/elapsed') -const assert = require("assert"); +const {elapsed} = require('../src/elapsed'); +const assert = require('assert'); -describe('elapsed', function(){ - const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); - it('measures time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(300); - return tracked.print("task"); - }) .then( - (actual)=> assert( - /task in 30\d+ms/.test(actual), - `Expected "${actual}" to match /task in 30\\d+ms/` - ) - ) - }); - it('measures short time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(10); - return tracked.print("short task"); - }) .then( - (actual) => assert( - /short task in 1\d+ms/.test(actual), - `Expected "${actual}" to match /short task in 1\\d+ms/` - ) - ); - }); - it('measures long time correctly', async () => { - return elapsed(async (tracked) => { - await snooze(1200); - return tracked.print("long task"); - }) .then( - (actual) => assert( - /long task in 2s/.test(actual), - `Expected "${actual}" to match /long task in 2s/` - ) - ); - }); - it('handles errors in task correctly', async () => { - try { - await elapsed(async (tracked) => { - throw new Error("task error"); - }); - assert.fail("Expected an error to be thrown"); - } catch (error) { - assert.throws(() => { throw error }, /task error/); - } - }); -}) \ No newline at end of file +describe('elapsed', function() { + const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + it('measures time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(300); + return tracked.print('task'); + }) .then( + (actual)=> assert( + /task in 30\d+ms/.test(actual), + `Expected "${actual}" to match /task in 30\\d+ms/` + ) + ); + }); + it('measures short time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(10); + return tracked.print('short task'); + }) .then( + (actual) => assert( + /short task in 1\d+ms/.test(actual), + `Expected "${actual}" to match /short task in 1\\d+ms/` + ) + ); + }); + it('measures long time correctly', async () => { + return elapsed(async (tracked) => { + await snooze(1200); + return tracked.print('long task'); + }) .then( + (actual) => assert( + /long task in 2s/.test(actual), + `Expected "${actual}" to match /long task in 2s/` + ) + ); + }); + it('handles errors in task correctly', async () => { + await assert.rejects( + elapsed(async () => { + throw new Error("task error"); + }), + (error) => { + assert.throws(() => { throw error }, /task error/); + return true; + } + ); + }); +}); From 4abfb9c27ddad196e003cd0632fcc6856d40c652 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Mon, 25 Nov 2024 20:14:54 +0300 Subject: [PATCH 10/17] fix(#368): formatted --- test/test_elapsed.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_elapsed.js b/test/test_elapsed.js index 25146fc..c6123f2 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -63,10 +63,12 @@ describe('elapsed', function() { it('handles errors in task correctly', async () => { await assert.rejects( elapsed(async () => { - throw new Error("task error"); + throw new Error('task error'); }), (error) => { - assert.throws(() => { throw error }, /task error/); + assert.throws(() => { + throw error; + }, /task error/); return true; } ); From 58347691399eda47b1f5f4ad3549ba2d73644bea Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Tue, 26 Nov 2024 20:00:18 +0300 Subject: [PATCH 11/17] fix(#368): formatted --- src/commands/java/compile.js | 2 +- src/elapsed.js | 9 +++------ test/test_elapsed.js | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/commands/java/compile.js b/src/commands/java/compile.js index 2a3016c..def550a 100644 --- a/src/commands/java/compile.js +++ b/src/commands/java/compile.js @@ -35,7 +35,7 @@ module.exports = function(opts) { const target = path.resolve(opts.target); /** * @todo #368:30min Wrap logs in 'elapsed' - * - It is necessary to use 'elapsed' in all logging cases that require output of elapsed time + * It is necessary to use 'elapsed' in all logging cases that require output of elapsed time */ return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => { console.info('Java .class files compiled into %s', rel(target)); diff --git a/src/elapsed.js b/src/elapsed.js index af1585f..eb41fb8 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -23,15 +23,12 @@ */ /** - * @todo #368:30min Decide the proper location for the `elapsed` utility function. - * - Consider if this method belong is in the right place. - * It might belong in a utility module. For now, it remains here. - * Review if the test file for this method is appropriately located, - * as its current location might not be ideal. + * @todo #368:30min Decide if the `elapsed` utility function is in the right place + * Consider relocating `elapsed` utility function and its test file if needed */ /** - * A utility function to measure the elapsed time of a task and provideÏ + * A utility function to measure the elapsed time of a task and provide * detailed timing information. * * This function wraps a given task (callback function) and provides it with diff --git a/test/test_elapsed.js b/test/test_elapsed.js index c6123f2..3c41682 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -31,8 +31,8 @@ describe('elapsed', function() { return elapsed(async (tracked) => { await snooze(300); return tracked.print('task'); - }) .then( - (actual)=> assert( + }).then( + (actual) => assert( /task in 30\d+ms/.test(actual), `Expected "${actual}" to match /task in 30\\d+ms/` ) @@ -42,7 +42,7 @@ describe('elapsed', function() { return elapsed(async (tracked) => { await snooze(10); return tracked.print('short task'); - }) .then( + }).then( (actual) => assert( /short task in 1\d+ms/.test(actual), `Expected "${actual}" to match /short task in 1\\d+ms/` @@ -53,7 +53,7 @@ describe('elapsed', function() { return elapsed(async (tracked) => { await snooze(1200); return tracked.print('long task'); - }) .then( + }).then( (actual) => assert( /long task in 2s/.test(actual), `Expected "${actual}" to match /long task in 2s/` From 816cb0f3fb6216b4cf5e487ea10ffdaf4c66107d Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Tue, 26 Nov 2024 21:28:41 +0300 Subject: [PATCH 12/17] fix(#368): formatted --- src/elapsed.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/elapsed.js b/src/elapsed.js index eb41fb8..d44655e 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -23,8 +23,7 @@ */ /** - * @todo #368:30min Decide if the `elapsed` utility function is in the right place - * Consider relocating `elapsed` utility function and its test file if needed + * @todo #368:30min Check if `elapsed` and its tests are in the right place */ /** From b37f5bf15ce7bcd7efbdcaaf8c624f1cd7a1e57f Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Tue, 26 Nov 2024 21:37:08 +0300 Subject: [PATCH 13/17] fix(#368): formatted --- src/elapsed.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/elapsed.js b/src/elapsed.js index d44655e..004e6a2 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -23,7 +23,8 @@ */ /** - * @todo #368:30min Check if `elapsed` and its tests are in the right place + * @todo #368:30min Decide if the `elapsed` utility function is in the right place, + * consider relocating it and its test file if needed */ /** From 7ef9618bee3a8fd1721232980bc0c503fefbaa72 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Tue, 26 Nov 2024 21:38:39 +0300 Subject: [PATCH 14/17] fix(#368): formatted --- src/elapsed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/elapsed.js b/src/elapsed.js index 004e6a2..be29e33 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -24,7 +24,7 @@ /** * @todo #368:30min Decide if the `elapsed` utility function is in the right place, - * consider relocating it and its test file if needed + * consider relocating it and its test file if needed */ /** From 4dd5eeadbb125e3e3273ee6ee501c5da81536c55 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Wed, 27 Nov 2024 08:12:22 +0300 Subject: [PATCH 15/17] fix(#368): formatted --- src/elapsed.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/elapsed.js b/src/elapsed.js index be29e33..66937f7 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -22,11 +22,6 @@ * SOFTWARE. */ -/** - * @todo #368:30min Decide if the `elapsed` utility function is in the right place, - * consider relocating it and its test file if needed - */ - /** * A utility function to measure the elapsed time of a task and provide * detailed timing information. @@ -61,3 +56,7 @@ module.exports.elapsed = function elapsed(task) { } }); }; +/** + * @todo #368:30min Decide if the `elapsed` utility function is in the right place, + * consider relocating it and its test file if needed + */ From 9d3e2a8c57ed290af8ed333e541d34e82bef2690 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Wed, 27 Nov 2024 13:32:38 +0300 Subject: [PATCH 16/17] fix(#368): formatted --- src/elapsed.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/elapsed.js b/src/elapsed.js index 66937f7..dd9e58e 100644 --- a/src/elapsed.js +++ b/src/elapsed.js @@ -36,6 +36,8 @@ * is invoked with a `tracked` object as an argument. * @return {*} Result of the wrapped callback function. The result of the * `task` callback will be returned unchanged. + * @todo #368:30min Decide if the `elapsed` utility function is in the right place, + * consider relocating it and its test file if needed */ module.exports.elapsed = function elapsed(task) { const startTime = Date.now(); @@ -56,7 +58,3 @@ module.exports.elapsed = function elapsed(task) { } }); }; -/** - * @todo #368:30min Decide if the `elapsed` utility function is in the right place, - * consider relocating it and its test file if needed - */ From f91183393874fdd07961ba7ef553869f23eb1776 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Wed, 27 Nov 2024 14:21:44 +0300 Subject: [PATCH 17/17] fix(#368): simplify regex for time measurement in test --- test/test_elapsed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_elapsed.js b/test/test_elapsed.js index 3c41682..32834ae 100644 --- a/test/test_elapsed.js +++ b/test/test_elapsed.js @@ -33,8 +33,8 @@ describe('elapsed', function() { return tracked.print('task'); }).then( (actual) => assert( - /task in 30\d+ms/.test(actual), - `Expected "${actual}" to match /task in 30\\d+ms/` + /task in 3\d+ms/.test(actual), + `Expected "${actual}" to match /task in 3\\d+ms/` ) ); });