From 1832a9868aa1ce376d7a408afa3c01c0f44a5cb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 19 Jun 2023 22:02:34 +0200 Subject: [PATCH] fix: ensure simulator will stop after a timeout (120s) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 21 ++++++++++++++++----- src/utils/Constants.ts | 2 ++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index d56beefc..3612f281 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -162,11 +162,22 @@ export class Bootstrap extends EventEmitter { Constants.EMPTY_FREEZED_OBJECT ) ); - await ChargingStationUtils.waitForChargingStationEvents( - this, - ChargingStationWorkerMessageEvents.stopped, - this.numberOfChargingStations - ); + await Promise.race([ + ChargingStationUtils.waitForChargingStationEvents( + this, + ChargingStationWorkerMessageEvents.stopped, + this.numberOfChargingStations + ), + new Promise((resolve) => { + setTimeout(() => { + const message = `Timeout reached ${Utils.formatDurationMilliSeconds( + Constants.STOP_SIMULATOR_TIMEOUT + )} at stopping charging stations simulator`; + console.warn(chalk.yellow(message)); + resolve(message); + }, Constants.STOP_SIMULATOR_TIMEOUT); + }), + ]); await this.workerImplementation?.stop(); this.workerImplementation = null; this.uiServer?.stop(); diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index bb765c75..30327b6a 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -47,6 +47,8 @@ export class Constants { static readonly MAX_RANDOM_INTEGER = 281474976710654; + static readonly STOP_SIMULATOR_TIMEOUT = 120000; // Ms + static readonly EMPTY_FREEZED_OBJECT = Object.freeze({}); static readonly EMPTY_FUNCTION = Object.freeze(() => { /* This is intentional */ -- 2.34.1