From cf552d52a4e26ee60c47684eda573ed5fa31f4d6 Mon Sep 17 00:00:00 2001 From: Vladyslav Karpenko Date: Tue, 19 Dec 2023 19:12:42 +0200 Subject: [PATCH] Reimplement global timeouts.request usage Updated and added test cases to control the behavior: - if application env has no timeouts.request in the config Procedure fallback to 0 timeout - config.server.timeouts.request applied to procedure script execution - local timeout setting of the script overrides global timeouts.request in both directions. Closes: https://github.com/metarhia/impress/issues/1947 Refs: https://github.com/metarhia/impress/pull/1948 PR-URL: https://github.com/metarhia/impress/pull/1949 --- CHANGELOG.md | 1 + lib/procedure.js | 3 ++- test/api.js | 1 + test/procedure.js | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f8332db..613c9868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased][unreleased] - Fixed API endpoints local queue settings applying +- Reimplement global timeouts.request usage during a Procedure invocation ## [3.0.13][] - 2023-10-22 diff --git a/lib/procedure.js b/lib/procedure.js index 1c3caf7a..dbc4436e 100644 --- a/lib/procedure.js +++ b/lib/procedure.js @@ -31,7 +31,8 @@ class Procedure { this.description = exp.description || ''; this.access = exp.access || ''; this.validate = exp.validate || null; - this.timeout = exp.timeout || 0; + const { timeouts } = application.config.server; + this.timeout = (exp.timeout ?? timeouts.request) || 0; this.serializer = exp.serialize || null; this.protocols = exp.protocols || null; this.deprecated = exp.deprecated || false; diff --git a/test/api.js b/test/api.js index b7b8c640..762d16e0 100644 --- a/test/api.js +++ b/test/api.js @@ -14,6 +14,7 @@ const application = { absolute(relative) { return path.join(this.path, relative); }, + config: { server: { timeouts: {} } }, }; metatests.testAsync('lib/api load', async (test) => { diff --git a/test/procedure.js b/test/procedure.js index 89776d43..7bf34ada 100644 --- a/test/procedure.js +++ b/test/procedure.js @@ -15,6 +15,7 @@ metatests.testAsync('lib/procedure', async (test) => { async enter() {}, leave() {}, }, + config: { server: { timeouts: {} } }, }; const procedure = new Procedure(script, 'method', application); @@ -65,6 +66,7 @@ metatests.testAsync('lib/procedure validate', async (test) => { async enter() {}, leave() {}, }, + config: { server: { timeouts: {} } }, }; const procedure = new Procedure(script, 'method', application); @@ -94,6 +96,7 @@ metatests.testAsync('lib/procedure validate async', async (test) => { async enter() {}, leave() {}, }, + config: { server: { timeouts: {} } }, }; const procedure = new Procedure(script, 'method', application); @@ -123,6 +126,7 @@ metatests.testAsync('lib/procedure timeout', async (test) => { async enter() {}, leave() {}, }, + config: { server: { timeouts: { request: 20 } } }, }; const procedure = new Procedure(script, 'method', application); @@ -157,6 +161,7 @@ metatests.testAsync('lib/procedure queue', async (test) => { async enter() {}, leave() {}, }, + config: { server: { timeouts: {} } }, }; const rpc = async (proc, args) => { @@ -203,3 +208,32 @@ metatests.testAsync('lib/procedure queue', async (test) => { return last.value; }, new Error('Semaphore queue is full')); }); + +metatests.testAsync('lib/procedure global timeouts.request', async (test) => { + const DONE = 'success'; + + const script = () => ({ + timeout: undefined, + + method: async ({ waitTime }) => + new Promise((resolve) => { + setTimeout(() => resolve(DONE), waitTime); + }), + }); + + const application = { + Error, + semaphore: { + async enter() {}, + leave() {}, + }, + config: { server: { timeouts: { request: 10 } } }, + }; + + const procedure = new Procedure(script, 'method', application); + + await test.rejects( + async () => procedure.invoke({}, { waitTime: 20 }), + new Error('Timeout of 10ms reached'), + ); +});