1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const domain = require('domain'); 5const fs = require('fs'); 6const vm = require('vm'); 7 8process.on('warning', common.mustNotCall()); 9 10{ 11 const d = domain.create(); 12 13 d.run(common.mustCall(() => { 14 Promise.resolve().then(common.mustCall(() => { 15 assert.strictEqual(process.domain, d); 16 })); 17 })); 18} 19 20{ 21 const d = domain.create(); 22 23 d.run(common.mustCall(() => { 24 Promise.resolve().then(() => {}).then(() => {}).then(common.mustCall(() => { 25 assert.strictEqual(process.domain, d); 26 })); 27 })); 28} 29 30{ 31 const d = domain.create(); 32 33 d.run(common.mustCall(() => { 34 vm.runInNewContext(` 35 const promise = Promise.resolve(); 36 assert.strictEqual(promise.domain, undefined); 37 promise.then(common.mustCall(() => { 38 assert.strictEqual(process.domain, d); 39 })); 40 `, { common, assert, process, d }); 41 })); 42} 43 44{ 45 const d1 = domain.create(); 46 const d2 = domain.create(); 47 let p; 48 d1.run(common.mustCall(() => { 49 p = Promise.resolve(42); 50 })); 51 52 d2.run(common.mustCall(() => { 53 p.then(common.mustCall((v) => { 54 assert.strictEqual(process.domain, d2); 55 })); 56 })); 57} 58 59{ 60 const d1 = domain.create(); 61 const d2 = domain.create(); 62 let p; 63 d1.run(common.mustCall(() => { 64 p = Promise.resolve(42); 65 })); 66 67 d2.run(common.mustCall(() => { 68 p.then(d1.bind(common.mustCall((v) => { 69 assert.strictEqual(process.domain, d1); 70 }))); 71 })); 72} 73 74{ 75 const d1 = domain.create(); 76 const d2 = domain.create(); 77 let p; 78 d1.run(common.mustCall(() => { 79 p = Promise.resolve(42); 80 })); 81 82 d1.run(common.mustCall(() => { 83 d2.run(common.mustCall(() => { 84 p.then(common.mustCall((v) => { 85 assert.strictEqual(process.domain, d2); 86 })); 87 })); 88 })); 89} 90 91{ 92 const d1 = domain.create(); 93 const d2 = domain.create(); 94 let p; 95 d1.run(common.mustCall(() => { 96 p = Promise.reject(new Error('foobar')); 97 })); 98 99 d2.run(common.mustCall(() => { 100 p.catch(common.mustCall((v) => { 101 assert.strictEqual(process.domain, d2); 102 })); 103 })); 104} 105 106{ 107 const d = domain.create(); 108 109 d.run(common.mustCall(() => { 110 Promise.resolve().then(common.mustCall(() => { 111 setTimeout(common.mustCall(() => { 112 assert.strictEqual(process.domain, d); 113 }), 0); 114 })); 115 })); 116} 117 118{ 119 const d = domain.create(); 120 121 d.run(common.mustCall(() => { 122 Promise.resolve().then(common.mustCall(() => { 123 fs.readFile(__filename, common.mustCall(() => { 124 assert.strictEqual(process.domain, d); 125 })); 126 })); 127 })); 128} 129