summaryrefslogtreecommitdiffstats
path: root/test/client-unref.js
blob: c7e3a5db73481e79bfe2610cbb92f8b532deee0b (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
'use strict'

const { Worker, isMainThread, workerData } = require('worker_threads')

if (isMainThread) {
  const tap = require('tap')
  const { createServer } = require('http')

  tap.test('client automatically closes itself when idle', t => {
    t.plan(1)

    const server = createServer((req, res) => {
      res.end()
    })
    t.teardown(server.close.bind(server))
    server.keepAliveTimeout = 9999

    server.listen(0, () => {
      const url = `http://localhost:${server.address().port}`
      const worker = new Worker(__filename, { workerData: { url } })
      worker.on('exit', code => {
        t.equal(code, 0)
      })
    })
  })

  tap.test('client automatically closes itself if the server is not there', t => {
    t.plan(1)

    const url = 'http://localhost:4242' // hopefully empty port
    const worker = new Worker(__filename, { workerData: { url } })
    worker.on('exit', code => {
      t.equal(code, 0)
    })
  })
} else {
  const { Client } = require('..')

  const client = new Client(workerData.url)
  client.request({ path: '/', method: 'GET' }, () => {
    // We do not care about Errors

    setTimeout(() => {
      throw new Error()
    }, 1e3).unref()
  })
}