source: trip-planner-front/node_modules/piscina/test/post-task.ts@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1import { MessageChannel } from 'worker_threads';
2import Piscina from '..';
3import { test } from 'tap';
4import { resolve } from 'path';
5
6test('postTask() can transfer ArrayBuffer instances', async ({ equal }) => {
7 const pool = new Piscina({
8 filename: resolve(__dirname, 'fixtures/simple-isworkerthread.ts')
9 });
10
11 const ab = new ArrayBuffer(40);
12 await pool.runTask({ ab }, [ab]);
13 equal(pool.completed, 1);
14 equal(ab.byteLength, 0);
15});
16
17test('postTask() can transfer ArrayBuffer instances', async ({ equal }) => {
18 const pool = new Piscina({
19 filename: resolve(__dirname, 'fixtures/simple-isworkerthread.ts')
20 });
21
22 const ab = new ArrayBuffer(40);
23 await pool.run({ ab }, { transferList: [ab] });
24 equal(pool.completed, 1);
25 equal(ab.byteLength, 0);
26});
27
28test('postTask() cannot clone build-in objects', async ({ rejects }) => {
29 const pool = new Piscina({
30 filename: resolve(__dirname, 'fixtures/simple-isworkerthread.ts')
31 });
32
33 const obj = new MessageChannel().port1;
34 rejects(pool.runTask({ obj }));
35});
36
37test('postTask() resolves with a rejection when the handler rejects', async ({ rejects }) => {
38 const pool = new Piscina({
39 filename: resolve(__dirname, 'fixtures/eval.js')
40 });
41
42 rejects(pool.runTask('Promise.reject(new Error("foo"))'), /foo/);
43});
44
45test('postTask() resolves with a rejection when the handler throws', async ({ rejects }) => {
46 const pool = new Piscina({
47 filename: resolve(__dirname, 'fixtures/eval.js')
48 });
49
50 rejects(pool.runTask('throw new Error("foo")'), /foo/);
51});
52
53test('postTask() validates transferList', async ({ rejects }) => {
54 const pool = new Piscina({
55 filename: resolve(__dirname, 'fixtures/eval.js')
56 });
57
58 rejects(pool.runTask('0', 42 as any),
59 /transferList argument must be an Array/);
60
61 rejects(pool.run('0', { transferList: 42 as any }),
62 /transferList argument must be an Array/);
63});
64
65test('postTask() validates filename', async ({ rejects }) => {
66 const pool = new Piscina({
67 filename: resolve(__dirname, 'fixtures/eval.js')
68 });
69
70 rejects(pool.runTask('0', [], 42 as any),
71 /filename argument must be a string/);
72
73 rejects(pool.run('0', { filename: 42 as any }),
74 /filename argument must be a string/);
75});
76
77test('postTask() validates name', async ({ rejects }) => {
78 const pool = new Piscina({
79 filename: resolve(__dirname, 'fixtures/eval.js')
80 });
81
82 rejects(pool.run('0', { name: 42 as any }),
83 /name argument must be a string/);
84});
85
86test('postTask() validates abortSignal', async ({ rejects }) => {
87 const pool = new Piscina({
88 filename: resolve(__dirname, 'fixtures/eval.js')
89 });
90
91 rejects(pool.runTask('0', [], undefined, 42 as any),
92 /signal argument must be an object/);
93
94 rejects(pool.run('0', { signal: 42 as any }),
95 /signal argument must be an object/);
96});
97
98test('Piscina emits drain', async ({ ok }) => {
99 const pool = new Piscina({
100 filename: resolve(__dirname, 'fixtures/eval.js')
101 });
102
103 let drained = false;
104 pool.on('drain', () => {
105 drained = true;
106 });
107
108 await Promise.all([pool.runTask('123'), pool.runTask('123')]);
109
110 ok(drained);
111});
112
113test('Piscina can use async loaded workers', async ({ equal }) => {
114 const pool = new Piscina({
115 filename: resolve(__dirname, 'fixtures/eval-async.js')
116 });
117 equal(await pool.runTask('1'), 1);
118});
119
120test('Piscina can use async loaded esm workers', {}, async ({ equal }) => {
121 const pool = new Piscina({
122 filename: resolve(__dirname, 'fixtures/esm-async.mjs')
123 });
124 equal(await pool.runTask('1'), 1);
125});
126
127test('Piscina.run options is correct type', async ({ rejects }) => {
128 const pool = new Piscina({
129 filename: resolve(__dirname, 'fixtures/eval.js')
130 });
131
132 rejects(pool.run(42, 1 as any), /options must be an object/);
133});
Note: See TracBrowser for help on using the repository browser.