From: Jérôme Benoit Date: Mon, 19 Jun 2023 20:02:34 +0000 (+0200) Subject: fix: ensure simulator will stop after a timeout (120s) X-Git-Tag: v1.2.17~17 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1832a9868aa1ce376d7a408afa3c01c0f44a5cb3;p=e-mobility-charging-stations-simulator.git fix: ensure simulator will stop after a timeout (120s) Signed-off-by: Jérôme Benoit --- 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 */