|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
| 20 | +import * as http from 'http' |
20 | 21 | import { test } from 'tap'
|
21 | 22 | import { URL } from 'url'
|
22 |
| -import { connection } from '../utils' |
| 23 | +import FakeTimers from '@sinonjs/fake-timers' |
| 24 | +import { buildServer, connection } from '../utils' |
23 | 25 | import { Client, errors } from '../..'
|
24 | 26 | import * as symbols from '@elastic/transport/lib/symbols'
|
25 | 27 | import { BaseConnectionPool, CloudConnectionPool } from '@elastic/transport'
|
@@ -463,3 +465,42 @@ test('user agent is in the correct format', t => {
|
463 | 465 | t.ok(/^\d+\.\d+\.\d+/.test(agentSplit[0].split('/')[1]))
|
464 | 466 | t.end()
|
465 | 467 | })
|
| 468 | + |
| 469 | +test('Ensure new client instance stores requestTimeout for each connection', t => { |
| 470 | + const client = new Client({ |
| 471 | + node: { url: new URL('http://localhost:9200') }, |
| 472 | + requestTimeout: 60000, |
| 473 | + }) |
| 474 | + t.equal(client.connectionPool.connections[0].timeout, 60000) |
| 475 | + t.end() |
| 476 | +}) |
| 477 | + |
| 478 | +test('Ensure new client does not time out at default (30s) when client sets requestTimeout', async t => { |
| 479 | + const clock = FakeTimers.install({ toFake: ['setTimeout', 'clearTimeout'] }) |
| 480 | + t.teardown(() => clock.uninstall()) |
| 481 | + |
| 482 | + function handler (_req: http.IncomingMessage, res: http.ServerResponse) { |
| 483 | + setTimeout(() => { |
| 484 | + t.pass('timeout ended') |
| 485 | + res.setHeader('content-type', 'application/json') |
| 486 | + res.end(JSON.stringify({ success: true })) |
| 487 | + }, 31000) // default is 30000 |
| 488 | + clock.runToLast() |
| 489 | + } |
| 490 | + |
| 491 | + const [{ port }, server] = await buildServer(handler) |
| 492 | + |
| 493 | + const client = new Client({ |
| 494 | + node: `http://localhost:${port}`, |
| 495 | + requestTimeout: 60000 |
| 496 | + }) |
| 497 | + |
| 498 | + try { |
| 499 | + await client.transport.request({ method: 'GET', path: '/' }) |
| 500 | + } catch (error) { |
| 501 | + t.fail('timeout error hit') |
| 502 | + } finally { |
| 503 | + server.stop() |
| 504 | + t.end() |
| 505 | + } |
| 506 | +}) |
0 commit comments