1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// Simple tests of most basic domain functionality.
24
25const common = require('../common');
26const assert = require('assert');
27const domain = require('domain');
28
29process.on('warning', common.mustNotCall());
30
31const d = new domain.Domain();
32
33d.on('error', common.mustCall(function(er) {
34  console.error('caught', er);
35
36  assert.strictEqual(er.domain, d);
37  assert.strictEqual(er.domainThrown, true);
38  assert.ok(!er.domainEmitter);
39  assert.strictEqual(er.actual.code, 'ENOENT');
40  assert.match(er.actual.path, /\bthis file does not exist\b/i);
41  assert.strictEqual(typeof er.actual.errno, 'number');
42}));
43
44
45// Implicit handling of thrown errors while in a domain, via the
46// single entry points of ReqWrap and MakeCallback.  Even if
47// we try very hard to escape, there should be no way to, even if
48// we go many levels deep through timeouts and multiple IO calls.
49// Everything that happens between the domain.enter() and domain.exit()
50// calls will be bound to the domain, even if multiple levels of
51// handles are created.
52d.run(function() {
53  setTimeout(function() {
54    const fs = require('fs');
55    fs.readdir(__dirname, function() {
56      fs.open('this file does not exist', 'r', function(er) {
57        assert.ifError(er);
58        throw new Error('should not get here!');
59      });
60    });
61  }, 100);
62});
63