-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.ts
111 lines (103 loc) · 2.97 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import Thread from "./Thread.ts";
import { returnNumber } from "./test_import.js";
import { plus } from "https://deno.land/x/[email protected]/mod.ts";
Deno.test("Importing ts file over network", async () => {
const run = new Promise((resolve) => {
const t = new Thread(
(e) => {
return plus(e.data as number, 1);
},
"module",
[
'import { plus } from "https://deno.land/x/[email protected]/mod.ts"',
],
);
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(1);
});
assertEquals(await run, "2");
});
Deno.test("Worker takes in external function", async () => {
const run = new Promise((resolve) => {
function testfunc() {
return 1;
}
const t = new Thread(testfunc, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(2);
});
assertEquals(await run, 1);
});
Deno.test("Worker async function supported", async () => {
const run = new Promise((resolve) => {
const t = new Thread(async () => {
await new Promise((ir) => setTimeout(ir, 1000));
return 1;
}, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(2);
});
assertEquals(await run, 1);
});
Deno.test("Command/Method chaining works", async () => {
const run = new Promise((resolve) => {
const t = new Thread((_) => 0, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(0);
});
assertEquals(await run, 0);
});
Deno.test("Worker returns message", async () => {
const run = new Promise((resolve) => {
const t = new Thread((e) => e.data, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(2);
});
assertEquals(await run, 2);
});
Deno.test("Local file imports work", async () => {
const run = new Promise((resolve) => {
const t = new Thread((_) => returnNumber(), "module", [
'import {returnNumber} from "./test_import.js"',
]);
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(2);
});
assertEquals(await run, 1);
});
Deno.test("Over the network file imports work", async () => {
const run = new Promise((resolve) => {
const t = new Thread((_) => returnNumber(), "module", [
'import { returnNumber } from "https://raw.githubusercontent.com/duart38/Thread/master/test_import.js"',
]);
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(1);
});
assertEquals(await run, 1);
});
Deno.test("Worker has global object", async () => {
// deno-lint-ignore ban-types
const run = new Promise<{} | undefined>((resolve) => {
const t = new Thread((_, glob) => glob, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(0);
});
assertEquals(await run, {});
});