-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
c8afae8
commit 9e68975
Showing
25 changed files
with
396 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
x-pack/plugins/apm/server/lib/helpers/create_es_client/cancel_es_request_on_abort.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport'; | ||
import { KibanaRequest } from 'src/core/server'; | ||
|
||
export function cancelEsRequestOnAbort<T extends TransportRequestPromise<any>>( | ||
promise: T, | ||
request: KibanaRequest | ||
) { | ||
const subscription = request.events.aborted$.subscribe(() => { | ||
promise.abort(); | ||
}); | ||
|
||
// using .catch() here means unsubscribe will be called | ||
// after it has thrown an error, so we use .then(onSuccess, onFailure) | ||
// syntax | ||
promise.then( | ||
() => subscription.unsubscribe(), | ||
() => subscription.unsubscribe() | ||
); | ||
|
||
return promise; | ||
} |
77 changes: 77 additions & 0 deletions
77
x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { contextServiceMock } from 'src/core/server/mocks'; | ||
import { createHttpServer } from 'src/core/server/test_utils'; | ||
import supertest from 'supertest'; | ||
import { createApmEventClient } from '.'; | ||
|
||
describe('createApmEventClient', () => { | ||
let server: ReturnType<typeof createHttpServer>; | ||
|
||
beforeEach(() => { | ||
server = createHttpServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await server.stop(); | ||
}); | ||
it('cancels a search when a request is aborted', async () => { | ||
const { server: innerServer, createRouter } = await server.setup({ | ||
context: contextServiceMock.createSetupContract(), | ||
}); | ||
const router = createRouter('/'); | ||
|
||
const abort = jest.fn(); | ||
router.get( | ||
{ path: '/', validate: false }, | ||
async (context, request, res) => { | ||
const eventClient = createApmEventClient({ | ||
esClient: { | ||
search: () => { | ||
return Object.assign( | ||
new Promise((resolve) => setTimeout(resolve, 3000)), | ||
{ abort } | ||
); | ||
}, | ||
} as any, | ||
debug: false, | ||
request, | ||
indices: {} as any, | ||
options: { | ||
includeFrozen: false, | ||
}, | ||
}); | ||
|
||
await eventClient.search({ | ||
apm: { | ||
events: [], | ||
}, | ||
}); | ||
|
||
return res.ok({ body: 'ok' }); | ||
} | ||
); | ||
|
||
await server.start(); | ||
|
||
const incomingRequest = supertest(innerServer.listener) | ||
.get('/') | ||
// end required to send request | ||
.end(); | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(() => { | ||
incomingRequest.abort(); | ||
setTimeout(() => { | ||
resolve(undefined); | ||
}, 0); | ||
}, 50); | ||
}); | ||
|
||
expect(abort).toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.