worker-testbed 1.0.1 → 1.0.3
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/build/index.cjs +41 -31
- package/build/index.mjs +42 -32
- package/package.json +1 -1
- package/types.d.ts +2 -2
package/build/index.cjs
CHANGED
|
@@ -6,7 +6,9 @@ var functoolsKit = require('functools-kit');
|
|
|
6
6
|
var tape = require('tape');
|
|
7
7
|
|
|
8
8
|
const workerFileSubject = new functoolsKit.BehaviorSubject();
|
|
9
|
+
const finishSubject = new functoolsKit.Subject();
|
|
9
10
|
let testRegistry = new functoolsKit.ToolRegistry("workertest");
|
|
11
|
+
let testCounter = 0;
|
|
10
12
|
const waitForFile = async () => {
|
|
11
13
|
if (workerFileSubject.data) {
|
|
12
14
|
return workerFileSubject.data;
|
|
@@ -21,41 +23,49 @@ class TestWrapper {
|
|
|
21
23
|
this.cb = cb;
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
|
-
const test = (testName, cb) => {
|
|
25
|
-
if (worker_threads.isMainThread) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
26
|
+
const test = async (testName, cb) => {
|
|
27
|
+
if (!worker_threads.isMainThread) {
|
|
28
|
+
testRegistry = testRegistry.register(testName, new TestWrapper(testName, cb));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const workerFile = await waitForFile();
|
|
32
|
+
tape(testName, async (test) => {
|
|
33
|
+
const [awaiter, { resolve }] = functoolsKit.createAwaiter();
|
|
34
|
+
testCounter += 1;
|
|
35
|
+
const worker = new worker_threads.Worker(workerFile, {
|
|
36
|
+
workerData: { testName },
|
|
37
|
+
});
|
|
38
|
+
worker.once("message", ({ status, msg }) => {
|
|
39
|
+
if (status === "pass") {
|
|
40
|
+
test.pass(msg);
|
|
41
|
+
}
|
|
42
|
+
else if (status === "fail") {
|
|
43
|
+
test.fail(msg);
|
|
44
|
+
}
|
|
45
|
+
resolve();
|
|
46
|
+
});
|
|
47
|
+
worker.on("error", (err) => {
|
|
48
|
+
test.fail(`Worker error: ${err.message}`);
|
|
49
|
+
resolve();
|
|
50
|
+
});
|
|
51
|
+
worker.on("exit", (code) => {
|
|
52
|
+
if (code !== 0) {
|
|
53
|
+
test.fail(`Worker stopped with exit code ${code}`);
|
|
43
54
|
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;
|
|
55
|
+
}
|
|
52
56
|
});
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
{
|
|
58
|
+
await awaiter;
|
|
59
|
+
testCounter -= 1;
|
|
60
|
+
await finishSubject.next();
|
|
61
|
+
}
|
|
62
|
+
});
|
|
55
63
|
};
|
|
56
|
-
const run = async (__filename) => {
|
|
64
|
+
const run = async (__filename, cb) => {
|
|
57
65
|
if (worker_threads.isMainThread) {
|
|
58
|
-
workerFileSubject.next(url.fileURLToPath(__filename));
|
|
66
|
+
await workerFileSubject.next(url.fileURLToPath(__filename));
|
|
67
|
+
await finishSubject.filter(() => testCounter === 0).toPromise();
|
|
68
|
+
cb();
|
|
59
69
|
return;
|
|
60
70
|
}
|
|
61
71
|
if (!worker_threads.parentPort) {
|
package/build/index.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { isMainThread, Worker, parentPort, workerData } from 'worker_threads';
|
|
2
2
|
import { fileURLToPath } from 'url';
|
|
3
|
-
import { BehaviorSubject, ToolRegistry, createAwaiter } from 'functools-kit';
|
|
3
|
+
import { BehaviorSubject, Subject, ToolRegistry, createAwaiter } from 'functools-kit';
|
|
4
4
|
import tape from 'tape';
|
|
5
5
|
|
|
6
6
|
const workerFileSubject = new BehaviorSubject();
|
|
7
|
+
const finishSubject = new Subject();
|
|
7
8
|
let testRegistry = new ToolRegistry("workertest");
|
|
9
|
+
let testCounter = 0;
|
|
8
10
|
const waitForFile = async () => {
|
|
9
11
|
if (workerFileSubject.data) {
|
|
10
12
|
return workerFileSubject.data;
|
|
@@ -19,41 +21,49 @@ class TestWrapper {
|
|
|
19
21
|
this.cb = cb;
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
|
-
const test = (testName, cb) => {
|
|
23
|
-
if (isMainThread) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
24
|
+
const test = async (testName, cb) => {
|
|
25
|
+
if (!isMainThread) {
|
|
26
|
+
testRegistry = testRegistry.register(testName, new TestWrapper(testName, cb));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const workerFile = await waitForFile();
|
|
30
|
+
tape(testName, async (test) => {
|
|
31
|
+
const [awaiter, { resolve }] = createAwaiter();
|
|
32
|
+
testCounter += 1;
|
|
33
|
+
const worker = new Worker(workerFile, {
|
|
34
|
+
workerData: { testName },
|
|
35
|
+
});
|
|
36
|
+
worker.once("message", ({ status, msg }) => {
|
|
37
|
+
if (status === "pass") {
|
|
38
|
+
test.pass(msg);
|
|
39
|
+
}
|
|
40
|
+
else if (status === "fail") {
|
|
41
|
+
test.fail(msg);
|
|
42
|
+
}
|
|
43
|
+
resolve();
|
|
44
|
+
});
|
|
45
|
+
worker.on("error", (err) => {
|
|
46
|
+
test.fail(`Worker error: ${err.message}`);
|
|
47
|
+
resolve();
|
|
48
|
+
});
|
|
49
|
+
worker.on("exit", (code) => {
|
|
50
|
+
if (code !== 0) {
|
|
51
|
+
test.fail(`Worker stopped with exit code ${code}`);
|
|
41
52
|
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;
|
|
53
|
+
}
|
|
50
54
|
});
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
{
|
|
56
|
+
await awaiter;
|
|
57
|
+
testCounter -= 1;
|
|
58
|
+
await finishSubject.next();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
53
61
|
};
|
|
54
|
-
const run = async (__filename) => {
|
|
62
|
+
const run = async (__filename, cb) => {
|
|
55
63
|
if (isMainThread) {
|
|
56
|
-
workerFileSubject.next(fileURLToPath(__filename));
|
|
64
|
+
await workerFileSubject.next(fileURLToPath(__filename));
|
|
65
|
+
await finishSubject.filter(() => testCounter === 0).toPromise();
|
|
66
|
+
cb();
|
|
57
67
|
return;
|
|
58
68
|
}
|
|
59
69
|
if (!parentPort) {
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ interface ITest {
|
|
|
2
2
|
pass(msg?: string): void;
|
|
3
3
|
fail(msg?: string): void;
|
|
4
4
|
}
|
|
5
|
-
declare const test: (testName: string, cb: (t: ITest) => void) => void
|
|
6
|
-
declare const run: (__filename: string) => Promise<void>;
|
|
5
|
+
declare const test: (testName: string, cb: (t: ITest) => void) => Promise<void>;
|
|
6
|
+
declare const run: (__filename: string, cb: () => void) => Promise<void>;
|
|
7
7
|
|
|
8
8
|
export { run, test };
|