Blame view

node_modules/continuation-local-storage/test/error-handling.tap.js 4.1 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
  'use strict';
  
  var test   = require('tap').test
    , cls    = require('../context.js')
    , domain = require('domain')
    ;
  
  test("continuation-local storage glue with a throw in the continuation chain",
       function (t) {
    var namespace = cls.createNamespace('test');
    namespace.run(function () {
      var d = domain.create();
      namespace.set('outer', true);
  
      d.on('error', function (blerg) {
        t.equal(blerg.message, "explicitly nonlocal exit", "got the expected exception");
        t.ok(namespace.get('outer'), "outer context is still active");
        t.notOk(namespace.get('inner'), "inner context should have been exited by throw");
        t.equal(namespace._set.length, 1, "should be back to outer state");
  
        cls.destroyNamespace('test');
        t.end();
      });
  
      // tap is only trying to help
      process.nextTick(d.bind(function () {
        t.ok(namespace.get('outer'), "outer mutation worked");
        t.notOk(namespace.get('inner'), "inner mutation hasn't happened yet");
  
        namespace.run(function () {
          namespace.set('inner', true);
          throw new Error("explicitly nonlocal exit");
        });
      }));
    });
  });
  
  test("synchronous throw attaches the context", function (t) {
    t.plan(3);
  
    var namespace = cls.createNamespace('cls@synchronous');
    namespace.run(function () {
      namespace.set('value', 'transaction clear');
      try {
        namespace.run(function () {
          namespace.set('value', 'transaction set');
          throw new Error('cls@synchronous explosion');
        });
      }
      catch (e) {
        t.ok(namespace.fromException(e), "context was attached to error");
        t.equal(namespace.fromException(e)['value'], 'transaction set',
                "found the inner value");
      }
  
      t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
    });
  
    cls.destroyNamespace('cls@synchronous');
  });
  
  test("synchronous throw checks if error exists", function (t) {
    t.plan(2);
  
    var namespace = cls.createNamespace('cls@synchronous-null-error');
    namespace.run(function () {
      namespace.set('value', 'transaction clear');
      try {
        namespace.run(function () {
          namespace.set('value', 'transaction set');
          throw null;
        });
      }
      catch (e) {
        // as we had a null error, cls couldn't set the new inner value
        t.equal(namespace.get('value'), 'transaction clear', 'from outer value');
      }
  
      t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
    });
  
    cls.destroyNamespace('cls@synchronous-null-error');
  });
  
  test("throw in process.nextTick attaches the context", function (t) {
    t.plan(3);
  
    var namespace = cls.createNamespace('cls@nexttick');
  
    var d = domain.create();
    d.on('error', function (e) {
      t.ok(namespace.fromException(e), "context was attached to error");
      t.equal(namespace.fromException(e)['value'], 'transaction set',
              "found the inner value");
  
      cls.destroyNamespace('cls@nexttick');
    });
  
    namespace.run(function () {
      namespace.set('value', 'transaction clear');
  
      // tap is only trying to help
      process.nextTick(d.bind(function () {
        namespace.run(function () {
          namespace.set('value', 'transaction set');
          throw new Error("cls@nexttick explosion");
        });
      }));
  
      t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
    });
  });
  
  test("throw in setTimeout attaches the context", function (t) {
    t.plan(3);
  
    var namespace = cls.createNamespace('cls@nexttick');
    var d = domain.create();
  
    d.on('error', function (e) {
      t.ok(namespace.fromException(e), "context was attached to error");
      t.equal(namespace.fromException(e)['value'], 'transaction set',
              "found the inner value");
  
      cls.destroyNamespace('cls@nexttick');
    });
  
    namespace.run(function () {
      namespace.set('value', 'transaction clear');
  
      // tap is only trying to help
      setTimeout(d.bind(function () {
        namespace.run(function () {
          namespace.set('value', 'transaction set');
          throw new Error("cls@nexttick explosion");
        });
      }));
  
      t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
    });
  });