Skip to content

Commit

Permalink
test: improve gc detection (#3504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Aug 27, 2024
1 parent c2c6161 commit e6a466f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions test/fetch/fetch-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const { fetch } = require('../..')
const { createServer } = require('node:http')
const { closeServerAsPromise } = require('../utils/node-http')

const hasGC = typeof global.gc !== 'undefined'

test('do not leak', (t, done) => {
if (!hasGC) {
throw new Error('gc is not available. Run with \'--expose-gc\'.')
}
const { ok } = tspl(t, { plan: 1 })
const server = createServer((req, res) => {
res.end()
Expand Down
4 changes: 4 additions & 0 deletions test/fetch/fire-and-forget.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ const blob = randomFillSync(new Uint8Array(1024 * 512))

// Enable when/if FinalizationRegistry in Node.js 18 becomes stable again
const isNode18 = process.version.startsWith('v18')
const hasGC = typeof global.gc !== 'undefined'

test('does not need the body to be consumed to continue', { timeout: 180_000, skip: isNode18 }, async (t) => {
if (!hasGC) {
throw new Error('gc is not available. Run with \'--expose-gc\'.')
}
const agent = new Agent({
keepAliveMaxTimeout: 10,
keepAliveTimeoutThreshold: 10
Expand Down
3 changes: 3 additions & 0 deletions test/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ test('fromInnerResponse', () => {
})

test('clone body garbage collection', async () => {
if (typeof global.gc === 'undefined') {
throw new Error('gc is not available. Run with \'--expose-gc\'.')
}
const asyncLocalStorage = new AsyncLocalStorage()
let ref

Expand Down
24 changes: 16 additions & 8 deletions test/gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ const { test, after } = require('node:test')
const { createServer } = require('node:net')
const { Client, Pool } = require('..')

const skip = typeof global.gc === 'undefined'

setInterval(() => {
global.gc()
}, 100).unref()

test('gc should collect the client if, and only if, there are no active sockets', { skip }, async t => {
const hasGC = typeof global.gc !== 'undefined'

if (hasGC) {
setInterval(() => {
global.gc()
}, 100).unref()
}

test('gc should collect the client if, and only if, there are no active sockets', async t => {
if (!hasGC) {
throw new Error('gc is not available. Run with \'--expose-gc\'.')
}
t = tspl(t, { plan: 4 })

const server = createServer((socket) => {
Expand Down Expand Up @@ -56,7 +61,10 @@ test('gc should collect the client if, and only if, there are no active sockets'
await t.completed
})

test('gc should collect the pool if, and only if, there are no active sockets', { skip }, async t => {
test('gc should collect the pool if, and only if, there are no active sockets', async t => {
if (!hasGC) {
throw new Error('gc is not available. Run with \'--expose-gc\'.')
}
t = tspl(t, { plan: 4 })

const server = createServer((socket) => {
Expand Down

0 comments on commit e6a466f

Please # to comment.