Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy test environment when test is done and file syncing switched off (fixes #10) #12

Merged
merged 5 commits into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you move it to try clause? In that case on call() exception async queue will never be processed and test never be done.

Copy link
Member Author

@wuzyk wuzyk Sep 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On exception test gets done in __exception call in catch block . If call __processAsync() after try/catch clause testDone is called twice.

} 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