-
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.
- Loading branch information
Showing
6 changed files
with
110 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { actions, getWorker } from "./launchedWorker"; | ||
|
||
test("sync actinos", () => { | ||
expect(actions.ping()).toBe("pong!?"); | ||
getWorker()!.terminate(); | ||
}); |
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,5 @@ | ||
import { launchSyncWorker } from "accel-record-core/dist/synclib/index.js"; | ||
|
||
export const { actions, getWorker, stopWorker } = launchSyncWorker(import.meta.filename, { | ||
ping: () => "pong!?", | ||
}); |
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 @@ | ||
I could read a file. |
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,51 @@ | ||
import { open } from "node:fs/promises"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import actions from "./worker"; | ||
import fail from "./workerWithError"; | ||
|
||
const client = actions.launch(); | ||
|
||
test("sync actinos", async () => { | ||
expect(client.ping()).toBe("pong!?"); | ||
expect(client.incr(3)).toBe(4); | ||
expect(client.magic(0)).toBe(1); | ||
expect(client.magic(1)).toBe(3); | ||
expect(client.magic(2)).toBe(5); | ||
expect(client.errorSample).toThrowError("errorSample"); | ||
try { | ||
client.myErrorTest(); | ||
} catch (e) { | ||
expect(e).toMatchObject({ name: "MyError", message: "myErrorTest", prop1: "foo" }); | ||
} | ||
}); | ||
|
||
test("sync FileHandle read", async () => { | ||
const filepath = fileURLToPath(path.join(import.meta.url, "../sample.txt")); | ||
const fileHandle = await open(filepath, "r"); | ||
const arrayBuffer = client.readFile(fileHandle); | ||
const textDecoder = new TextDecoder("utf-8"); | ||
const text = textDecoder.decode(arrayBuffer); | ||
expect(text).toMatch("I could read a file."); | ||
}); | ||
|
||
test("sync File read", () => { | ||
const fileContent = new Uint8Array([72, 101, 108, 108, 111]); // Binary data for "Hello" | ||
const blob = new Blob([fileContent], { type: "text/plain" }); | ||
const file = new File([blob], "example.txt", { type: "text/plain" }); | ||
|
||
const arrayBuffer = client.file(file); | ||
const textDecoder = new TextDecoder("utf-8"); | ||
const text = textDecoder.decode(arrayBuffer); | ||
expect(text).toMatch("Hello"); | ||
}); | ||
|
||
test("sync actinos with error", async () => { | ||
fail.launch(); | ||
await new Promise<void>((resolve) => { | ||
fail.getWorker()!.on("error", (error) => { | ||
expect(error).toMatchObject({ message: "Sample error on launching worker." }); | ||
resolve(); | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import { defineSyncWorker } from "accel-record-core/dist/synclib/index.js"; | ||
import { type FileHandle } from "fs/promises"; | ||
|
||
export class MyError extends Error { | ||
name = "MyError"; | ||
constructor( | ||
message: string, | ||
public prop1: string | ||
) { | ||
super(message); | ||
} | ||
} | ||
|
||
let s = 0; | ||
export default defineSyncWorker(import.meta.filename, { | ||
incr: async (a: number) => a + 1, | ||
magic: (t: number) => ++s + t, | ||
ping: () => "pong!?", | ||
readFile: async (fileHandle: FileHandle) => { | ||
const stats = await fileHandle.stat(); | ||
const buffer = Buffer.alloc(stats.size); | ||
await fileHandle.read(buffer, 0, stats.size, 0); | ||
const arrayBuffer = buffer.buffer.slice( | ||
buffer.byteOffset, | ||
buffer.byteOffset + buffer.byteLength | ||
); | ||
return arrayBuffer; | ||
}, | ||
file: async (file: File) => { | ||
const arrayBuffer = await file.arrayBuffer(); | ||
return arrayBuffer; | ||
}, | ||
errorSample: () => { | ||
throw new Error("errorSample"); | ||
}, | ||
myErrorTest: () => { | ||
throw new MyError("myErrorTest", "foo"); | ||
}, | ||
}); |
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,8 @@ | ||
import { defineSyncWorker } from "accel-record-core/dist/synclib/index.js"; | ||
import { workerData } from "worker_threads"; | ||
|
||
if (workerData.sharedBuffer) { | ||
throw new Error("Sample error on launching worker."); | ||
} | ||
|
||
export default defineSyncWorker(import.meta.filename, {}); |