From f2a00082f9f488087a8d09fef50870b9cd460509 Mon Sep 17 00:00:00 2001 From: Tomas Della Vedova Date: Tue, 24 Aug 2021 11:53:14 +0200 Subject: [PATCH] Always emit request aborted event (#1534) --- lib/Transport.js | 1 + test/acceptance/product-check.test.js | 63 ++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/lib/Transport.js b/lib/Transport.js index f6588225e..d998e6aa1 100644 --- a/lib/Transport.js +++ b/lib/Transport.js @@ -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 }) diff --git a/test/acceptance/product-check.test.js b/test/acceptance/product-check.test.js index cdda9449f..2ed7cec88 100644 --- a/test/acceptance/product-check.test.js +++ b/test/acceptance/product-check.test.js @@ -20,7 +20,7 @@ 'use strict' const { test } = require('tap') -const { Client } = require('../../') +const { Client, errors } = require('../../') const { connection: { MockConnectionTimeout, @@ -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()) +})