Skip to content

Commit 124205a

Browse files
Jimmy Jiaothiym23
authored andcommitted
test: support Promises
1 parent de255c6 commit 124205a

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

test/promises.tap.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
'use strict';
2+
3+
var tap = require('tap')
4+
, test = tap.test
5+
, createNamespace = require('../context.js').createNamespace
6+
;
7+
8+
test("continuation-local state with promises", function (t) {
9+
t.plan(4);
10+
11+
var namespace = createNamespace('namespace');
12+
namespace.run(function () {
13+
namespace.set('test', 0xabad1dea);
14+
15+
t.test("chained promises", function (t) {
16+
if (!global.Promise) return t.end();
17+
18+
namespace.run(function () {
19+
namespace.set('test', 31337);
20+
t.equal(namespace.get('test'), 31337, "state has been mutated");
21+
22+
Promise.resolve()
23+
.then(function () {
24+
t.equal(namespace.get('test'), 31337,
25+
"mutated state has persisted to first continuation");
26+
})
27+
.then(function () {
28+
t.equal(namespace.get('test'), 31337,
29+
"mutated state has persisted to second continuation");
30+
})
31+
.then(function () {
32+
t.equal(namespace.get('test'), 31337,
33+
"mutated state has persisted to third continuation");
34+
t.end();
35+
});
36+
});
37+
});
38+
39+
t.test("chained unwrapped promises", function (t) {
40+
if (!global.Promise) return t.end();
41+
42+
namespace.run(function () {
43+
namespace.set('test', 999);
44+
t.equal(namespace.get('test'), 999, "state has been mutated");
45+
46+
Promise.resolve()
47+
.then(function () {
48+
t.equal(namespace.get('test'), 999,
49+
"mutated state has persisted to first continuation");
50+
return Promise.resolve();
51+
})
52+
.then(function () {
53+
t.equal(namespace.get('test'), 999,
54+
"mutated state has persisted to second continuation");
55+
return Promise.resolve();
56+
})
57+
.then(function () {
58+
t.equal(namespace.get('test'), 999,
59+
"mutated state has persisted to third continuation");
60+
t.end();
61+
});
62+
});
63+
});
64+
65+
t.test("nested promises", function (t) {
66+
if (!global.Promise) return t.end();
67+
68+
namespace.run(function () {
69+
namespace.set('test', 54321);
70+
t.equal(namespace.get('test'), 54321, "state has been mutated");
71+
72+
Promise.resolve()
73+
.then(function () {
74+
t.equal(namespace.get('test'), 54321,
75+
"mutated state has persisted to first continuation");
76+
77+
Promise.resolve()
78+
.then(function () {
79+
t.equal(namespace.get('test'), 54321,
80+
"mutated state has persisted to second continuation");
81+
82+
Promise.resolve()
83+
.then(function () {
84+
t.equal(namespace.get('test'), 54321,
85+
"mutated state has persisted to third continuation");
86+
t.end();
87+
});
88+
});
89+
});
90+
});
91+
});
92+
93+
t.test("forked continuations", function (t) {
94+
if (!global.Promise) return t.end();
95+
96+
namespace.run(function () {
97+
namespace.set('test', 10101);
98+
t.equal(namespace.get('test'), 10101, "state has been mutated");
99+
100+
var promise = Promise.resolve();
101+
102+
promise
103+
.then(function () {
104+
t.equal(namespace.get('test'), 10101,
105+
"mutated state has persisted to first continuation");
106+
});
107+
promise
108+
.then(function () {
109+
t.equal(namespace.get('test'), 10101,
110+
"mutated state has persisted to second continuation");
111+
});
112+
promise
113+
.then(function () {
114+
t.equal(namespace.get('test'), 10101,
115+
"mutated state has persisted to third continuation");
116+
t.end();
117+
});
118+
});
119+
});
120+
});
121+
});

0 commit comments

Comments
 (0)