summaryrefslogtreecommitdiffstats
path: root/test/connect-timeout.js
blob: a736a5401706ff3802ea230464f50f6cb4fa7436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
  })
})