|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const domain = require('domain'); |
| 6 | +const EventEmitter = require('events'); |
| 7 | + |
| 8 | +/* |
| 9 | + * Make sure that the domains stack and the active domain is setup properly when |
| 10 | + * a domain's error handler is called due to an error event being emitted. |
| 11 | + * More specifically, we want to test that: |
| 12 | + * - the active domain in the domain's error handler is *not* that domain, *but* |
| 13 | + * the active domain is a any direct parent domain at the time the error was |
| 14 | + * emitted. |
| 15 | + * - the domains stack in the domain's error handler does *not* include that |
| 16 | + * domain, *but* it includes all parents of that domain when the error was |
| 17 | + * emitted. |
| 18 | + */ |
| 19 | +const d1 = domain.create(); |
| 20 | +const d2 = domain.create(); |
| 21 | +const d3 = domain.create(); |
| 22 | + |
| 23 | +function checkExpectedDomains(err) { |
| 24 | + // First, check that domains stack and active domain is as expected when the |
| 25 | + // event handler is called synchronously via ee.emit('error'). |
| 26 | + if (domain._stack.length !== err.expectedStackLength) { |
| 27 | + console.error('expected domains stack length of %d, but instead is %d', |
| 28 | + err.expectedStackLength, domain._stack.length); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + if (process.domain !== err.expectedActiveDomain) { |
| 33 | + console.error('expected active domain to be %j, but instead is %j', |
| 34 | + err.expectedActiveDomain, process.domain); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + // Then make sure that the domains stack and active domain is setup as |
| 39 | + // expected when executing a callback scheduled via nextTick from the error |
| 40 | + // handler. |
| 41 | + process.nextTick(() => { |
| 42 | + const expectedStackLengthInNextTickCb = |
| 43 | + err.expectedStackLength > 0 ? 1 : 0; |
| 44 | + if (domain._stack.length !== expectedStackLengthInNextTickCb) { |
| 45 | + console.error('expected stack length in nextTick cb to be %d, ' + |
| 46 | + 'but instead is %d', expectedStackLengthInNextTickCb, |
| 47 | + domain._stack.length); |
| 48 | + process.exit(1); |
| 49 | + } |
| 50 | + |
| 51 | + const expectedActiveDomainInNextTickCb = |
| 52 | + expectedStackLengthInNextTickCb === 0 ? undefined : |
| 53 | + err.expectedActiveDomain; |
| 54 | + if (process.domain !== expectedActiveDomainInNextTickCb) { |
| 55 | + console.error('expected active domain in nextTick cb to be %j, ' + |
| 56 | + 'but instead is %j', expectedActiveDomainInNextTickCb, |
| 57 | + process.domain); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +d1.on('error', common.mustCall((err) => { |
| 64 | + checkExpectedDomains(err); |
| 65 | +}, 2)); |
| 66 | + |
| 67 | +d2.on('error', common.mustCall((err) => { |
| 68 | + checkExpectedDomains(err); |
| 69 | +}, 2)); |
| 70 | + |
| 71 | +d3.on('error', common.mustCall((err) => { |
| 72 | + checkExpectedDomains(err); |
| 73 | +}, 1)); |
| 74 | + |
| 75 | +d1.run(() => { |
| 76 | + const ee = new EventEmitter(); |
| 77 | + assert.strictEqual(process.domain, d1); |
| 78 | + assert.strictEqual(domain._stack.length, 1); |
| 79 | + |
| 80 | + const err = new Error('oops'); |
| 81 | + err.expectedStackLength = 0; |
| 82 | + err.expectedActiveDomain = null; |
| 83 | + ee.emit('error', err); |
| 84 | + |
| 85 | + assert.strictEqual(process.domain, d1); |
| 86 | + assert.strictEqual(domain._stack.length, 1); |
| 87 | +}); |
| 88 | + |
| 89 | +d1.run(() => { |
| 90 | + d1.run(() => { |
| 91 | + const ee = new EventEmitter(); |
| 92 | + |
| 93 | + assert.strictEqual(process.domain, d1); |
| 94 | + assert.strictEqual(domain._stack.length, 2); |
| 95 | + |
| 96 | + const err = new Error('oops'); |
| 97 | + err.expectedStackLength = 0; |
| 98 | + err.expectedActiveDomain = null; |
| 99 | + ee.emit('error', err); |
| 100 | + |
| 101 | + assert.strictEqual(process.domain, d1); |
| 102 | + assert.strictEqual(domain._stack.length, 2); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +d1.run(() => { |
| 107 | + d2.run(() => { |
| 108 | + const ee = new EventEmitter(); |
| 109 | + |
| 110 | + assert.strictEqual(process.domain, d2); |
| 111 | + assert.strictEqual(domain._stack.length, 2); |
| 112 | + |
| 113 | + const err = new Error('oops'); |
| 114 | + err.expectedStackLength = 1; |
| 115 | + err.expectedActiveDomain = d1; |
| 116 | + ee.emit('error', err); |
| 117 | + |
| 118 | + assert.strictEqual(process.domain, d2); |
| 119 | + assert.strictEqual(domain._stack.length, 2); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +d1.run(() => { |
| 124 | + d2.run(() => { |
| 125 | + d2.run(() => { |
| 126 | + const ee = new EventEmitter(); |
| 127 | + |
| 128 | + assert.strictEqual(process.domain, d2); |
| 129 | + assert.strictEqual(domain._stack.length, 3); |
| 130 | + |
| 131 | + const err = new Error('oops'); |
| 132 | + err.expectedStackLength = 1; |
| 133 | + err.expectedActiveDomain = d1; |
| 134 | + ee.emit('error', err); |
| 135 | + |
| 136 | + assert.strictEqual(process.domain, d2); |
| 137 | + assert.strictEqual(domain._stack.length, 3); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +d3.run(() => { |
| 143 | + d1.run(() => { |
| 144 | + d3.run(() => { |
| 145 | + d3.run(() => { |
| 146 | + const ee = new EventEmitter(); |
| 147 | + |
| 148 | + assert.strictEqual(process.domain, d3); |
| 149 | + assert.strictEqual(domain._stack.length, 4); |
| 150 | + |
| 151 | + const err = new Error('oops'); |
| 152 | + err.expectedStackLength = 2; |
| 153 | + err.expectedActiveDomain = d1; |
| 154 | + ee.emit('error', err); |
| 155 | + |
| 156 | + assert.strictEqual(process.domain, d3); |
| 157 | + assert.strictEqual(domain._stack.length, 4); |
| 158 | + }); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments