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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
'use strict'
/* global WeakRef, FinalizationRegistry */
const { test } = require('tap')
const { createServer } = require('net')
const { Client, Pool } = require('..')
const SKIP = typeof WeakRef === 'undefined' || typeof FinalizationRegistry === 'undefined'
setInterval(() => {
global.gc()
}, 100).unref()
test('gc should collect the client if, and only if, there are no active sockets', { skip: SKIP }, t => {
t.plan(4)
const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
socket.write('Keep-Alive: timeout=1s\r\n')
socket.write('Connection: keep-alive\r\n')
socket.write('\r\n\r\n')
})
t.teardown(server.close.bind(server))
let weakRef
let disconnected = false
const registry = new FinalizationRegistry((data) => {
t.equal(data, 'test')
t.equal(disconnected, true)
t.equal(weakRef.deref(), undefined)
})
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeoutThreshold: 100
})
client.once('disconnect', () => {
disconnected = true
})
weakRef = new WeakRef(client)
registry.register(client, 'test')
client.request({
path: '/',
method: 'GET'
}, (err, { body }) => {
t.error(err)
body.resume()
})
})
})
test('gc should collect the pool if, and only if, there are no active sockets', { skip: SKIP }, t => {
t.plan(4)
const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
socket.write('Keep-Alive: timeout=1s\r\n')
socket.write('Connection: keep-alive\r\n')
socket.write('\r\n\r\n')
})
t.teardown(server.close.bind(server))
let weakRef
let disconnected = false
const registry = new FinalizationRegistry((data) => {
t.equal(data, 'test')
t.equal(disconnected, true)
t.equal(weakRef.deref(), undefined)
})
server.listen(0, () => {
const pool = new Pool(`http://localhost:${server.address().port}`, {
connections: 1,
keepAliveTimeoutThreshold: 500
})
pool.once('disconnect', () => {
disconnected = true
})
weakRef = new WeakRef(pool)
registry.register(pool, 'test')
pool.request({
path: '/',
method: 'GET'
}, (err, { body }) => {
t.error(err)
body.resume()
})
})
})
|