Skip to content

Commit 5702807

Browse files
JoshMockgithub-actions[bot]
authored andcommitted
1 parent 074b335 commit 5702807

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

test/unit/client.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
* under the License.
1818
*/
1919

20+
import * as http from 'http'
2021
import { test } from 'tap'
2122
import { URL } from 'url'
22-
import { connection } from '../utils'
23+
import FakeTimers from '@sinonjs/fake-timers'
24+
import { buildServer, connection } from '../utils'
2325
import { Client, errors } from '../..'
2426
import * as symbols from '@elastic/transport/lib/symbols'
2527
import { BaseConnectionPool, CloudConnectionPool } from '@elastic/transport'
@@ -463,3 +465,42 @@ test('user agent is in the correct format', t => {
463465
t.ok(/^\d+\.\d+\.\d+/.test(agentSplit[0].split('/')[1]))
464466
t.end()
465467
})
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

Comments
 (0)