Updated Connections handling (#1127)

* Updated Connections handling

- The ConnectionPool.getConnection method now always returns a connection
- The ConnectionPool.update mehtod now cleans the dead list
- Deprecated the NoLivingConnectionsError

* Updated test

* Updated docs

* The NoLivingConnectionsError can still happen if the filter/selector returns no nodes

* Updated test

* Updated docs

* Catch undefined connections as well

* Updated test

* Updated ApiError type def
This commit is contained in:
Tomas Della Vedova
2020-03-26 13:41:05 +01:00
committed by GitHub
parent 85616b07ef
commit f913f7d2d2
7 changed files with 62 additions and 49 deletions

View File

@ -249,6 +249,20 @@ test('API', t => {
pool.getConnection({ filter })
})
t.test('If all connections are marked as dead, getConnection should return a dead connection', t => {
const pool = new ConnectionPool({ Connection })
const href1 = 'http://localhost:9200/'
const href2 = 'http://localhost:9200/other'
const conn1 = pool.addConnection(href1)
const conn2 = pool.addConnection(href2)
pool.markDead(conn1)
pool.markDead(conn2)
const conn = pool.getConnection()
t.ok(conn instanceof Connection)
t.is(conn.status, 'dead')
t.end()
})
t.end()
})
@ -762,27 +776,3 @@ test('Node filter', t => {
t.end()
})
test('Single node behavior', t => {
t.test('sniffing disabled (markDead and markAlive should be noop)', t => {
t.plan(4)
const pool = new ConnectionPool({ Connection, sniffEnabled: false })
const conn = pool.addConnection('http://localhost:9200/')
t.true(pool.markDead(conn) instanceof ConnectionPool)
t.strictEqual(pool.dead.length, 0)
t.true(pool.markAlive(conn) instanceof ConnectionPool)
t.strictEqual(pool.dead.length, 0)
})
t.test('sniffing enabled (markDead and markAlive should work)', t => {
t.plan(4)
const pool = new ConnectionPool({ Connection, sniffEnabled: true })
const conn = pool.addConnection('http://localhost:9200/')
t.true(pool.markDead(conn) instanceof ConnectionPool)
t.strictEqual(pool.dead.length, 1)
t.true(pool.markAlive(conn) instanceof ConnectionPool)
t.strictEqual(pool.dead.length, 0)
})
t.end()
})

View File

@ -335,9 +335,10 @@ test('Not JSON payload from server', t => {
})
})
test('NoLivingConnectionsError', t => {
t.plan(1)
test('NoLivingConnectionsError (null connection)', t => {
t.plan(3)
const pool = new ConnectionPool({ Connection })
pool.addConnection('http://localhost:9200')
const transport = new Transport({
emit: () => {},
@ -346,7 +347,40 @@ test('NoLivingConnectionsError', t => {
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
sniffOnStart: false,
nodeSelector (connections) {
t.is(connections.length, 1)
t.true(connections[0] instanceof Connection)
return null
}
})
transport.request({
method: 'GET',
path: '/hello'
}, (err, { body }) => {
t.ok(err instanceof NoLivingConnectionsError)
})
})
test('NoLivingConnectionsError (undefined connection)', t => {
t.plan(3)
const pool = new ConnectionPool({ Connection })
pool.addConnection('http://localhost:9200')
const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false,
nodeSelector (connections) {
t.is(connections.length, 1)
t.true(connections[0] instanceof Connection)
return undefined
}
})
transport.request({