From 3090248a3efc43d1d65d1950bf1799e46d62a925 Mon Sep 17 00:00:00 2001 From: Bandini Bhopi Date: Sat, 12 Aug 2023 01:43:49 +0000 Subject: [PATCH] Refactor BaseConnectionPool empty method Signed-off-by: Bandini Bhopi --- CHANGELOG.md | 1 + lib/pool/BaseConnectionPool.js | 11 ++++++----- test/unit/base-connection-pool.test.js | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec9eabeae..b224ded94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Bumps `eslint-config-prettier` from 8.8.0 to 9.0.0 ### Changed - Make handling of long numerals an option that is disabled by default ([#557](https://github.com/opensearch-project/opensearch-js/pull/557)) +- Refactor BaseConnectionPool empty method so that callback will always get called even when there is no connection ([#585](https://github.com/opensearch-project/opensearch-js/pull/585)) ### Deprecated ### Removed ### Fixed diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index 30c5ad8df..a01ec5f24 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -150,13 +150,14 @@ class BaseConnectionPool { let openConnections = this.size; this.connections.forEach((connection) => { connection.close(() => { - if (--openConnections === 0) { - this.connections = []; - this.size = this.connections.length; - callback(); - } + openConnections--; }); }); + if (openConnections === 0) { + this.connections = []; + this.size = this.connections.length; + callback(); + } } /** diff --git a/test/unit/base-connection-pool.test.js b/test/unit/base-connection-pool.test.js index 5b2ea5fc7..bb1842fb0 100644 --- a/test/unit/base-connection-pool.test.js +++ b/test/unit/base-connection-pool.test.js @@ -156,6 +156,14 @@ test('API', (t) => { t.end(); }); + t.test('empty with no connection', (t) => { + const pool = new BaseConnectionPool({ Connection }); + t.equal(pool.connections.length, 0); + pool.empty(); + t.equal(pool.size, 0); + t.end(); + }); + t.test('urlToHost', (t) => { const pool = new BaseConnectionPool({ Connection }); const url = 'http://localhost:9200';