diff --git a/lib/sandbox.js b/lib/sandbox.js index 314f606..9efa501 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -21,7 +21,7 @@ function Sandbox(options) { self._message_queue = []; self.options = { - timeout: 500, + timeout: options && options.timeout !== undefined ? options.timeout : 500, node: 'node', shovel: path.join(__dirname, 'shovel.js') }; @@ -96,12 +96,14 @@ Sandbox.prototype.run = function(code, hollaback) { self.child.stdin.write(code); self.child.stdin.end(); - self.child.timer = setTimeout(function() { - this.parent.stdout.removeListener('output', output); - stdout = JSON.stringify({ result: 'TimeoutError', console: [] }); - this.parent.kill('SIGKILL'); - }, self.options.timeout); - self.child.timer.parent = self.child; + if(self.options.timeout){ + self.child.timer = setTimeout(function() { + this.parent.stdout.removeListener('output', output); + stdout = JSON.stringify({ result: 'TimeoutError', console: [] }); + this.parent.kill('SIGKILL'); + }, self.options.timeout); + self.child.timer.parent = self.child; + } }; diff --git a/test/sandbox.js b/test/sandbox.js index 73ca174..1a31830 100644 --- a/test/sandbox.js +++ b/test/sandbox.js @@ -116,4 +116,24 @@ describe('Sandbox', function() { }); }); -}); \ No newline at end of file + it('should keep the process alive when timeout is explicitly disabled', function(done){ + sb = new Sandbox({timeout : null}); //disable timeout + var messageHandler = sinon.spy(); + var messageHandler2 = sinon.spy(); + var num_messages_sent = 0; + var interval = setInterval(function(){ + sb.postMessage(++num_messages_sent); + }, 100); + sb.on('message', messageHandler); + sb.run('onmessage = function (msg) { postMessage(msg); };', messageHandler2); + setTimeout(function(){ + messageHandler.callCount.should.eql(num_messages_sent); + num_messages_sent.should.be.greaterThan(0); + + messageHandler2.callCount.should.eql(0); //messageHandler2 should never be called + clearInterval(interval); + done(); + },1000); + }); + +});