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'; 23require('../common'); 24const assert = require('assert'); 25const vm = require('vm'); 26 27// Timeout of 100ms executing endless loop 28assert.throws( 29 function() { 30 vm.runInThisContext('while(true) {}', { timeout: 100 }); 31 }, 32 { 33 code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 34 message: 'Script execution timed out after 100ms' 35 }); 36 37// Timeout of 1000ms, script finishes first 38vm.runInThisContext('', { timeout: 1000 }); 39 40// Nested vm timeouts, inner timeout propagates out 41assert.throws( 42 function() { 43 const context = { 44 log: console.log, 45 runInVM: function(timeout) { 46 vm.runInNewContext('while(true) {}', context, { timeout }); 47 } 48 }; 49 vm.runInNewContext('runInVM(10)', context, { timeout: 10000 }); 50 throw new Error('Test 5 failed'); 51 }, 52 { 53 code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 54 message: 'Script execution timed out after 10ms' 55 }); 56 57// Nested vm timeouts, outer timeout is shorter and fires first. 58assert.throws( 59 function() { 60 const context = { 61 runInVM: function(timeout) { 62 vm.runInNewContext('while(true) {}', context, { timeout }); 63 } 64 }; 65 vm.runInNewContext('runInVM(10000)', context, { timeout: 100 }); 66 throw new Error('Test 6 failed'); 67 }, 68 { 69 code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 70 message: 'Script execution timed out after 100ms' 71 }); 72 73// Nested vm timeouts, inner script throws an error. 74assert.throws(function() { 75 const context = { 76 runInVM: function(timeout) { 77 vm.runInNewContext('throw new Error(\'foobar\')', context, { timeout }); 78 } 79 }; 80 vm.runInNewContext('runInVM(10000)', context, { timeout: 100000 }); 81}, /foobar/); 82