-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathoverlapping-nexttick.tap.js
192 lines (154 loc) · 4.03 KB
/
overlapping-nexttick.tap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var test = require('tap').test
, assert = require('assert')
;
if (!global.setImmediate) global.setImmediate = setTimeout;
/**
*
*
*
*
* SETUP AND BOILERPLATE
*
*
*
*/
if (!process.addAsyncListener) require('../index.js');
/*
* CLS code
*/
function Namespace () {
this.active = Object.create(null);
this._stack = [];
this.id = null;
}
Namespace.prototype.set = function (key, value) {
this.active[key] = value;
return value;
};
Namespace.prototype.get = function (key) {
return this.active[key];
};
Namespace.prototype.enter = function (context) {
assert.ok(context, "context must be provided for entering");
this._stack.push(this.active);
this.active = context;
};
Namespace.prototype.exit = function (context) {
assert.ok(context, "context must be provided for exiting");
if (this.active === context) {
assert.ok(this._stack.length, "can't remove top context");
this.active = this._stack.pop();
return;
}
var index = this._stack.lastIndexOf(context);
assert.ok(index >= 0, "context not currently entered; can't exit");
assert.ok(index, "can't remove top context");
this.active = this._stack[index - 1];
this._stack.length = index - 1;
};
Namespace.prototype.createContext = function () {
return Object.create(this.active);
};
Namespace.prototype.run = function (fn) {
var context = this.createContext();
this.enter(context);
try {
fn(context);
return context;
}
finally {
this.exit(context);
}
};
Namespace.prototype.bind = function (fn, context) {
if (!context) context = this.active;
var self = this;
return function () {
self.enter(context);
try {
return fn.apply(this, arguments);
}
finally {
self.exit(context);
}
};
};
function create(name) {
assert.ok(name, "namespace must be given a name!");
var namespace = new Namespace(name);
namespace.id = process.addAsyncListener(
{
create : function () { return namespace.active; },
before : function (context, domain) { namespace.enter(domain); },
after : function (context, domain) { namespace.exit(domain); }
}
);
return namespace;
}
/*
* Transaction code
*/
var id = 1337;
function Transaction() { this.id = id++; }
/*
* Tracer code
*/
var namespace = create("__NR_tracer");
function getTransaction() {
var state = namespace.get('state');
if (state) return state.transaction;
}
function transactionProxy(handler) {
return function wrapTransactionInvocation() {
var state = {transaction : new Transaction()};
var context = namespace.createContext();
context.state = state;
return namespace.bind(handler, context).apply(this, arguments);
};
}
/**
*
*
*
*
* TESTS
*
*
*
*/
test("overlapping requests", function (t) {
t.plan(2);
t.test("simple overlap", function (t) {
t.plan(3);
setImmediate(function () { console.log('!'); });
var n = create("test2");
t.ok(!n.get('state'), "state should not yet be visible");
n.run(function () {
n.set('state', true);
t.ok(n.get('state'), "state should be visible");
setImmediate(function () { t.ok(n.get('state'), "state should be visible"); });
});
});
t.test("two process.nextTicks", function (t) {
t.plan(6);
function handler(id) {
var transaction = getTransaction();
t.ok(transaction, "transaction should be visible");
t.equal((transaction || {}).id, id, "transaction matches");
}
t.ok(!getTransaction(), "transaction should not yet be visible");
var first;
var proxied = transactionProxy(function () {
t.ok(getTransaction(), "transaction should be visible");
first = getTransaction().id;
process.nextTick(function () { handler(first); }, 42);
});
proxied();
process.nextTick(transactionProxy(function () {
t.ok(getTransaction(), "transaction should be visible");
var second = getTransaction().id;
t.notEqual(first, second, "different transaction IDs");
process.nextTick(function () { handler(second); }, 42);
}), 42);
});
});