Skip to content

Commit

Permalink
test: add test for memory leak (#3450)
Browse files Browse the repository at this point in the history
* test: add test for memory leak

* lint
  • Loading branch information
snyamathi authored and mcollina committed Aug 17, 2024
1 parent c81f5a7 commit 5287054
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const { test } = require('node:test')
const assert = require('node:assert')
const { setImmediate } = require('node:timers/promises')
const { AsyncLocalStorage } = require('node:async_hooks')
const { tspl } = require('@matteo.collina/tspl')
const {
Response,
Expand Down Expand Up @@ -285,3 +287,29 @@ test('fromInnerResponse', () => {
assert.strictEqual(getHeadersList(response[kHeaders]), innerResponse.headersList)
assert.strictEqual(getHeadersGuard(response[kHeaders]), 'immutable')
})

test('clone body garbage collection', async () => {
const asyncLocalStorage = new AsyncLocalStorage()
let ref

await new Promise(resolve => {
asyncLocalStorage.run(new Map(), async () => {
const res = new Response('hello world')
const clone = res.clone()

asyncLocalStorage.getStore().set('key', clone)
ref = new WeakRef(clone.body)

await res.text()
await clone.text() // consume body

resolve()
})
})

await setImmediate()
global.gc()

const cloneBody = ref.deref()
assert.equal(cloneBody, undefined, 'clone body was not garbage collected')
})

0 comments on commit 5287054

Please sign in to comment.