Skip to content

Commit f2a0008

Browse files
committed
Always emit request aborted event (#1534)
1 parent 8634de2 commit f2a0008

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/Transport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class Transport {
182182

183183
const makeRequest = () => {
184184
if (meta.aborted === true) {
185+
this.emit('request', new RequestAbortedError(), result)
185186
return process.nextTick(callback, new RequestAbortedError(), result)
186187
}
187188
meta.connection = this.getConnection({ requestId: meta.request.id })

test/acceptance/product-check.test.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'use strict'
2121

2222
const { test } = require('tap')
23-
const { Client } = require('../../')
23+
const { Client, errors } = require('../../')
2424
const {
2525
connection: {
2626
MockConnectionTimeout,
@@ -1285,3 +1285,64 @@ test('Observability events should have all the expected properties', t => {
12851285
t.equal(err.message, 'The client noticed that the server is not Elasticsearch and we do not support this unknown product.')
12861286
})
12871287
})
1288+
1289+
test('Abort a request while running the product check', t => {
1290+
t.plan(4)
1291+
const MockConnection = buildMockConnection({
1292+
onRequest (params) {
1293+
return {
1294+
statusCode: 200,
1295+
headers: {
1296+
'x-elastic-product': 'Elasticsearch'
1297+
},
1298+
body: {
1299+
name: '1ef419078577',
1300+
cluster_name: 'docker-cluster',
1301+
cluster_uuid: 'cQ5pAMvRRTyEzObH4L5mTA',
1302+
version: {
1303+
number: '8.0.0-SNAPSHOT',
1304+
build_flavor: 'default',
1305+
build_type: 'docker',
1306+
build_hash: '5fb4c050958a6b0b6a70a6fb3e616d0e390eaac3',
1307+
build_date: '2021-07-10T01:45:02.136546168Z',
1308+
build_snapshot: true,
1309+
lucene_version: '8.9.0',
1310+
minimum_wire_compatibility_version: '7.15.0',
1311+
minimum_index_compatibility_version: '7.0.0'
1312+
},
1313+
tagline: 'You Know, for Search'
1314+
}
1315+
}
1316+
}
1317+
})
1318+
1319+
const client = new Client({
1320+
node: 'http://localhost:9200',
1321+
Connection: MockConnection
1322+
})
1323+
1324+
client.on('request', (err, event) => {
1325+
if (event.meta.request.params.path.includes('search')) {
1326+
t.ok(err instanceof errors.RequestAbortedError)
1327+
}
1328+
})
1329+
1330+
// the response event won't be executed for the search
1331+
client.on('response', (err, event) => {
1332+
t.error(err)
1333+
t.equal(event.meta.request.params.path, '/')
1334+
})
1335+
1336+
const req = client.search({
1337+
index: 'foo',
1338+
body: {
1339+
query: {
1340+
match_all: {}
1341+
}
1342+
}
1343+
}, (err, result) => {
1344+
t.ok(err instanceof errors.RequestAbortedError)
1345+
})
1346+
1347+
setImmediate(() => req.abort())
1348+
})

0 commit comments

Comments
 (0)