forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove-test.ts
92 lines (82 loc) · 2.78 KB
/
move-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
import Piscina from '..';
import {
isMovable,
markMovable,
isTransferable
} from '../dist/common';
import { test } from 'tap';
import { types } from 'util';
import { MessageChannel, MessagePort } from 'worker_threads';
import { resolve } from 'path';
const {
transferableSymbol,
valueSymbol
} = Piscina;
test('Marking an object as movable works as expected', async ({ ok }) => {
const obj : any = {
get [transferableSymbol] () : object { return {}; },
get [valueSymbol] () : object { return {}; }
};
ok(isTransferable(obj));
ok(!isMovable(obj)); // It's not movable initially
markMovable(obj);
ok(isMovable(obj)); // It is movable now
});
test('Marking primitives and null works as expected', async ({ equal }) => {
equal(Piscina.move(null), null);
equal(Piscina.move(1 as any), 1);
equal(Piscina.move(false as any), false);
equal(Piscina.move('test' as any), 'test');
});
test('Using Piscina.move() returns a movable object', async ({ ok }) => {
const obj : any = {
get [transferableSymbol] () : object { return {}; },
get [valueSymbol] () : object { return {}; }
};
ok(!isMovable(obj)); // It's not movable initially
const movable = Piscina.move(obj);
ok(isMovable(movable)); // It is movable now
});
test('Using ArrayBuffer works as expected', async ({ ok, equal }) => {
const ab = new ArrayBuffer(5);
const movable = Piscina.move(ab);
ok(isMovable(movable));
ok(types.isAnyArrayBuffer(movable[valueSymbol]));
ok(types.isAnyArrayBuffer(movable[transferableSymbol]));
equal(movable[transferableSymbol], ab);
});
test('Using TypedArray works as expected', async ({ ok, equal }) => {
const ab = new Uint8Array(5);
const movable = Piscina.move(ab);
ok(isMovable(movable));
ok((types as any).isArrayBufferView(movable[valueSymbol]));
ok(types.isAnyArrayBuffer(movable[transferableSymbol]));
equal(movable[transferableSymbol], ab.buffer);
});
test('Using MessagePort works as expected', async ({ ok, equal }) => {
const mc = new MessageChannel();
const movable = Piscina.move(mc.port1);
ok(isMovable(movable));
ok(movable[valueSymbol] instanceof MessagePort);
ok(movable[transferableSymbol] instanceof MessagePort);
equal(movable[transferableSymbol], mc.port1);
});
test('Moving works', async ({ equal, ok }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/move.ts')
});
{
// Test with empty transferList
const ab = new ArrayBuffer(10);
const ret = await pool.run(Piscina.move(ab));
equal(ab.byteLength, 0); // It was moved
ok(types.isAnyArrayBuffer(ret));
}
{
// Test with empty transferList
const ab = new ArrayBuffer(10);
const ret = await pool.run(Piscina.move(ab), { transferList: [] });
equal(ab.byteLength, 0); // It was moved
ok(types.isAnyArrayBuffer(ret));
}
});