deno.land / std@0.166.0 / node / worker_threads_test.ts

worker_threads_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertObjectMatch } from "../testing/asserts.ts";import { fromFileUrl, relative } from "../path/mod.ts";import { EventEmitter, once } from "./events.ts";import * as workerThreads from "./worker_threads.ts";
Deno.test({ name: "[worker_threads] isMainThread", fn() { assertEquals(workerThreads.isMainThread, true); },});
Deno.test({ name: "[worker_threads] threadId", fn() { assertEquals(workerThreads.threadId, 0); },});
Deno.test({ name: "[worker_threads] resourceLimits", fn() { assertObjectMatch(workerThreads.resourceLimits, {}); },});
Deno.test({ name: "[worker_threads] parentPort", fn() { assertEquals(workerThreads.parentPort, null); },});
Deno.test({ name: "[worker_threads] workerData", fn() { assertEquals(workerThreads.workerData, null); },});
Deno.test({ name: "[worker_threads] setEnvironmentData / getEnvironmentData", fn() { workerThreads.setEnvironmentData("test", "test"); assertEquals(workerThreads.getEnvironmentData("test"), "test"); // delete workerThreads.setEnvironmentData("test"); assertEquals(workerThreads.getEnvironmentData("test"), undefined); },});
Deno.test({ name: "[worker_threads] Worker threadId", async fn() { const worker = new workerThreads.Worker( new URL("./testdata/worker_threads.ts", import.meta.url), ); worker.postMessage("Hello, how are you my thread?"); await once(worker, "message"); assertEquals((await once(worker, "message"))[0].threadId, 1); worker.terminate();
const worker1 = new workerThreads.Worker( new URL("./testdata/worker_threads.ts", import.meta.url), ); worker1.postMessage("Hello, how are you my thread?"); await once(worker1, "message"); assertEquals((await once(worker1, "message"))[0].threadId, 2); worker1.terminate(); },});
Deno.test({ name: "[worker_threads] Worker basics", async fn() { workerThreads.setEnvironmentData("test", "test"); workerThreads.setEnvironmentData(1, { test: "random", random: "test", }); const { port1 } = new MessageChannel(); const worker = new workerThreads.Worker( new URL("./testdata/worker_threads.ts", import.meta.url), { workerData: ["hey", true, false, 2, port1], transferList: [port1], }, ); worker.postMessage("Hello, how are you my thread?"); assertEquals((await once(worker, "message"))[0], "I'm fine!"); const data = (await once(worker, "message"))[0]; // data.threadId can be 1 when this test is runned individually if (data.threadId === 1) data.threadId = 3; assertObjectMatch(data, { isMainThread: false, threadId: 3, workerData: ["hey", true, false, 2], envData: ["test", { test: "random", random: "test" }], }); worker.terminate(); }, sanitizeResources: false,});
const workerThreadsURL = JSON.stringify( new URL("./worker_threads.ts", import.meta.url).toString(),);
Deno.test({ name: "[worker_threads] Worker eval", async fn() { const worker = new workerThreads.Worker( ` import { parentPort } from ${workerThreadsURL}; parentPort.postMessage("It works!"); `, { eval: true, }, ); assertEquals((await once(worker, "message"))[0], "It works!"); worker.terminate(); },});
Deno.test({ name: "[worker_threads] inheritences", async fn() { const eventsURL = JSON.stringify( new URL("./events.ts", import.meta.url).toString(), );
const worker = new workerThreads.Worker( ` import { EventEmitter } from ${eventsURL}; import { parentPort } from ${workerThreadsURL}; parentPort.postMessage(parentPort instanceof EventTarget); parentPort.postMessage(parentPort instanceof EventEmitter); `, { eval: true, }, ); assertEquals((await once(worker, "message"))[0], true); assertEquals((await once(worker, "message"))[0], false); assert(worker instanceof EventEmitter); assert(!(worker instanceof EventTarget)); worker.terminate(); },});
Deno.test({ name: "[worker_threads] Worker workerData", async fn() { const worker = new workerThreads.Worker( new URL("./testdata/worker_threads.ts", import.meta.url), { workerData: null, }, ); worker.postMessage("Hello, how are you my thread?"); await once(worker, "message"); assertEquals((await once(worker, "message"))[0].workerData, null); worker.terminate();
const worker1 = new workerThreads.Worker( new URL("./testdata/worker_threads.ts", import.meta.url), ); worker1.postMessage("Hello, how are you my thread?"); await once(worker1, "message"); assertEquals((await once(worker1, "message"))[0].workerData, undefined); worker1.terminate(); },});
Deno.test({ name: "[worker_threads] Worker with relative path", async fn() { const worker = new workerThreads.Worker(relative( Deno.cwd(), fromFileUrl(new URL("./testdata/worker_threads.ts", import.meta.url)), )); worker.postMessage("Hello, how are you my thread?"); assertEquals((await once(worker, "message"))[0], "I'm fine!"); worker.terminate(); },});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago