-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from jsr-core/cross-test
test: Test codes with Node/Bun
- Loading branch information
Showing
20 changed files
with
837 additions
and
702 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,2 +1,3 @@ | ||
/node_modules | ||
/.tools | ||
/.deno |
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 @@ | ||
@jsr:registry=https://npm.jsr.io |
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,17 @@ | ||
// Using `deadline` in `@std/[email protected]` cause the following error: | ||
// 'Promise resolution is still pending but the event loop has already resolved' | ||
// So we need to implement `deadline` by ourselves. | ||
export async function deadline<T>( | ||
promise: Promise<T>, | ||
timeout: number, | ||
): Promise<T> { | ||
const waiter = Promise.withResolvers<never>(); | ||
const timer = setTimeout( | ||
() => waiter.reject(new DOMException("Signal timed out.")), | ||
timeout, | ||
); | ||
return await Promise.race([ | ||
waiter.promise, | ||
promise.finally(() => clearTimeout(timer)), | ||
]); | ||
} |
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,14 +1,13 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals } from "@std/assert"; | ||
import { AsyncValue } from "./async_value.ts"; | ||
|
||
Deno.test("AsyncValue", async (t) => { | ||
await t.step( | ||
"'get' returns a promise that resolves to the value set by 'set'", | ||
async () => { | ||
const v = new AsyncValue(0); | ||
assertEquals(await v.get(), 0); | ||
await v.set(1); | ||
assertEquals(await v.get(), 1); | ||
}, | ||
); | ||
}); | ||
test( | ||
"AsyncValue 'get' returns a promise that resolves to the value set by 'set'", | ||
async () => { | ||
const v = new AsyncValue(0); | ||
assertEquals(await v.get(), 0); | ||
await v.set(1); | ||
assertEquals(await v.get(), 1); | ||
}, | ||
); |
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,94 +1,94 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals, assertRejects, assertThrows } from "@std/assert"; | ||
import { deadline, delay } from "@std/async"; | ||
import { delay } from "@std/async"; | ||
import { Barrier } from "./barrier.ts"; | ||
import { deadline } from "./_testutil.ts"; | ||
|
||
Deno.test("Barrier", async (t) => { | ||
await t.step( | ||
"'wait' waits until the number of waiters reached the size specified to the barrier", | ||
async () => { | ||
const barrier = new Barrier(5); | ||
const workers = []; | ||
const results: string[] = []; | ||
for (let i = 0; i < 5; i++) { | ||
workers.push((async () => { | ||
results.push(`before wait ${i}`); | ||
await barrier.wait(); | ||
results.push(`after wait ${i}`); | ||
})()); | ||
} | ||
await Promise.all(workers); | ||
assertEquals(results, [ | ||
"before wait 0", | ||
"before wait 1", | ||
"before wait 2", | ||
"before wait 3", | ||
"before wait 4", | ||
"after wait 0", | ||
"after wait 1", | ||
"after wait 2", | ||
"after wait 3", | ||
"after wait 4", | ||
]); | ||
}, | ||
); | ||
test( | ||
"Barrier 'wait' waits until the number of waiters reached the size specified to the barrier", | ||
async () => { | ||
const barrier = new Barrier(5); | ||
const workers = []; | ||
const results: string[] = []; | ||
for (let i = 0; i < 5; i++) { | ||
workers.push((async () => { | ||
results.push(`before wait ${i}`); | ||
await barrier.wait(); | ||
results.push(`after wait ${i}`); | ||
})()); | ||
} | ||
await Promise.all(workers); | ||
assertEquals(results, [ | ||
"before wait 0", | ||
"before wait 1", | ||
"before wait 2", | ||
"before wait 3", | ||
"before wait 4", | ||
"after wait 0", | ||
"after wait 1", | ||
"after wait 2", | ||
"after wait 3", | ||
"after wait 4", | ||
]); | ||
}, | ||
); | ||
|
||
await t.step( | ||
"'wait' with non-aborted signal", | ||
async () => { | ||
const controller = new AbortController(); | ||
const barrier = new Barrier(2); | ||
test( | ||
"Barrier 'wait' with non-aborted signal", | ||
async () => { | ||
const controller = new AbortController(); | ||
const barrier = new Barrier(2); | ||
|
||
await assertRejects( | ||
() => deadline(barrier.wait({ signal: controller.signal }), 100), | ||
DOMException, | ||
"Signal timed out.", | ||
); | ||
}, | ||
); | ||
await assertRejects( | ||
() => deadline(barrier.wait({ signal: controller.signal }), 100), | ||
DOMException, | ||
"Signal timed out.", | ||
); | ||
}, | ||
); | ||
|
||
await t.step( | ||
"'wait' with signal aborted after delay", | ||
async () => { | ||
const controller = new AbortController(); | ||
const barrier = new Barrier(2); | ||
const reason = new Error("Aborted"); | ||
test( | ||
"Barrier 'wait' with signal aborted after delay", | ||
async () => { | ||
const controller = new AbortController(); | ||
const barrier = new Barrier(2); | ||
const reason = new Error("Aborted"); | ||
|
||
delay(50).then(() => controller.abort(reason)); | ||
delay(50).then(() => controller.abort(reason)); | ||
|
||
await assertRejects( | ||
() => deadline(barrier.wait({ signal: controller.signal }), 100), | ||
Error, | ||
"Aborted", | ||
); | ||
}, | ||
); | ||
await assertRejects( | ||
() => deadline(barrier.wait({ signal: controller.signal }), 100), | ||
Error, | ||
"Aborted", | ||
); | ||
}, | ||
); | ||
|
||
await t.step( | ||
"'wait' with already aborted signal", | ||
async () => { | ||
const controller = new AbortController(); | ||
const barrier = new Barrier(2); | ||
const reason = new Error("Aborted"); | ||
test( | ||
"Barrier 'wait' with already aborted signal", | ||
async () => { | ||
const controller = new AbortController(); | ||
const barrier = new Barrier(2); | ||
const reason = new Error("Aborted"); | ||
|
||
controller.abort(reason); | ||
controller.abort(reason); | ||
|
||
await assertRejects( | ||
() => deadline(barrier.wait({ signal: controller.signal }), 100), | ||
Error, | ||
"Aborted", | ||
); | ||
}, | ||
); | ||
await assertRejects( | ||
() => deadline(barrier.wait({ signal: controller.signal }), 100), | ||
Error, | ||
"Aborted", | ||
); | ||
}, | ||
); | ||
|
||
await t.step( | ||
"throws RangeError if size is not a positive safe integer", | ||
() => { | ||
assertThrows(() => new Barrier(NaN), RangeError); | ||
assertThrows(() => new Barrier(Infinity), RangeError); | ||
assertThrows(() => new Barrier(-Infinity), RangeError); | ||
assertThrows(() => new Barrier(-1), RangeError); | ||
assertThrows(() => new Barrier(1.1), RangeError); | ||
assertThrows(() => new Barrier(0), RangeError); | ||
}, | ||
); | ||
}); | ||
test( | ||
"Barrier throws RangeError if size is not a positive safe integer", | ||
() => { | ||
assertThrows(() => new Barrier(NaN), RangeError); | ||
assertThrows(() => new Barrier(Infinity), RangeError); | ||
assertThrows(() => new Barrier(-Infinity), RangeError); | ||
assertThrows(() => new Barrier(-1), RangeError); | ||
assertThrows(() => new Barrier(1.1), RangeError); | ||
assertThrows(() => new Barrier(0), RangeError); | ||
}, | ||
); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.