isaacs | 0400571 | 2012-09-20 22:13:36 | [diff] [blame^] | 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 | var common = require('../common.js'); |
| 23 | var assert = require('assert'); |
| 24 | var domain = require('domain'); |
| 25 | var disposalFailed = false; |
| 26 | |
| 27 | // no matter what happens, we should increment a 10 times. |
| 28 | var a = 0; |
| 29 | log(); |
| 30 | function log(){ |
| 31 | console.log(a++, process.domain); |
| 32 | if (a < 10) setTimeout(log, 20); |
| 33 | } |
| 34 | |
| 35 | // in 50ms we'll throw an error. |
| 36 | setTimeout(err, 50); |
| 37 | function err(){ |
| 38 | var d = domain.create(); |
| 39 | d.on('error', handle); |
| 40 | d.run(err2); |
| 41 | |
| 42 | function err2() { |
| 43 | // this timeout should never be called, since the domain gets |
| 44 | // disposed when the error happens. |
| 45 | setTimeout(function() { |
| 46 | console.error('This should not happen.'); |
| 47 | disposalFailed = true; |
| 48 | process.exit(1); |
| 49 | }); |
| 50 | |
| 51 | // this function doesn't exist, and throws an error as a result. |
| 52 | err3(); |
| 53 | } |
| 54 | |
| 55 | function handle(e) { |
| 56 | // this should clean up everything properly. |
| 57 | d.dispose(); |
| 58 | console.error(e); |
| 59 | console.error('in handler', process.domain, process.domain === d); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | process.on('exit', function() { |
| 64 | assert.equal(a, 10); |
| 65 | assert.equal(disposalFailed, false); |
| 66 | console.log('ok'); |
| 67 | }); |