-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: memleak in timeout w/ abort signal (#32)
- Loading branch information
Showing
2 changed files
with
64 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as events from 'node:events'; | ||
import { timeout } from '../time'; | ||
|
||
describe('Helper tests', () => { | ||
test('timeout function should not cause memory leak by accumulating abort listeners on abort', async () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
const countListeners = () => events.getEventListeners(signal, 'abort').length; | ||
|
||
// Ensure the initial listener count is zero | ||
expect(countListeners()).toBe(0); | ||
|
||
// Run enough iterations to detect a pattern | ||
for (let i = 0; i < 100; i++) { | ||
try { | ||
const sleepPromise = timeout(1000, signal); | ||
controller.abort(); // Abort immediately | ||
await sleepPromise; | ||
} catch (err: any) { | ||
expect(err.toString()).toMatch(/aborted/i); | ||
} | ||
|
||
// Assert that listener count does not increase | ||
expect(countListeners()).toBeLessThanOrEqual(1); // 1 listener may temporarily be added and removed | ||
} | ||
|
||
// Final check to confirm listeners are cleaned up | ||
expect(countListeners()).toBe(0); | ||
}); | ||
|
||
test('timeout function should not cause memory leak by accumulating abort listeners on successful completion', async () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
const countListeners = () => events.getEventListeners(signal, 'abort').length; | ||
|
||
// Ensure the initial listener count is zero | ||
expect(countListeners()).toBe(0); | ||
|
||
// Run enough iterations to detect a pattern | ||
for (let i = 0; i < 100; i++) { | ||
await timeout(2, signal); // Complete sleep without abort | ||
|
||
// Assert that listener count does not increase | ||
expect(countListeners()).toBe(0); // No listeners should remain after successful sleep completion | ||
} | ||
|
||
// Final check to confirm listeners are cleaned up | ||
expect(countListeners()).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters