Updated abort behavior (#1141)
* Updated abort behavior - Support for aborting a request with the promise api - Aborting a request will cause a RequestAbortedError - Normalized Connection class errors, now every error returned is wrapped by the client errors constructors * Updated test * Updated docs * Updated code generation script * Renamed test * Code coverage * Avoid calling twice transport.request
This commit is contained in:
committed by
GitHub
parent
953a8033ab
commit
27a8e2a9bf
@ -21,7 +21,8 @@ const {
|
||||
TimeoutError,
|
||||
ResponseError,
|
||||
ConnectionError,
|
||||
ConfigurationError
|
||||
ConfigurationError,
|
||||
RequestAbortedError
|
||||
} = require('../../lib/errors')
|
||||
|
||||
const ConnectionPool = require('../../lib/pool/ConnectionPool')
|
||||
@ -88,6 +89,32 @@ test('Basic (promises support)', t => {
|
||||
.catch(t.fail)
|
||||
})
|
||||
|
||||
test('Basic - failing (promises support)', t => {
|
||||
t.plan(1)
|
||||
|
||||
const pool = new ConnectionPool({ Connection: MockConnectionTimeout })
|
||||
pool.addConnection('http://localhost:9200')
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport
|
||||
.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
})
|
||||
.catch(err => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
})
|
||||
})
|
||||
|
||||
test('Basic (options + promises support)', t => {
|
||||
t.plan(1)
|
||||
|
||||
@ -764,7 +791,7 @@ test('Should call resurrect on every request', t => {
|
||||
test('Should return a request aborter utility', t => {
|
||||
t.plan(1)
|
||||
|
||||
const pool = new ConnectionPool({ Connection, MockConnection })
|
||||
const pool = new ConnectionPool({ Connection: MockConnection })
|
||||
pool.addConnection({
|
||||
url: new URL('http://localhost:9200'),
|
||||
id: 'node1'
|
||||
@ -783,12 +810,11 @@ test('Should return a request aborter utility', t => {
|
||||
const request = transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (_err, body) => {
|
||||
t.fail('Should not be called')
|
||||
}, (err, result) => {
|
||||
t.ok(err instanceof RequestAbortedError)
|
||||
})
|
||||
|
||||
request.abort()
|
||||
t.pass('ok')
|
||||
})
|
||||
|
||||
test('Retry mechanism and abort', t => {
|
||||
@ -819,8 +845,6 @@ test('Retry mechanism and abort', t => {
|
||||
emit: event => {
|
||||
if (event === 'request' && count++ > 0) {
|
||||
request.abort()
|
||||
server.stop()
|
||||
t.pass('ok')
|
||||
}
|
||||
},
|
||||
connectionPool: pool,
|
||||
@ -834,12 +858,48 @@ test('Retry mechanism and abort', t => {
|
||||
const request = transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (e, { body }) => {
|
||||
t.fail('Should not be called')
|
||||
}, (err, result) => {
|
||||
t.ok(err instanceof RequestAbortedError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Abort a request with the promise API', t => {
|
||||
t.plan(1)
|
||||
|
||||
const pool = new ConnectionPool({ Connection: MockConnection })
|
||||
pool.addConnection({
|
||||
url: new URL('http://localhost:9200'),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
const request = transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
})
|
||||
|
||||
request
|
||||
.then(() => {
|
||||
t.fail('Should not be called')
|
||||
})
|
||||
.catch(err => {
|
||||
t.ok(err instanceof RequestAbortedError)
|
||||
})
|
||||
|
||||
request.abort()
|
||||
})
|
||||
|
||||
test('ResponseError', t => {
|
||||
t.plan(3)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user