11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst util = require('util');
61cb0ef41Sopenharmony_ciconst { MessageChannel } = require('worker_threads');
71cb0ef41Sopenharmony_ciconst tick = require('../common/tick');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciassert.throws(common.mustCall(() => {
121cb0ef41Sopenharmony_ci  port1.postMessage(null, [port1]);
131cb0ef41Sopenharmony_ci}), common.mustCall((err) => {
141cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'DataCloneError');
151cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'Transfer list contains source port');
161cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 25);
171cb0ef41Sopenharmony_ci  assert.ok(err instanceof Error);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  const DOMException = err.constructor;
201cb0ef41Sopenharmony_ci  assert.ok(err instanceof DOMException);
211cb0ef41Sopenharmony_ci  assert.strictEqual(DOMException.name, 'DOMException');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  return true;
241cb0ef41Sopenharmony_ci}));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// The failed transfer should not affect the ports in anyway.
271cb0ef41Sopenharmony_ciport2.onmessage = common.mustCall((message) => {
281cb0ef41Sopenharmony_ci  assert.strictEqual(message.data, 2);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const inspectedPort1 = util.inspect(port1);
311cb0ef41Sopenharmony_ci  const inspectedPort2 = util.inspect(port2);
321cb0ef41Sopenharmony_ci  assert(inspectedPort1.includes('active: true'), inspectedPort1);
331cb0ef41Sopenharmony_ci  assert(inspectedPort2.includes('active: true'), inspectedPort2);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  port1.close();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  tick(10, () => {
381cb0ef41Sopenharmony_ci    const inspectedPort1 = util.inspect(port1);
391cb0ef41Sopenharmony_ci    const inspectedPort2 = util.inspect(port2);
401cb0ef41Sopenharmony_ci    assert(inspectedPort1.includes('active: false'), inspectedPort1);
411cb0ef41Sopenharmony_ci    assert(inspectedPort2.includes('active: false'), inspectedPort2);
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci});
441cb0ef41Sopenharmony_ciport1.postMessage(2);
45