11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst domain = require('domain');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst vm = require('vm');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciprocess.on('warning', common.mustNotCall());
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const d = domain.create();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  d.run(common.mustCall(() => {
141cb0ef41Sopenharmony_ci    Promise.resolve().then(common.mustCall(() => {
151cb0ef41Sopenharmony_ci      assert.strictEqual(process.domain, d);
161cb0ef41Sopenharmony_ci    }));
171cb0ef41Sopenharmony_ci  }));
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  const d = domain.create();
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  d.run(common.mustCall(() => {
241cb0ef41Sopenharmony_ci    Promise.resolve().then(() => {}).then(() => {}).then(common.mustCall(() => {
251cb0ef41Sopenharmony_ci      assert.strictEqual(process.domain, d);
261cb0ef41Sopenharmony_ci    }));
271cb0ef41Sopenharmony_ci  }));
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci{
311cb0ef41Sopenharmony_ci  const d = domain.create();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  d.run(common.mustCall(() => {
341cb0ef41Sopenharmony_ci    vm.runInNewContext(`
351cb0ef41Sopenharmony_ci      const promise = Promise.resolve();
361cb0ef41Sopenharmony_ci      assert.strictEqual(promise.domain, undefined);
371cb0ef41Sopenharmony_ci      promise.then(common.mustCall(() => {
381cb0ef41Sopenharmony_ci        assert.strictEqual(process.domain, d);
391cb0ef41Sopenharmony_ci      }));
401cb0ef41Sopenharmony_ci    `, { common, assert, process, d });
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  const d1 = domain.create();
461cb0ef41Sopenharmony_ci  const d2 = domain.create();
471cb0ef41Sopenharmony_ci  let p;
481cb0ef41Sopenharmony_ci  d1.run(common.mustCall(() => {
491cb0ef41Sopenharmony_ci    p = Promise.resolve(42);
501cb0ef41Sopenharmony_ci  }));
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  d2.run(common.mustCall(() => {
531cb0ef41Sopenharmony_ci    p.then(common.mustCall((v) => {
541cb0ef41Sopenharmony_ci      assert.strictEqual(process.domain, d2);
551cb0ef41Sopenharmony_ci    }));
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci{
601cb0ef41Sopenharmony_ci  const d1 = domain.create();
611cb0ef41Sopenharmony_ci  const d2 = domain.create();
621cb0ef41Sopenharmony_ci  let p;
631cb0ef41Sopenharmony_ci  d1.run(common.mustCall(() => {
641cb0ef41Sopenharmony_ci    p = Promise.resolve(42);
651cb0ef41Sopenharmony_ci  }));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  d2.run(common.mustCall(() => {
681cb0ef41Sopenharmony_ci    p.then(d1.bind(common.mustCall((v) => {
691cb0ef41Sopenharmony_ci      assert.strictEqual(process.domain, d1);
701cb0ef41Sopenharmony_ci    })));
711cb0ef41Sopenharmony_ci  }));
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  const d1 = domain.create();
761cb0ef41Sopenharmony_ci  const d2 = domain.create();
771cb0ef41Sopenharmony_ci  let p;
781cb0ef41Sopenharmony_ci  d1.run(common.mustCall(() => {
791cb0ef41Sopenharmony_ci    p = Promise.resolve(42);
801cb0ef41Sopenharmony_ci  }));
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  d1.run(common.mustCall(() => {
831cb0ef41Sopenharmony_ci    d2.run(common.mustCall(() => {
841cb0ef41Sopenharmony_ci      p.then(common.mustCall((v) => {
851cb0ef41Sopenharmony_ci        assert.strictEqual(process.domain, d2);
861cb0ef41Sopenharmony_ci      }));
871cb0ef41Sopenharmony_ci    }));
881cb0ef41Sopenharmony_ci  }));
891cb0ef41Sopenharmony_ci}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci{
921cb0ef41Sopenharmony_ci  const d1 = domain.create();
931cb0ef41Sopenharmony_ci  const d2 = domain.create();
941cb0ef41Sopenharmony_ci  let p;
951cb0ef41Sopenharmony_ci  d1.run(common.mustCall(() => {
961cb0ef41Sopenharmony_ci    p = Promise.reject(new Error('foobar'));
971cb0ef41Sopenharmony_ci  }));
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  d2.run(common.mustCall(() => {
1001cb0ef41Sopenharmony_ci    p.catch(common.mustCall((v) => {
1011cb0ef41Sopenharmony_ci      assert.strictEqual(process.domain, d2);
1021cb0ef41Sopenharmony_ci    }));
1031cb0ef41Sopenharmony_ci  }));
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci{
1071cb0ef41Sopenharmony_ci  const d = domain.create();
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  d.run(common.mustCall(() => {
1101cb0ef41Sopenharmony_ci    Promise.resolve().then(common.mustCall(() => {
1111cb0ef41Sopenharmony_ci      setTimeout(common.mustCall(() => {
1121cb0ef41Sopenharmony_ci        assert.strictEqual(process.domain, d);
1131cb0ef41Sopenharmony_ci      }), 0);
1141cb0ef41Sopenharmony_ci    }));
1151cb0ef41Sopenharmony_ci  }));
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci{
1191cb0ef41Sopenharmony_ci  const d = domain.create();
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  d.run(common.mustCall(() => {
1221cb0ef41Sopenharmony_ci    Promise.resolve().then(common.mustCall(() => {
1231cb0ef41Sopenharmony_ci      fs.readFile(__filename, common.mustCall(() => {
1241cb0ef41Sopenharmony_ci        assert.strictEqual(process.domain, d);
1251cb0ef41Sopenharmony_ci      }));
1261cb0ef41Sopenharmony_ci    }));
1271cb0ef41Sopenharmony_ci  }));
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci{
1301cb0ef41Sopenharmony_ci  // Unhandled rejections become errors on the domain
1311cb0ef41Sopenharmony_ci  const d = domain.create();
1321cb0ef41Sopenharmony_ci  d.on('error', common.mustCall((e) => {
1331cb0ef41Sopenharmony_ci    assert.strictEqual(e.message, 'foo');
1341cb0ef41Sopenharmony_ci  }));
1351cb0ef41Sopenharmony_ci  d.run(common.mustCall(() => {
1361cb0ef41Sopenharmony_ci    Promise.reject(new Error('foo'));
1371cb0ef41Sopenharmony_ci  }));
1381cb0ef41Sopenharmony_ci}
139