Skip to content

Commit 6e97b1a

Browse files
committed
Add test to verify default node filter function (#2756)
(cherry picked from commit 98b3802)
1 parent db41d28 commit 6e97b1a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface ClientOptions {
135135
* @defaultValue null */
136136
agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | false
137137
/** @property nodeFilter A custom function used by the connection pool to determine which nodes are qualified to receive a request
138-
* @defaultValue () => true */
138+
* @defaultValue A function that uses the Connection `roles` property to avoid master-only nodes */
139139
nodeFilter?: nodeFilterFn
140140
/** @property nodeSelector A custom function used by the connection pool to determine which node should receive the next request
141141
* @defaultValue A "round robin" function that loops sequentially through each node in the pool. */

test/unit/client.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ test('Missing node(s)', t => {
7777
t.end()
7878
})
7979

80+
test('multi nodes with roles, using default node filter', async t => {
81+
const client = new Client({
82+
nodes: [
83+
{
84+
url: new URL('http://node1:9200'),
85+
roles: { master: true, data: false, ingest: false, ml: false }
86+
},
87+
{
88+
url: new URL('http://node2:9200'),
89+
roles: { master: true, data: true, ingest: false, ml: false }
90+
},
91+
]
92+
})
93+
const conn = client.connectionPool.getConnection({
94+
now: Date.now() + 1000 * 60 * 3,
95+
requestId: 1,
96+
name: 'elasticsearch-js',
97+
context: null
98+
})
99+
100+
t.equal(conn?.url.hostname, 'node2')
101+
102+
t.end()
103+
})
104+
80105
test('Custom headers', t => {
81106
const client = new Client({
82107
node: 'http://localhost:9200',

0 commit comments

Comments
 (0)