-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5dbe07
commit 264c714
Showing
2 changed files
with
82 additions
and
16 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 |
---|---|---|
@@ -1,34 +1,84 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { assertEquals, assertRejects } from "../testing/asserts.ts"; | ||
import { deferred } from "./deferred.ts"; | ||
import { delay } from "./delay.ts"; | ||
import { deadline, DeadlineError } from "./deadline.ts"; | ||
|
||
Deno.test("[async] deadline: return fulfilled promise", async () => { | ||
const p = deferred(); | ||
const t = setTimeout(() => p.resolve("Hello"), 100); | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const p = delay(100, { signal }) | ||
.catch(() => {}) | ||
.then(() => "Hello"); | ||
const result = await deadline(p, 1000); | ||
assertEquals(result, "Hello"); | ||
clearTimeout(t); | ||
controller.abort(); | ||
}); | ||
|
||
Deno.test("[async] deadline: throws DeadlineError", async () => { | ||
const p = deferred(); | ||
const t = setTimeout(() => p.resolve("Hello"), 1000); | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const p = delay(1000, { signal }) | ||
.catch(() => {}) | ||
.then(() => "Hello"); | ||
await assertRejects(async () => { | ||
await deadline(p, 100); | ||
}, DeadlineError); | ||
clearTimeout(t); | ||
controller.abort(); | ||
}); | ||
|
||
Deno.test("[async] deadline: thrown when promise is rejected", async () => { | ||
const p = deferred(); | ||
const t = setTimeout(() => p.reject(new Error("booom")), 100); | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const p = delay(100, { signal }) | ||
.catch(() => {}) | ||
.then(() => Promise.reject(new Error("booom"))); | ||
await assertRejects( | ||
async () => { | ||
await deadline(p, 1000); | ||
}, | ||
Error, | ||
"booom", | ||
); | ||
clearTimeout(t); | ||
controller.abort(); | ||
}); | ||
|
||
Deno.test("[async] deadline: with non-aborted signal", async () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const p = delay(100, { signal }) | ||
.catch(() => {}) | ||
.then(() => "Hello"); | ||
const abort = new AbortController(); | ||
const result = await deadline(p, 1000, { signal: abort.signal }); | ||
assertEquals(result, "Hello"); | ||
controller.abort(); | ||
}); | ||
|
||
Deno.test("[async] deadline: with signal aborted after delay", async () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const p = delay(100, { signal }) | ||
.catch(() => {}) | ||
.then(() => "Hello"); | ||
const abort = new AbortController(); | ||
const promise = deadline(p, 100, { signal: abort.signal }); | ||
abort.abort(); | ||
await assertRejects(async () => { | ||
await promise; | ||
}, DeadlineError); | ||
controller.abort(); | ||
}); | ||
|
||
Deno.test("[async] deadline: with already aborted signal", async () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const p = delay(100, { signal }) | ||
.catch(() => {}) | ||
.then(() => "Hello"); | ||
const abort = new AbortController(); | ||
abort.abort(); | ||
await assertRejects(async () => { | ||
await deadline(p, 100, { signal: abort.signal }); | ||
}, DeadlineError); | ||
controller.abort(); | ||
}); |