Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BaseConnectionPool empty method #585

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is really a bug fix, the callback should be called when there's no connections. So I would move this to fixes and say "Fix: ensure connection pool callback with no connections"

### 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();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test that ensures the callback is called, too?


t.test('urlToHost', (t) => {
const pool = new BaseConnectionPool({ Connection });
const url = 'http://localhost:9200';
Expand Down
Loading