forked from duart38/Thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
93 lines (85 loc) · 2.37 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
import {
assertEquals,
assertThrows,
fail,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import Thread from "./Thread.ts";
import { returnNumber } from "./test_import.js";
Deno.test("incorrect file extension throws error", (): void => {
assertThrows(() => {
let tr = new Thread(
(e) => {
return 1;
},
"module",
["import Observe from 'https://raw.githubusercontent.com/duart38/Observe/master/Observe.ts'"],
);
});
});
Deno.test("Worker takes in external function", async () => {
let run = new Promise((resolve) => {
function testfunc() {
return 1;
}
let t = new Thread(testfunc, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(2);
});
assertEquals(await run, 1);
});
Deno.test("Command/Method chaining works", async () => {
let run = new Promise((resolve) => {
let t = new Thread((e) => 0, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(0);
});
assertEquals(await run, 0);
});
Deno.test("Worker returns message", async () => {
let run = new Promise((resolve) => {
let 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 () => {
let run = new Promise((resolve) => {
let t = new Thread((e) => 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 new file imports work", async () => {
let run = new Promise((resolve) => {
let t = new Thread((e) => 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 () => {
let run = new Promise<{} | undefined>((resolve) => {
let t = new Thread((e, glob) => glob, "module");
t.onMessage((n) => {
t.remove()?.then(() => resolve(n));
});
t.postMessage(0);
});
assertEquals(await run, {});
});