Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mys1024 committed Mar 30, 2024
1 parent 0048923 commit e78fe3e
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
deno-version:
- v1.41.0
- v1.x
os:
- ubuntu-latest
- windows-latest
Expand Down
84 changes: 1 addition & 83 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { assertEquals } from "@std/assert";
import { useWorkerFn } from "../src/main.ts";
import type {
Add,
AddBytesWithoutTransferring,
AddBytesWithTransferring,
Fib,
Redefine,
ThrowErr,
} from "./basic.test.worker.ts";
import type { Add, Fib, Redefine, ThrowErr } from "./basic.test.worker.ts";
import type { Add as Add2 } from "./basic.test.worker.another.ts";

Deno.test({
Expand Down Expand Up @@ -45,29 +38,6 @@ Deno.test({
);
});

await t.step("multiple proxy functions", async () => {
const worker = new Worker(
new URL("./basic.test.worker.ts", import.meta.url),
{
type: "module",
},
);

const add1 = useWorkerFn<Add>("add", worker);
const add2 = useWorkerFn<Add>("add", worker);
const add3 = useWorkerFn<Add>("add", worker);
const results = Promise.all([add1(100, 200), add2(50, 50), add3(2, 8)]);
const timeout = new Promise((_, reject) => {
setTimeout(() => {
reject("timeout!");
}, 500);
});
assertEquals(
await Promise.race([results, timeout]),
[300, 100, 10],
);
});

await t.step("err passthrough", async () => {
const throwErr = useWorkerFn<ThrowErr>(
"throwErr",
Expand Down Expand Up @@ -146,58 +116,6 @@ Deno.test({
);
}
});

await t.step("transferring enabled", async () => {
const worker = new Worker(
new URL("./basic.test.worker.ts", import.meta.url),
{
type: "module",
},
);
const addBytesWithTransferring = useWorkerFn<AddBytesWithTransferring>(
"addBytesWithTransferring",
worker,
{
transfer: true,
},
);
const bytes1 = new Uint8Array([1, 2, 3]).buffer;
const bytes2 = new Uint8Array([3, 2, 1]).buffer;
assertEquals(
Array.from(
new Uint8Array(await addBytesWithTransferring(bytes1, bytes2, 3)),
),
[4, 4, 4],
);
assertEquals(bytes1.byteLength, 0);
assertEquals(bytes2.byteLength, 0);
});

await t.step("transferring disabled", async () => {
const worker = new Worker(
new URL("./basic.test.worker.ts", import.meta.url),
{
type: "module",
},
);
const addBytesWithoutTransferring = useWorkerFn<
AddBytesWithoutTransferring
>("addBytesWithoutTransferring", worker, {
transfer: false,
});
const bytes1 = new Uint8Array([1, 2, 3]).buffer;
const bytes2 = new Uint8Array([3, 2, 1]).buffer;
assertEquals(
Array.from(
new Uint8Array(
await addBytesWithoutTransferring(bytes1, bytes2, 3),
),
),
[4, 4, 4],
);
assertEquals(bytes1.byteLength, 3);
assertEquals(bytes2.byteLength, 3);
});
});
},
});
48 changes: 0 additions & 48 deletions test/basic.test.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,12 @@ function redefine() {
defineWorkerFn("redefine", redefine);
}

function addBytesWithTransferring(
buffer1: ArrayBuffer,
buffer2: ArrayBuffer,
length: number,
) {
const bytes1 = new Uint8Array(buffer1);
const bytes2 = new Uint8Array(buffer2);
for (let i = 0; i < length; i++) {
bytes1[i] += bytes2[i];
}
return new Promise<ArrayBuffer>((resolve) => {
resolve(bytes1.buffer);
setTimeout(() => {
if (bytes1.buffer.byteLength !== 0) {
throw new Error("The return value should be transferred.");
}
});
});
}

function addBytesWithoutTransferring(
buffer1: ArrayBuffer,
buffer2: ArrayBuffer,
length: number,
) {
const bytes1 = new Uint8Array(buffer1);
const bytes2 = new Uint8Array(buffer2);
for (let i = 0; i < length; i++) {
bytes1[i] += bytes2[i];
}
return new Promise<ArrayBuffer>((resolve) => {
resolve(bytes1.buffer);
setTimeout(() => {
if (bytes1.buffer.byteLength === 0) {
throw new Error("The return value should not be transferred.");
}
});
});
}

defineWorkerFn("add", add);
defineWorkerFn("fib", fib);
defineWorkerFn("throwErr", throwErr);
defineWorkerFn("redefine", redefine);
defineWorkerFn("addBytesWithTransferring", addBytesWithTransferring, {
transfer: true,
});
defineWorkerFn("addBytesWithoutTransferring", addBytesWithoutTransferring, {
transfer: false,
});

export type Add = typeof add;
export type Fib = typeof fib;
export type ThrowErr = typeof throwErr;
export type Redefine = typeof redefine;
export type AddBytesWithTransferring = typeof addBytesWithTransferring;
export type AddBytesWithoutTransferring = typeof addBytesWithoutTransferring;

0 comments on commit e78fe3e

Please sign in to comment.