11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { Worker, isMainThread } = require('worker_threads');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (isMainThread) {
91cb0ef41Sopenharmony_ci  const w = new Worker(__filename, {
101cb0ef41Sopenharmony_ci    stdin: true,
111cb0ef41Sopenharmony_ci    stdout: true,
121cb0ef41Sopenharmony_ci    stderr: true
131cb0ef41Sopenharmony_ci  });
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const { stdin, stdout, stderr } = w;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  w.on('exit', common.mustCall((code) => {
181cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    // `postMessage` should not throw after termination
211cb0ef41Sopenharmony_ci    // (this mimics the browser behavior).
221cb0ef41Sopenharmony_ci    w.postMessage('foobar');
231cb0ef41Sopenharmony_ci    w.ref();
241cb0ef41Sopenharmony_ci    w.unref();
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    // Sanity check.
271cb0ef41Sopenharmony_ci    assert.strictEqual(w.threadId, -1);
281cb0ef41Sopenharmony_ci    assert.strictEqual(w.stdin, stdin);
291cb0ef41Sopenharmony_ci    assert.strictEqual(w.stdout, stdout);
301cb0ef41Sopenharmony_ci    assert.strictEqual(w.stderr, stderr);
311cb0ef41Sopenharmony_ci  }));
321cb0ef41Sopenharmony_ci} else {
331cb0ef41Sopenharmony_ci  process.exit(0);
341cb0ef41Sopenharmony_ci}
35