summaryrefslogtreecommitdiff
path: root/test/parallel/test-child-process-advanced-serialization.js
blob: 71a0c44a042d31a9aa9173b4c738ca0cfbfa3675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const { once } = require('events');
const { inspect } = require('util');

if (process.argv[2] !== 'child') {
  for (const value of [null, 42, Infinity, 'foo']) {
    assert.throws(() => {
      child_process.spawn(process.execPath, [], { serialization: value });
    }, {
      code: 'ERR_INVALID_ARG_VALUE',
      message: "The property 'options.serialization' " +
        "must be one of: undefined, 'json', 'advanced'. " +
        `Received ${inspect(value)}`
    });
  }

  (async () => {
    const cp = child_process.spawn(process.execPath, [__filename, 'child'],
                                   {
                                     stdio: ['ipc', 'inherit', 'inherit'],
                                     serialization: 'advanced'
                                   });

    const circular = {};
    circular.circular = circular;
    for await (const message of [
      { uint8: new Uint8Array(4) },
      { float64: new Float64Array([ Math.PI ]) },
      { buffer: Buffer.from('Hello!') },
      { map: new Map([{ a: 1 }, { b: 2 }]) },
      { bigInt: 1337n },
      circular,
      new Error('Something went wrong'),
      new RangeError('Something range-y went wrong'),
    ]) {
      cp.send(message);
      const [ received ] = await once(cp, 'message');
      assert.deepStrictEqual(received, message);
    }

    cp.disconnect();
  })().then(common.mustCall());
} else {
  process.on('message', (msg) => process.send(msg));
}