blob: 90e7210ce21aea119f10c28039aa42aae5eaee5c [file] [log] [blame]
Roman Reissf29762f2015-05-19 11:00:061'use strict';
Brendan Ashworth0df54302015-02-22 02:47:042var common = require('../common');
isaacs04005712012-09-20 22:13:363var assert = require('assert');
4var domain = require('domain');
5var disposalFailed = false;
6
7// no matter what happens, we should increment a 10 times.
8var a = 0;
9log();
Roman Reissf29762f2015-05-19 11:00:0610function log() {
isaacs04005712012-09-20 22:13:3611 console.log(a++, process.domain);
12 if (a < 10) setTimeout(log, 20);
13}
14
15// in 50ms we'll throw an error.
16setTimeout(err, 50);
Roman Reissf29762f2015-05-19 11:00:0617function err() {
isaacs04005712012-09-20 22:13:3618 var d = domain.create();
19 d.on('error', handle);
20 d.run(err2);
21
22 function err2() {
23 // this timeout should never be called, since the domain gets
24 // disposed when the error happens.
25 setTimeout(function() {
26 console.error('This should not happen.');
27 disposalFailed = true;
28 process.exit(1);
29 });
30
31 // this function doesn't exist, and throws an error as a result.
32 err3();
33 }
34
35 function handle(e) {
36 // this should clean up everything properly.
37 d.dispose();
38 console.error(e);
39 console.error('in handler', process.domain, process.domain === d);
40 }
41}
42
43process.on('exit', function() {
44 assert.equal(a, 10);
45 assert.equal(disposalFailed, false);
46 console.log('ok');
47});