11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  Worker, MessageChannel
81cb0ef41Sopenharmony_ci} = require('worker_threads');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst channel = new MessageChannel();
111cb0ef41Sopenharmony_ciconst workerData = { mesage: channel.port1 };
121cb0ef41Sopenharmony_ciconst transferList = [channel.port1];
131cb0ef41Sopenharmony_ciconst meowScript = () => 'meow';
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  // Should receive the transferList param.
171cb0ef41Sopenharmony_ci  new Worker(`${meowScript}`, { eval: true, workerData, transferList });
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  // Should work with more than one MessagePort.
221cb0ef41Sopenharmony_ci  const channel1 = new MessageChannel();
231cb0ef41Sopenharmony_ci  const channel2 = new MessageChannel();
241cb0ef41Sopenharmony_ci  const workerData = { message: channel1.port1, message2: channel2.port1 };
251cb0ef41Sopenharmony_ci  const transferList = [channel1.port1, channel2.port1];
261cb0ef41Sopenharmony_ci  new Worker(`${meowScript}`, { eval: true, workerData, transferList });
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci{
301cb0ef41Sopenharmony_ci  const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
311cb0ef41Sopenharmony_ci  assert.strictEqual(uint8Array.length, 4);
321cb0ef41Sopenharmony_ci  new Worker(`
331cb0ef41Sopenharmony_ci    const { parentPort, workerData } = require('worker_threads');
341cb0ef41Sopenharmony_ci    parentPort.postMessage(workerData);
351cb0ef41Sopenharmony_ci    `, {
361cb0ef41Sopenharmony_ci    eval: true,
371cb0ef41Sopenharmony_ci    workerData: uint8Array,
381cb0ef41Sopenharmony_ci    transferList: [uint8Array.buffer]
391cb0ef41Sopenharmony_ci  }).on(
401cb0ef41Sopenharmony_ci    'message',
411cb0ef41Sopenharmony_ci    (message) =>
421cb0ef41Sopenharmony_ci      assert.deepStrictEqual(message, Uint8Array.of(1, 2, 3, 4))
431cb0ef41Sopenharmony_ci  );
441cb0ef41Sopenharmony_ci  assert.strictEqual(uint8Array.length, 0);
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci{
481cb0ef41Sopenharmony_ci  // Should throw on non valid transferList input.
491cb0ef41Sopenharmony_ci  const channel1 = new MessageChannel();
501cb0ef41Sopenharmony_ci  const channel2 = new MessageChannel();
511cb0ef41Sopenharmony_ci  const workerData = { message: channel1.port1, message2: channel2.port1 };
521cb0ef41Sopenharmony_ci  assert.throws(() => new Worker(`${meowScript}`, {
531cb0ef41Sopenharmony_ci    eval: true,
541cb0ef41Sopenharmony_ci    workerData,
551cb0ef41Sopenharmony_ci    transferList: []
561cb0ef41Sopenharmony_ci  }), {
571cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
581cb0ef41Sopenharmony_ci    message: 'Object that needs transfer was found in message but not ' +
591cb0ef41Sopenharmony_ci             'listed in transferList'
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci}
62