forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
433 additions
and
191 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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
/* | ||
* 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(); | ||
}); | ||
|
||
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 supertest from 'supertest'; | ||
import { createApmEventClient } from '.'; | ||
import { createHttpServer } from '../../../../../../../../src/core/server/test_utils'; | ||
|
||
describe('create_apm_event_client', () => { | ||
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.