11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst fs = require('fs');
51cb0ef41Sopenharmony_ciconst util = require('util');
61cb0ef41Sopenharmony_ciconst { Writable } = require('stream');
71cb0ef41Sopenharmony_ciconst { Worker, isMainThread } = require('worker_threads');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciclass BufferingWritable extends Writable {
101cb0ef41Sopenharmony_ci  constructor() {
111cb0ef41Sopenharmony_ci    super();
121cb0ef41Sopenharmony_ci    this.chunks = [];
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  _write(chunk, enc, cb) {
161cb0ef41Sopenharmony_ci    this.chunks.push(chunk);
171cb0ef41Sopenharmony_ci    cb();
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  get buffer() {
211cb0ef41Sopenharmony_ci    return Buffer.concat(this.chunks);
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciif (isMainThread) {
261cb0ef41Sopenharmony_ci  const original = new BufferingWritable();
271cb0ef41Sopenharmony_ci  const passed = new BufferingWritable();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  const w = new Worker(__filename, { stdin: true, stdout: true });
301cb0ef41Sopenharmony_ci  const source = fs.createReadStream(process.execPath);
311cb0ef41Sopenharmony_ci  source.pipe(w.stdin);
321cb0ef41Sopenharmony_ci  source.pipe(original);
331cb0ef41Sopenharmony_ci  w.stdout.pipe(passed);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  passed.on('finish', common.mustCall(() => {
361cb0ef41Sopenharmony_ci    assert.strictEqual(original.buffer.compare(passed.buffer), 0,
371cb0ef41Sopenharmony_ci                       `Original: ${util.inspect(original.buffer)}, ` +
381cb0ef41Sopenharmony_ci                       `Actual: ${util.inspect(passed.buffer)}`);
391cb0ef41Sopenharmony_ci  }));
401cb0ef41Sopenharmony_ci} else {
411cb0ef41Sopenharmony_ci  process.stdin.pipe(process.stdout);
421cb0ef41Sopenharmony_ci}
43