-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sleep function memory leak (#5023)
Fixes #4817
- Loading branch information
Showing
2 changed files
with
54 additions
and
13 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
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,38 @@ | ||
import { jest } from '@jest/globals'; | ||
|
||
import { InterruptError } from '../errors/index.js'; | ||
import { InterruptibleSleep } from './index.js'; | ||
|
||
describe('InterruptibleSleep', () => { | ||
it('should sleep for 100ms', async () => { | ||
const sleeper = new InterruptibleSleep(); | ||
const start = Date.now(); | ||
await sleeper.sleep(100); | ||
const end = Date.now(); | ||
// -1 ms wiggle room for rounding errors | ||
expect(end - start).toBeGreaterThanOrEqual(99); | ||
}); | ||
|
||
it('can start multiple sleeps', async () => { | ||
const sleeper = new InterruptibleSleep(); | ||
const start = Date.now(); | ||
await Promise.all([sleeper.sleep(100), sleeper.sleep(150)]); | ||
const end = Date.now(); | ||
expect(end - start).toBeGreaterThanOrEqual(149); | ||
}); | ||
|
||
it('can interrup multiple sleeps', async () => { | ||
const stub = jest.fn(); | ||
const sleeper = new InterruptibleSleep(); | ||
const start = Date.now(); | ||
let end1; | ||
const sleep1 = sleeper.sleep(100).then(() => { | ||
end1 = Date.now(); | ||
}); | ||
const sleep2 = sleeper.sleep(150).then(stub); | ||
setTimeout(() => sleeper.interrupt(true), 125); | ||
await Promise.all([sleep1, sleep2]).catch(e => expect(e).toBeInstanceOf(InterruptError)); | ||
expect(end1! - start).toBeGreaterThanOrEqual(99); | ||
expect(stub).not.toHaveBeenCalled(); | ||
}); | ||
}); |