Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve gc detection #3504

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -304,6 +304,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
Loading