source: trip-planner-front/node_modules/piscina/test/move-test.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.2 KB
Line 
1import Piscina from '..';
2import {
3 isMovable,
4 markMovable,
5 isTransferable
6} from '../dist/src/common';
7import { test } from 'tap';
8import { types } from 'util';
9import { MessageChannel, MessagePort } from 'worker_threads';
10import { resolve } from 'path';
11
12const {
13 transferableSymbol,
14 valueSymbol
15} = Piscina;
16
17test('Marking an object as movable works as expected', async ({ ok }) => {
18 const obj : any = {
19 get [transferableSymbol] () : object { return {}; },
20 get [valueSymbol] () : object { return {}; }
21 };
22 ok(isTransferable(obj));
23 ok(!isMovable(obj)); // It's not movable initially
24 markMovable(obj);
25 ok(isMovable(obj)); // It is movable now
26});
27
28test('Marking primitives and null works as expected', async ({ equal }) => {
29 equal(Piscina.move(null), null);
30 equal(Piscina.move(1 as any), 1);
31 equal(Piscina.move(false as any), false);
32 equal(Piscina.move('test' as any), 'test');
33});
34
35test('Using Piscina.move() returns a movable object', async ({ ok }) => {
36 const obj : any = {
37 get [transferableSymbol] () : object { return {}; },
38 get [valueSymbol] () : object { return {}; }
39 };
40 ok(!isMovable(obj)); // It's not movable initially
41 const movable = Piscina.move(obj);
42 ok(isMovable(movable)); // It is movable now
43});
44
45test('Using ArrayBuffer works as expected', async ({ ok, equal }) => {
46 const ab = new ArrayBuffer(5);
47 const movable = Piscina.move(ab);
48 ok(isMovable(movable));
49 ok(types.isAnyArrayBuffer(movable[valueSymbol]));
50 ok(types.isAnyArrayBuffer(movable[transferableSymbol]));
51 equal(movable[transferableSymbol], ab);
52});
53
54test('Using TypedArray works as expected', async ({ ok, equal }) => {
55 const ab = new Uint8Array(5);
56 const movable = Piscina.move(ab);
57 ok(isMovable(movable));
58 ok((types as any).isArrayBufferView(movable[valueSymbol]));
59 ok(types.isAnyArrayBuffer(movable[transferableSymbol]));
60 equal(movable[transferableSymbol], ab.buffer);
61});
62
63test('Using MessagePort works as expected', async ({ ok, equal }) => {
64 const mc = new MessageChannel();
65 const movable = Piscina.move(mc.port1);
66 ok(isMovable(movable));
67 ok(movable[valueSymbol] instanceof MessagePort);
68 ok(movable[transferableSymbol] instanceof MessagePort);
69 equal(movable[transferableSymbol], mc.port1);
70});
71
72test('Moving works', async ({ equal, ok }) => {
73 const pool = new Piscina({
74 filename: resolve(__dirname, 'fixtures/move.ts')
75 });
76
77 {
78 const ab = new ArrayBuffer(10);
79 const ret = await pool.runTask(Piscina.move(ab));
80 equal(ab.byteLength, 0); // It was moved
81 ok(types.isAnyArrayBuffer(ret));
82 }
83
84 {
85 // Test with empty transferList
86 const ab = new ArrayBuffer(10);
87 const ret = await pool.runTask(Piscina.move(ab), []);
88 equal(ab.byteLength, 0); // It was moved
89 ok(types.isAnyArrayBuffer(ret));
90 }
91
92 {
93 // Test with empty transferList
94 const ab = new ArrayBuffer(10);
95 const ret = await pool.run(Piscina.move(ab));
96 equal(ab.byteLength, 0); // It was moved
97 ok(types.isAnyArrayBuffer(ret));
98 }
99
100 {
101 // Test with empty transferList
102 const ab = new ArrayBuffer(10);
103 const ret = await pool.run(Piscina.move(ab), { transferList: [] });
104 equal(ab.byteLength, 0); // It was moved
105 ok(types.isAnyArrayBuffer(ret));
106 }
107});
Note: See TracBrowser for help on using the repository browser.