Skip to content

Always emit request aborted event #1534

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

Merged
merged 4 commits into from
Aug 24, 2021
Merged
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 lib/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class Transport {

const makeRequest = () => {
if (meta.aborted === true) {
this.emit('request', new RequestAbortedError(), result)
return process.nextTick(callback, new RequestAbortedError(), result)
}
meta.connection = this.getConnection({ requestId: meta.request.id })
Expand Down
63 changes: 62 additions & 1 deletion test/acceptance/product-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'use strict'

const { test } = require('tap')
const { Client } = require('../../')
const { Client, errors } = require('../../')
const {
connection: {
MockConnectionTimeout,
Expand Down Expand Up @@ -1285,3 +1285,64 @@ test('Observability events should have all the expected properties', t => {
t.equal(err.message, 'The client noticed that the server is not Elasticsearch and we do not support this unknown product.')
})
})

test('Abort a request while running the product check', t => {
t.plan(4)
const MockConnection = buildMockConnection({
onRequest (params) {
return {
statusCode: 200,
headers: {
'x-elastic-product': 'Elasticsearch'
},
body: {
name: '1ef419078577',
cluster_name: 'docker-cluster',
cluster_uuid: 'cQ5pAMvRRTyEzObH4L5mTA',
version: {
number: '8.0.0-SNAPSHOT',
build_flavor: 'default',
build_type: 'docker',
build_hash: '5fb4c050958a6b0b6a70a6fb3e616d0e390eaac3',
build_date: '2021-07-10T01:45:02.136546168Z',
build_snapshot: true,
lucene_version: '8.9.0',
minimum_wire_compatibility_version: '7.15.0',
minimum_index_compatibility_version: '7.0.0'
},
tagline: 'You Know, for Search'
}
}
}
})

const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection
})

client.on('request', (err, event) => {
if (event.meta.request.params.path.includes('search')) {
t.ok(err instanceof errors.RequestAbortedError)
}
})

// the response event won't be executed for the search
client.on('response', (err, event) => {
t.error(err)
t.equal(event.meta.request.params.path, '/')
})

const req = client.search({
index: 'foo',
body: {
query: {
match_all: {}
}
}
}, (err, result) => {
t.ok(err instanceof errors.RequestAbortedError)
})

setImmediate(() => req.abort())
})