summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-environmentdata.js
blob: aef0e1213ff6af02011fd447be51538334b1e201 (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
'use strict';
// Flags: --expose-internals

require('../common');
const {
  Worker,
  getEnvironmentData,
  setEnvironmentData,
  threadId,
} = require('worker_threads');

const { assignEnvironmentData } = require('internal/worker');

const {
  deepStrictEqual,
  strictEqual,
} = require('assert');

if (!process.env.HAS_STARTED_WORKER) {
  process.env.HAS_STARTED_WORKER = 1;
  setEnvironmentData('foo', 'bar');
  setEnvironmentData('hello', { value: 'world' });
  setEnvironmentData(1, 2);
  strictEqual(getEnvironmentData(1), 2);
  setEnvironmentData(1); // Delete it, key won't show up in the worker.
  new Worker(__filename);
  setEnvironmentData('hello');  // Delete it. Has no impact on the worker.
} else {
  strictEqual(getEnvironmentData('foo'), 'bar');
  deepStrictEqual(getEnvironmentData('hello'), { value: 'world' });
  strictEqual(getEnvironmentData(1), undefined);
  assignEnvironmentData(undefined); // It won't setup any key.
  strictEqual(getEnvironmentData(undefined), undefined);

  // Recurse to make sure the environment data is inherited
  if (threadId <= 2)
    new Worker(__filename);
}