move-test.ts 3.2 KB

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