-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: gracefully terminate connections when closing http server
- Loading branch information
Showing
6 changed files
with
159 additions
and
11 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
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
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,55 @@ | ||
import {ErrorAborted, TimeoutError} from "./errors.js"; | ||
|
||
export type WaitForOpts = { | ||
/** Time in milliseconds between checking condition */ | ||
interval?: number; | ||
/** Time in milliseconds to wait before throwing TimeoutError */ | ||
timeout?: number; | ||
/** Signal to abort waiting for condition by throwing ErrorAborted */ | ||
signal?: AbortSignal; | ||
}; | ||
|
||
/** | ||
* Wait for a condition to be true | ||
* | ||
* Simplified and abortable implementation of https://github.com/sindresorhus/p-wait-for | ||
*/ | ||
export function waitFor(condition: () => boolean, opts: WaitForOpts = {}): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const {interval = 10, timeout = Infinity, signal} = opts; | ||
|
||
if (signal && signal.aborted) { | ||
return reject(new ErrorAborted()); | ||
} | ||
|
||
if (condition()) { | ||
return resolve(); | ||
} | ||
|
||
let onDone: () => void = () => {}; | ||
|
||
const timeoutId = setTimeout(() => { | ||
onDone(); | ||
reject(new TimeoutError()); | ||
}, timeout); | ||
|
||
const intervalId = setInterval(() => { | ||
if (condition()) { | ||
onDone(); | ||
resolve(); | ||
} | ||
}, interval); | ||
|
||
const onAbort = (): void => { | ||
onDone(); | ||
reject(new ErrorAborted()); | ||
}; | ||
if (signal) signal.addEventListener("abort", onAbort); | ||
|
||
onDone = () => { | ||
clearTimeout(timeoutId); | ||
clearInterval(intervalId); | ||
if (signal) signal.removeEventListener("abort", onAbort); | ||
}; | ||
}); | ||
} |
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,37 @@ | ||
import "../setup.js"; | ||
import {expect} from "chai"; | ||
import {waitFor} from "../../src/waitFor.js"; | ||
import {ErrorAborted, TimeoutError} from "../../src/errors.js"; | ||
|
||
describe("waitFor", () => { | ||
const interval = 10; | ||
const timeout = 20; | ||
|
||
it("Should resolve if condition is already true", async () => { | ||
await expect(waitFor(() => true, {interval, timeout})).to.be.fulfilled; | ||
}); | ||
|
||
it("Should resolve if condition becomes true within timeout", async () => { | ||
let condition = false; | ||
setTimeout(() => { | ||
condition = true; | ||
}, interval); | ||
await waitFor(() => condition, {interval, timeout}); | ||
}); | ||
|
||
it("Should reject with TimeoutError if condition does not become true within timeout", async () => { | ||
await expect(waitFor(() => false, {interval, timeout})).to.be.rejectedWith(TimeoutError); | ||
}); | ||
|
||
it("Should reject with ErrorAborted if aborted before condition becomes true", async () => { | ||
const controller = new AbortController(); | ||
setTimeout(() => controller.abort(), interval); | ||
await expect(waitFor(() => false, {interval, timeout, signal: controller.signal})).to.be.rejectedWith(ErrorAborted); | ||
}); | ||
|
||
it("Should reject with ErrorAborted if signal is already aborted", async () => { | ||
const controller = new AbortController(); | ||
controller.abort(); | ||
await expect(waitFor(() => true, {interval, timeout, signal: controller.signal})).to.be.rejectedWith(ErrorAborted); | ||
}); | ||
}); |