Blame view

node_modules/continuation-local-storage/test/tracer-scenarios.tap.js 9.58 KB
f7563de62   Palak Handa   first commit
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  'use strict';
  
  var EventEmitter = require('events').EventEmitter
    , assert       = require('assert')
    , test         = require('tap').test
    , cls          = require('../context.js')
    ;
  
  var nextID = 1;
  function fresh(name) {
    assert.ok(!cls.getNamespace(name), "namespace " + name + " already exists");
    return cls.createNamespace(name);
  }
  
  function destroy(name) {
    return function destroyer(t) {
      cls.destroyNamespace(name);
      assert.ok(!cls.getNamespace(name), "namespace '" + name + "' should no longer exist");
      t.end();
    };
  }
  
  function runInTransaction(name, fn) {
    var namespace = cls.getNamespace(name);
    assert(namespace, "namespaces " + name + " doesn't exist");
  
    var context = namespace.createContext();
    context.transaction = ++nextID;
    process.nextTick(namespace.bind(fn, context));
  }
  
  test("asynchronous state propagation", function (t) {
    t.plan(24);
  
    t.test("a. async transaction with setTimeout", function (t) {
      t.plan(2);
  
      var namespace = fresh('a', this);
  
      function handler() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('a', function () { setTimeout(handler, 100); });
    });
  
    t.test("a. cleanup", destroy('a'));
  
    t.test("b. async transaction with setInterval", function (t) {
      t.plan(4);
  
      var namespace = fresh('b', this)
        , count     = 0
        , handle
        ;
  
      function handler() {
        count += 1;
        if (count > 2) clearInterval(handle);
        t.ok(namespace.get('transaction'), "transaction should be visible");
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('b', function () { handle = setInterval(handler, 50); });
    });
  
    t.test("b. cleanup", destroy('b'));
  
    t.test("c. async transaction with process.nextTick", function (t) {
      t.plan(2);
  
      var namespace = fresh('c', this);
  
      function handler() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('c', function () { process.nextTick(handler); });
    });
  
    t.test("c. cleanup", destroy('c'));
  
    t.test("d. async transaction with EventEmitter.emit", function (t) {
      t.plan(2);
  
      var namespace = fresh('d', this)
        , ee        = new EventEmitter()
        ;
  
      function handler() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('d', function () {
        ee.on('transaction', handler);
        ee.emit('transaction');
      });
    });
  
    t.test("d. cleanup", destroy('d'));
  
    t.test("e. two overlapping async transactions with setTimeout", function (t) {
      t.plan(6);
  
      var namespace = fresh('e', this)
        , first
        , second
        ;
  
      function handler(id) {
        t.ok(namespace.get('transaction'), "transaction should be visible");
        t.equal(namespace.get('transaction'), id, "transaction matches");
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('e', function () {
        first = namespace.get('transaction');
        setTimeout(handler.bind(null, first), 100);
      });
  
      setTimeout(function () {
        runInTransaction('e', function () {
          second = namespace.get('transaction');
          t.notEqual(first, second, "different transaction IDs");
          setTimeout(handler.bind(null, second), 100);
        });
      }, 25);
    });
  
    t.test("e. cleanup", destroy('e'));
  
    t.test("f. two overlapping async transactions with setInterval", function (t) {
      t.plan(15);
  
      var namespace = fresh('f', this);
  
      function runInterval() {
        var count = 0
          , handle
          , id
          ;
  
        function handler() {
          count += 1;
          if (count > 2) clearInterval(handle);
          t.ok(namespace.get('transaction'), "transaction should be visible");
          t.equal(id, namespace.get('transaction'), "transaction ID should be immutable");
        }
  
        function run() {
          t.ok(namespace.get('transaction'), "transaction should have been created");
          id = namespace.get('transaction');
          handle = setInterval(handler, 50);
        }
  
        runInTransaction('f', run);
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInterval(); runInterval();
    });
  
    t.test("f. cleanup", destroy('f'));
  
    t.test("g. two overlapping async transactions with process.nextTick", function (t) {
      t.plan(6);
  
      var namespace = fresh('g', this)
        , first
        , second
        ;
  
      function handler(id) {
        var transaction = namespace.get('transaction');
        t.ok(transaction, "transaction should be visible");
        t.equal(transaction, id, "transaction matches");
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('g', function () {
        first = namespace.get('transaction');
        process.nextTick(handler.bind(null, first));
      });
  
      process.nextTick(function () {
        runInTransaction('g', function () {
          second = namespace.get('transaction');
          t.notEqual(first, second, "different transaction IDs");
          process.nextTick(handler.bind(null, second));
        });
      });
    });
  
    t.test("g. cleanup", destroy('g'));
  
    t.test("h. two overlapping async runs with EventEmitter.prototype.emit", function (t) {
      t.plan(3);
  
      var namespace = fresh('h', this)
        , ee        = new EventEmitter()
        ;
  
      function handler() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
      }
  
      function lifecycle() {
        ee.once('transaction', process.nextTick.bind(process, handler));
        ee.emit('transaction');
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('h', lifecycle);
      runInTransaction('h', lifecycle);
    });
  
    t.test("h. cleanup", destroy('h'));
  
    t.test("i. async transaction with an async sub-call with setTimeout", function (t) {
      t.plan(5);
  
      var namespace = fresh('i', this);
  
      function inner(callback) {
        setTimeout(function () {
          t.ok(namespace.get('transaction'), "transaction should (yep) still be visible");
          callback();
        }, 50);
      }
  
      function outer() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
        setTimeout(function () {
          t.ok(namespace.get('transaction'), "transaction should still be visible");
          inner(function () {
            t.ok(namespace.get('transaction'), "transaction should even still be visible");
          });
        }, 50);
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('i', setTimeout.bind(null, outer, 50));
    });
  
    t.test("i. cleanup", destroy('i'));
  
    t.test("j. async transaction with an async sub-call with setInterval", function (t) {
      t.plan(5);
  
      var namespace = fresh('j', this)
        , outerHandle
        , innerHandle
        ;
  
      function inner(callback) {
        innerHandle = setInterval(function () {
          clearInterval(innerHandle);
          t.ok(namespace.get('transaction'), "transaction should (yep) still be visible");
          callback();
        }, 50);
      }
  
      function outer() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
        outerHandle = setInterval(function () {
          clearInterval(outerHandle);
          t.ok(namespace.get('transaction'), "transaction should still be visible");
          inner(function () {
            t.ok(namespace.get('transaction'), "transaction should even still be visible");
          });
        }, 50);
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('j', outer);
    });
  
    t.test("j. cleanup", destroy('j'));
  
    t.test("k. async transaction with an async call with process.nextTick", function (t) {
      t.plan(5);
  
      var namespace = fresh('k', this);
  
      function inner(callback) {
        process.nextTick(function () {
          t.ok(namespace.get('transaction'), "transaction should (yep) still be visible");
          callback();
        });
      }
  
      function outer() {
        t.ok(namespace.get('transaction'), "transaction should be visible");
        process.nextTick(function () {
          t.ok(namespace.get('transaction'), "transaction should still be visible");
          inner(function () {
            t.ok(namespace.get('transaction'), "transaction should even still be visible");
          });
        });
      }
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('k', function () { process.nextTick(outer); });
    });
  
    t.test("k. cleanup", destroy('k'));
  
    t.test("l. async transaction with an async call with EventEmitter.emit", function (t) {
      t.plan(4);
  
      var namespace = fresh('l', this)
        , outer     = new EventEmitter()
        , inner     = new EventEmitter()
        ;
  
      inner.on('pong', function (callback) {
        t.ok(namespace.get('transaction'), "transaction should still be visible");
        callback();
      });
  
      function outerCallback() {
        t.ok(namespace.get('transaction'), "transaction should even still be visible");
      }
  
      outer.on('ping', function () {
        t.ok(namespace.get('transaction'), "transaction should be visible");
        inner.emit('pong', outerCallback);
      });
  
      t.notOk(namespace.get('transaction'), "transaction should not yet be visible");
      runInTransaction('l', outer.emit.bind(outer, 'ping'));
    });
  
    t.test("l. cleanup", destroy('l'));
  });