From 234f2a1585d946e941ac9bd43d88273a657b1d16 Mon Sep 17 00:00:00 2001 From: "Davis W. Frank & Rajan Agaskar" Date: Fri, 7 Dec 2012 12:26:05 -0800 Subject: [PATCH] Reintroduce fn that clears stack occasionally - setTimeout will clear stack, prevent overflow. We run this once every thousand specs. - Browser users will probably want a time-based clear rather than spec count based clear, as a thousand tests is typically quite slow. The reporter should provide this. --- lib/jasmine-core/jasmine.js | 21 ++++++++++++++++++--- spec/node_suite.js | 1 + src/core/Env.js | 21 ++++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 87401787..49a7dc06 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -524,6 +524,8 @@ jasmine.buildExpectationResult = function(params) { var self = this; var global = options.global || jasmine.getGlobal(); + var encourageGC = options.encourageGarbageCollection || encourageGarbageCollection; + this.clock = new jasmine.Clock(global, new jasmine.DelayedFunctionScheduler()); var suiteConstructor = jasmine.Suite; @@ -539,8 +541,6 @@ jasmine.buildExpectationResult = function(params) { this.reporter = new jasmine.MultiReporter(); - this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL; - this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; this.lastUpdate = 0; this.specFilter = function() { return true; @@ -627,8 +627,11 @@ jasmine.buildExpectationResult = function(params) { function specResultCallback(result) { self.clock.uninstall(); self.currentSpec = null; - suite.specComplete(result); + encourageGC(function() { + suite.specComplete(result); + }); } + }; var queueConstructor = jasmine.Queue; @@ -639,6 +642,18 @@ jasmine.buildExpectationResult = function(params) { return new suiteConstructor(self, description, specDefinitions, self.currentSuite, queueFactory, isSuite); }; + var maximumSpecCallbackDepth = 100; + var currentSpecCallbackDepth = 0; + function encourageGarbageCollection(fn) { + currentSpecCallbackDepth++; + if (currentSpecCallbackDepth > maximumSpecCallbackDepth) { + currentSpecCallbackDepth = 0; + global.setTimeout(fn, 0); + } else { + fn(); + } + + } }; //TODO: shim Spec addMatchers behavior into Env. Should be rewritten to remove globals, etc. diff --git a/spec/node_suite.js b/spec/node_suite.js index 09f6337d..5ae018fe 100644 --- a/spec/node_suite.js +++ b/spec/node_suite.js @@ -159,6 +159,7 @@ process.argv.forEach(function(arg) { } }); +// var specs = jasmine.getAllSpecFiles(__dirname + '/smoke', new RegExp("test.js$")); var specs = jasmine.getAllSpecFiles(__dirname, new RegExp("Spec.js$")); var domIndependentSpecs = []; for (var i = 0; i < specs.length; i++) { diff --git a/src/core/Env.js b/src/core/Env.js index 955a3384..1b01a40f 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -9,6 +9,8 @@ var self = this; var global = options.global || jasmine.getGlobal(); + var encourageGC = options.encourageGarbageCollection || encourageGarbageCollection; + this.clock = new jasmine.Clock(global, new jasmine.DelayedFunctionScheduler()); var suiteConstructor = jasmine.Suite; @@ -24,8 +26,6 @@ this.reporter = new jasmine.MultiReporter(); - this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL; - this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; this.lastUpdate = 0; this.specFilter = function() { return true; @@ -112,8 +112,11 @@ function specResultCallback(result) { self.clock.uninstall(); self.currentSpec = null; - suite.specComplete(result); + encourageGC(function() { + suite.specComplete(result); + }); } + }; var queueConstructor = jasmine.Queue; @@ -124,6 +127,18 @@ return new suiteConstructor(self, description, specDefinitions, self.currentSuite, queueFactory, isSuite); }; + var maximumSpecCallbackDepth = 100; + var currentSpecCallbackDepth = 0; + function encourageGarbageCollection(fn) { + currentSpecCallbackDepth++; + if (currentSpecCallbackDepth > maximumSpecCallbackDepth) { + currentSpecCallbackDepth = 0; + global.setTimeout(fn, 0); + } else { + fn(); + } + + } }; //TODO: shim Spec addMatchers behavior into Env. Should be rewritten to remove globals, etc.