Skip to content

Commit

Permalink
Destroy test environment when test is done and file syncing switched …
Browse files Browse the repository at this point in the history
…off (fixes #10)
  • Loading branch information
wuzyk authored and lahmatiy committed Sep 27, 2016
1 parent f31333b commit 9ef1c84
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function findTest(test, filename){
}
}

function loadTests(data, autorun, notifyLoaderFunction){
function loadTests(data, autorun, notifyLoaderFunction, fileSync){
if (Array.isArray(data))
data = { test: data };

Expand All @@ -34,6 +34,8 @@ function loadTests(data, autorun, notifyLoaderFunction){
if (typeof notifyLoaderFunction == 'function')
notifyLoader = notifyLoaderFunction;

runner.fileSync.set(!!fileSync);

if (autorun)
setTimeout(function(){
runner.run();
Expand Down Expand Up @@ -152,7 +154,7 @@ else
/** @cut */ var testLoadTime = benchmark.time();

contentWindow.loadTests(function(data, feedback){
loadTests(data, params.autorun, feedback);
loadTests(data, params.autorun, feedback, params.fileSync);
});

/** @cut */basis.dev.info('Timing:\n' +
Expand Down
37 changes: 33 additions & 4 deletions src/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var DataObject = require('basis.data').Object;
var Dataset = require('basis.data').Dataset;
var Expression = require('basis.data.value').Expression;
var Subset = require('basis.data.dataset').Filter;
var Extract = require('basis.data.dataset').Extract;
var Slice = require('basis.data.dataset').Slice;
var count = require('basis.data.index').count;
var sum = require('basis.data.index').sum;
Expand All @@ -12,6 +13,7 @@ var TestCase = require('./test.js').TestCase;
var TestSuite = require('./test.js').TestSuite;
var createTest = require('./test.js').create;

var fileSync = new Value({ value: false });
var runnerState = new basis.Token('stopped');
var notifier = new basis.Token();
var elapsedTime = new Value({ value: 0 });
Expand Down Expand Up @@ -111,6 +113,35 @@ var processingQueueTop = new Slice({
}
});

// destroy env when test case/suite is done
new Subset({
source: new Extract({
source: fileSync.as(function(value){
return value ? null : testsToRun;
}),
rule: function(test){
return test.parentNode || test;
}
}),
ruleEvents: 'stateChanged',
rule: function(test){
return test.state == STATE.ERROR || test.state == STATE.READY;
},
handler: {
itemsChanged: function(sender, delta){
if (delta.inserted)
delta.inserted.forEach(function(test){
if (test.hasOwnEnvironment())
{
var env = test.getEnv();
if (env)
env.destroy();
}
});
}
}
});

var assertCount = sum(testsToRun, 'stateChanged', function(test){
if (test.state.data instanceof DataObject)
return test.state.data.data.testCount;
Expand Down Expand Up @@ -194,10 +225,7 @@ function run(){

// reset test state
testsToRun.forEach(function(item){
// destroy environment
var env = item.getEnv();
if (env)
env.destroy();
item.reset();
});

// add test to processing queue
Expand Down Expand Up @@ -228,6 +256,7 @@ basis.config.runnerBaseURI = '';
// exports
//
module.exports = {
fileSync: fileSync,
state: runnerState,
time: elapsedTime,
doneTests: doneTests,
Expand Down
19 changes: 8 additions & 11 deletions src/runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,26 @@ var AbstractTest = DomWrapperNode.subclass({
{
this.scope = scope;
this.env = scope.env;
return scope;
break;
}
}
}

if (autocreate)
if (!this.scope && autocreate)
{
this.env = envFactory.get(this.getHtml(), this.data.init && !this.data.sandbox);
this.scope = this.env.createScope(this.data.init);
}

if (this.env)
{
this.env.addHandler({
fileChange: function(){
this.reset();
},
destroy: function(){
this.env = null;
this.scope = null;
this.reset();
}
}, this);
}
Expand All @@ -139,22 +144,14 @@ var AbstractTest = DomWrapperNode.subclass({
},
reset: function(){
if (this.env)
{
this.env.destroy();
this.env = null;
this.scope = null;
}
},

destroy: function(){
DomWrapperNode.prototype.destroy.call(this);

if (this.env)
{
this.env.destroy();
this.env = null;
this.scope = null;
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/runner/test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,10 @@ module.exports = function createAssert(scope, testCode, settings){

try {
asyncQueue.shift().call();
__processAsync();
} catch(e) {
__exception(e);
}

__processAsync();
}, 0);
}
else if (async === 0)
Expand All @@ -232,6 +231,7 @@ module.exports = function createAssert(scope, testCode, settings){
testDone();
};


var asyncDone = async
? basis.fn.runOnce(__asyncDone)
: function(){};
Expand Down
18 changes: 14 additions & 4 deletions src/runner/test/env/iframe.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var event = require('basis.event');
var Emitter = require('basis.event').Emitter;
var fnInfo = require('../source/info.js');

Expand All @@ -24,6 +25,8 @@ iframeProto.setAttribute('style', [
].join(';'));

var Scope = Emitter.subclass({
className: 'Scope',

env: null,
initCode: '',
runInScope: null,
Expand Down Expand Up @@ -68,14 +71,19 @@ var Scope = Emitter.subclass({
this.env = null;
this.runArgs = null;
this.runInScope = null;
this.initCode = null;
}
});

var FrameEnv = Emitter.subclass({
className: 'FrameEnv',

html: null,

scopeClass: Scope,

emit_fileChange: event.create('fileChange'),

init: function(){
Emitter.prototype.init.call(this);

Expand All @@ -96,10 +104,8 @@ var FrameEnv = Emitter.subclass({

runInContext(frameWindow, require('./iframe_inject.code'));

this.createScope_ = frameWindow.__initTestEnvironment(function(){
// env deprecates
this.destroy();
}.bind(this));
this.createScope_ = frameWindow.__initTestEnvironment(this.emit_fileChange.bind(this));

this.scopeClass.extend({
Array: frameWindow.Array,
setTimeout: wrapToRunInContext(frameWindow.setTimeout, frameWindow),
Expand Down Expand Up @@ -132,11 +138,15 @@ var FrameEnv = Emitter.subclass({
this.element.parentNode.removeChild(this.element);
this.element = null;

basis.object.splice(this.scopeClass.prototype, ['Array', 'setTimeout', 'clearTimeout']);

this.createScope_ = null;
this.scopes.forEach(function(scope){
scope.destroy();
});
this.scopes = null;

this.html = null;
}
});

Expand Down

0 comments on commit 9ef1c84

Please sign in to comment.