summaryrefslogtreecommitdiffstats
path: root/test/connect-timeout.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/connect-timeout.js')
-rw-r--r--test/connect-timeout.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/test/connect-timeout.js b/test/connect-timeout.js
new file mode 100644
index 0000000..a736a54
--- /dev/null
+++ b/test/connect-timeout.js
@@ -0,0 +1,68 @@
+'use strict'
+
+const { test } = require('tap')
+const { Client, Pool, errors } = require('..')
+const net = require('net')
+const sleep = require('atomic-sleep')
+
+test('priotorise socket errors over timeouts', (t) => {
+ t.plan(1)
+ const connectTimeout = 1000
+ const client = new Pool('http://foobar.bar:1234', { connectTimeout: 2 })
+
+ client.request({ method: 'GET', path: '/foobar' })
+ .then(() => t.fail())
+ .catch((err) => {
+ t.equal(err.code, 'ENOTFOUND')
+ })
+
+ // block for 1s which is enough for the dns lookup to complete and TO to fire
+ sleep(connectTimeout)
+})
+
+// never connect
+net.connect = function (options) {
+ return new net.Socket(options)
+}
+
+test('connect-timeout', t => {
+ t.plan(1)
+
+ const client = new Client('http://localhost:9000', {
+ connectTimeout: 1e3
+ })
+ t.teardown(client.close.bind(client))
+
+ const timeout = setTimeout(() => {
+ t.fail()
+ }, 2e3)
+
+ client.request({
+ path: '/',
+ method: 'GET'
+ }, (err) => {
+ t.type(err, errors.ConnectTimeoutError)
+ clearTimeout(timeout)
+ })
+})
+
+test('connect-timeout', t => {
+ t.plan(1)
+
+ const client = new Pool('http://localhost:9000', {
+ connectTimeout: 1e3
+ })
+ t.teardown(client.close.bind(client))
+
+ const timeout = setTimeout(() => {
+ t.fail()
+ }, 2e3)
+
+ client.request({
+ path: '/',
+ method: 'GET'
+ }, (err) => {
+ t.type(err, errors.ConnectTimeoutError)
+ clearTimeout(timeout)
+ })
+})