worker-testbed 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Petr Tripolsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # workertest
2
+
3
+ > The AI-ready testbed which use Worker Threads to create isolated vm context
4
+
5
+ A worker-threads based test framework using `Node.js`, `tape`, and `functools-kit`, enabling parallel execution of isolated test cases. The main thread registers and spawns worker threads for each test, while the workers retrieve and execute tests, reporting results back via parentPort. This approach enhances test isolation, concurrency, and performance.
6
+
7
+ ## Usage
8
+
9
+ ```tsx
10
+ import { run, test } from 'workertest';
11
+
12
+ test("Will pass after three seconds", (t) => {
13
+ globalThis.test = {};
14
+ globalThis.test.foo = "bar";
15
+ setTimeout(() => {
16
+ t.pass(globalThis.test.foo);
17
+ }, 3_000);
18
+ })
19
+
20
+ test("Will fail after a second cause globalThis.test is undefined", (t) => {
21
+ setTimeout(() => {
22
+ t.pass(globalThis.test.foo);
23
+ }, 1_000);
24
+ })
25
+
26
+ run(import.meta.url)
27
+
28
+ ```
package/build/.gitkeep ADDED
File without changes
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ var worker_threads = require('worker_threads');
4
+ var url = require('url');
5
+ var functoolsKit = require('functools-kit');
6
+ var tape = require('tape');
7
+
8
+ const workerFileSubject = new functoolsKit.BehaviorSubject();
9
+ let testRegistry = new functoolsKit.ToolRegistry("workertest");
10
+ const waitForFile = async () => {
11
+ if (workerFileSubject.data) {
12
+ return workerFileSubject.data;
13
+ }
14
+ return await workerFileSubject.toPromise();
15
+ };
16
+ class TestWrapper {
17
+ testName;
18
+ cb;
19
+ constructor(testName, cb) {
20
+ this.testName = testName;
21
+ this.cb = cb;
22
+ }
23
+ }
24
+ const test = (testName, cb) => {
25
+ if (worker_threads.isMainThread) {
26
+ tape(testName, async (test) => {
27
+ const [awaiter, { resolve }] = functoolsKit.createAwaiter();
28
+ const workerFile = await waitForFile();
29
+ const worker = new worker_threads.Worker(workerFile, {
30
+ workerData: { testName },
31
+ });
32
+ worker.once("message", ({ status, msg }) => {
33
+ if (status === "pass") {
34
+ test.pass(msg);
35
+ }
36
+ else if (status === "fail") {
37
+ test.fail(msg);
38
+ }
39
+ resolve();
40
+ });
41
+ worker.on("error", (err) => {
42
+ test.fail(`Worker error: ${err.message}`);
43
+ resolve();
44
+ });
45
+ worker.on("exit", (code) => {
46
+ if (code !== 0) {
47
+ test.fail(`Worker stopped with exit code ${code}`);
48
+ resolve();
49
+ }
50
+ });
51
+ return await awaiter;
52
+ });
53
+ }
54
+ testRegistry = testRegistry.register(testName, new TestWrapper(testName, cb));
55
+ };
56
+ const run = async (__filename) => {
57
+ if (worker_threads.isMainThread) {
58
+ workerFileSubject.next(url.fileURLToPath(__filename));
59
+ return;
60
+ }
61
+ if (!worker_threads.parentPort) {
62
+ throw new Error("workertest parentPort is null");
63
+ }
64
+ testRegistry.get(worker_threads.workerData.testName).cb({
65
+ pass: (msg) => worker_threads.parentPort.postMessage({ status: "pass", msg }),
66
+ fail: (msg) => worker_threads.parentPort.postMessage({ status: "fail", msg }),
67
+ });
68
+ };
69
+
70
+ exports.run = run;
71
+ exports.test = test;
@@ -0,0 +1,68 @@
1
+ import { isMainThread, Worker, parentPort, workerData } from 'worker_threads';
2
+ import { fileURLToPath } from 'url';
3
+ import { BehaviorSubject, ToolRegistry, createAwaiter } from 'functools-kit';
4
+ import tape from 'tape';
5
+
6
+ const workerFileSubject = new BehaviorSubject();
7
+ let testRegistry = new ToolRegistry("workertest");
8
+ const waitForFile = async () => {
9
+ if (workerFileSubject.data) {
10
+ return workerFileSubject.data;
11
+ }
12
+ return await workerFileSubject.toPromise();
13
+ };
14
+ class TestWrapper {
15
+ testName;
16
+ cb;
17
+ constructor(testName, cb) {
18
+ this.testName = testName;
19
+ this.cb = cb;
20
+ }
21
+ }
22
+ const test = (testName, cb) => {
23
+ if (isMainThread) {
24
+ tape(testName, async (test) => {
25
+ const [awaiter, { resolve }] = createAwaiter();
26
+ const workerFile = await waitForFile();
27
+ const worker = new Worker(workerFile, {
28
+ workerData: { testName },
29
+ });
30
+ worker.once("message", ({ status, msg }) => {
31
+ if (status === "pass") {
32
+ test.pass(msg);
33
+ }
34
+ else if (status === "fail") {
35
+ test.fail(msg);
36
+ }
37
+ resolve();
38
+ });
39
+ worker.on("error", (err) => {
40
+ test.fail(`Worker error: ${err.message}`);
41
+ resolve();
42
+ });
43
+ worker.on("exit", (code) => {
44
+ if (code !== 0) {
45
+ test.fail(`Worker stopped with exit code ${code}`);
46
+ resolve();
47
+ }
48
+ });
49
+ return await awaiter;
50
+ });
51
+ }
52
+ testRegistry = testRegistry.register(testName, new TestWrapper(testName, cb));
53
+ };
54
+ const run = async (__filename) => {
55
+ if (isMainThread) {
56
+ workerFileSubject.next(fileURLToPath(__filename));
57
+ return;
58
+ }
59
+ if (!parentPort) {
60
+ throw new Error("workertest parentPort is null");
61
+ }
62
+ testRegistry.get(workerData.testName).cb({
63
+ pass: (msg) => parentPort.postMessage({ status: "pass", msg }),
64
+ fail: (msg) => parentPort.postMessage({ status: "fail", msg }),
65
+ });
66
+ };
67
+
68
+ export { run, test };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "worker-testbed",
3
+ "version": "1.0.0",
4
+ "description": "The AI-ready testbed which use Worker Threads to create isolated vm context",
5
+ "author": {
6
+ "name": "Petr Tripolsky",
7
+ "email": "tripolskypetr@gmail.com",
8
+ "url": "https://github.com/tripolskypetr"
9
+ },
10
+ "funding": {
11
+ "type": "individual",
12
+ "url": "http://paypal.me/tripolskypetr"
13
+ },
14
+ "license": "MIT",
15
+ "homepage": "https://react-declarative-playground.github.io",
16
+ "keywords": [
17
+ "react-declarative",
18
+ "dependency-injection",
19
+ "nodejs",
20
+ "transient",
21
+ "stateless",
22
+ "asp.net core"
23
+ ],
24
+ "files": [
25
+ "build",
26
+ "types.d.ts",
27
+ "README.md"
28
+ ],
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/react-declarative/react-declarative",
32
+ "documentation": "https://github.com/react-declarative/react-declarative/tree/master/docs"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/react-declarative/react-declarative/issues"
36
+ },
37
+ "scripts": {
38
+ "build": "rollup -c",
39
+ "test": "npm run build && node ./test/test.mjs"
40
+ },
41
+ "main": "build/index.cjs",
42
+ "module": "build/index.mjs",
43
+ "source": "src/index.ts",
44
+ "types": "./types.d.ts",
45
+ "exports": {
46
+ "require": "./build/index.cjs",
47
+ "types": "./types.d.ts",
48
+ "import": "./build/index.mjs",
49
+ "default": "./build/index.cjs"
50
+ },
51
+ "devDependencies": {
52
+ "@rollup/plugin-typescript": "11.1.6",
53
+ "@types/node": "22.9.0",
54
+ "@types/tape": "^5.8.1",
55
+ "rollup": "3.29.4",
56
+ "rollup-plugin-dts": "6.1.1",
57
+ "rollup-plugin-peer-deps-external": "2.2.4",
58
+ "tslib": "2.7.0"
59
+ },
60
+ "peerDependencies": {
61
+ "typescript": "^5.0.0"
62
+ },
63
+ "dependencies": {
64
+ "functools-kit": "^1.0.61",
65
+ "tape": "^5.9.0"
66
+ }
67
+ }
package/types.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ interface ITest {
2
+ pass(msg?: string): void;
3
+ fail(msg?: string): void;
4
+ }
5
+ declare const test: (testName: string, cb: (t: ITest) => void) => void;
6
+ declare const run: (__filename: string) => Promise<void>;
7
+
8
+ export { run, test };