Skip to content

Commit

Permalink
Refactor BaseConnectionPool empty method
Browse files Browse the repository at this point in the history
Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
  • Loading branch information
bandinib-amzn committed Aug 14, 2023
1 parent 9b44c61 commit 3090248
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions lib/pool/BaseConnectionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/unit/base-connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 3090248

Please sign in to comment.