domain: support promises · nodejs/node@84dabe8

1+

'use strict';

2+

const common = require('../common');

3+

const assert = require('assert');

4+

const domain = require('domain');

5+

const fs = require('fs');

6+

const vm = require('vm');

7+8+

common.crashOnUnhandledRejection();

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(`Promise.resolve().then(common.mustCall(() => {

35+

assert.strictEqual(process.domain, d);

36+

}));`, { common, assert, process, d });

37+

}));

38+

}

39+40+

{

41+

const d1 = domain.create();

42+

const d2 = domain.create();

43+

let p;

44+

d1.run(common.mustCall(() => {

45+

p = Promise.resolve(42);

46+

}));

47+48+

d2.run(common.mustCall(() => {

49+

p.then(common.mustCall((v) => {

50+

assert.strictEqual(process.domain, d2);

51+

assert.strictEqual(p.domain, d1);

52+

}));

53+

}));

54+

}

55+56+

{

57+

const d1 = domain.create();

58+

const d2 = domain.create();

59+

let p;

60+

d1.run(common.mustCall(() => {

61+

p = Promise.resolve(42);

62+

}));

63+64+

d2.run(common.mustCall(() => {

65+

p.then(p.domain.bind(common.mustCall((v) => {

66+

assert.strictEqual(process.domain, d1);

67+

assert.strictEqual(p.domain, d1);

68+

})));

69+

}));

70+

}

71+72+

{

73+

const d1 = domain.create();

74+

const d2 = domain.create();

75+

let p;

76+

d1.run(common.mustCall(() => {

77+

p = Promise.resolve(42);

78+

}));

79+80+

d1.run(common.mustCall(() => {

81+

d2.run(common.mustCall(() => {

82+

p.then(common.mustCall((v) => {

83+

assert.strictEqual(process.domain, d2);

84+

assert.strictEqual(p.domain, d1);

85+

}));

86+

}));

87+

}));

88+

}

89+90+

{

91+

const d1 = domain.create();

92+

const d2 = domain.create();

93+

let p;

94+

d1.run(common.mustCall(() => {

95+

p = Promise.reject(new Error('foobar'));

96+

}));

97+98+

d2.run(common.mustCall(() => {

99+

p.catch(common.mustCall((v) => {

100+

assert.strictEqual(process.domain, d2);

101+

assert.strictEqual(p.domain, d1);

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+

}