summaryrefslogtreecommitdiffstats
path: root/test/fetch/fetch-leak.js
blob: b8e6b16709fbc5ca69b6c7224e2d781433e29191 (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
'use strict'

const { test } = require('tap')
const { fetch } = require('../..')
const { createServer } = require('http')

test('do not leak', (t) => {
  t.plan(1)

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

  let url
  let done = false
  server.listen(0, function attack () {
    if (done) {
      return
    }
    url ??= new URL(`http://127.0.0.1:${server.address().port}`)
    const controller = new AbortController()
    fetch(url, { signal: controller.signal })
      .then(res => res.arrayBuffer())
      .catch(() => {})
      .then(attack)
  })

  let prev = Infinity
  let count = 0
  const interval = setInterval(() => {
    done = true
    global.gc()
    const next = process.memoryUsage().heapUsed
    if (next <= prev) {
      t.pass()
    } else if (count++ > 20) {
      t.fail()
    } else {
      prev = next
    }
  }, 1e3)
  t.teardown(() => clearInterval(interval))
})