progtest: Do not run tests async on console

Performance hit is too great; automated tests need to be _fast_!

* src/AsyncTestRunner.js: New TestRunner subtype.
* src/TestRunner.js (_runAsync): Rename to virtual protected
  `runAllTests'; this method is overridden by aforementioned subtype.
* src/env.js: Use it.
master v2.11.0
Mike Gerwitz 2018-03-05 16:22:13 -05:00
parent 6cefab5344
commit 0cb43d5e8a
3 changed files with 102 additions and 40 deletions

View File

@ -0,0 +1,74 @@
/**
* Async test case runner
*
* Copyright (C) 2018 R-T Specialty, LLC.
*
* This file is part of TAME.
*
* TAME is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const { Class } = require( 'easejs' );
const TestRunner = require( './TestRunner' );
/**
* Run test cases asynchronously and report results
*
* This allows the browser to repaint between cases.
*/
module.exports = Class( 'TestRunner' )
.extend( TestRunner,
{
/**
* Run all tests asynchronously
*
* TODO: This significantly slows down the runner! The better option
* would be to go back to sync and put it in a Web Worker in the client,
* which would also async updating of the UI.
*
* @param {Array<TestCase>} dfns test case definitions
*
* @return {Promise} promise to complete test cases, yielding results
*/
'override protected runAllTests'( dfns )
{
const total = dfns.length;
return new Promise( ( resolve, reject ) =>
{
const results = [];
const runNext = () =>
{
if ( dfns.length === 0 )
{
resolve( results );
return;
}
const dfn = dfns.shift();
const result = this.runTest( dfn, results.length, total );
results.push( result );
setTimeout( runNext, 0 );
};
runNext();
} );
},
} );

View File

@ -76,49 +76,29 @@ module.exports = Class( 'TestRunner',
this._reporter.preRun( total );
return this._runAsync( dfns ).then(
return this.runAllTests( dfns ).then(
results => ( this._reporter.done( results ), results )
);
},
/**
* Run all tests asynchronously
* Run all tests
*
* TODO: This significantly slows down the runner! The better option
* would be to go back to sync and put it in a Web Worker in the client,
* which would also async updating of the UI.
* This may be overridden by subtypes to change how the tests are run
* (for example, to run each asynchronously).
*
* @param {Array<TestCase>} dfns test case definitions
*
* @return {Promise} promise to complete test cases, yielding results
*/
'private _runAsync'( dfns )
'virtual protected runAllTests'( dfns )
{
const total = dfns.length;
return new Promise( ( resolve, reject ) =>
{
const results = [];
const runNext = () =>
{
if ( dfns.length === 0 )
{
resolve( results );
return;
}
const dfn = dfns.shift();
const result = this._runTest( dfn, results.length, total );
results.push( result );
setTimeout( runNext, 0 );
};
runNext();
} );
return Promise.resolve(
dfns.map( ( dfn, i ) => this.runTest( dfn, ( i + 1 ), total ) )
);
},
@ -131,7 +111,7 @@ module.exports = Class( 'TestRunner',
*
* @return {Object<desc,i,total,failures>} test results
*/
'private _runTest'( { description: desc, data, expect }, test_i, total )
'protected runTest'( { description: desc, data, expect }, test_i, total )
{
// no input map---#rate uses params directly
const result = this._tryRun( data );

View File

@ -26,6 +26,7 @@ const yaml_reader = require( 'js-yaml' );
const {
TestCase,
TestRunner,
AsyncTestRunner,
reader: {
ConstResolver,
@ -41,13 +42,26 @@ const {
module.exports = {
console: ( program, stdout, reporter ) =>
{
const runner = TestRunner(
( reporter || ConsoleTestReporter( stdout ) ),
console: ( program, stdout ) => module.exports.common(
program,
stdout,
TestRunner(
ConsoleTestReporter( stdout ),
program
);
)
),
browser: ( program, stdout ) => module.exports.common(
program,
stdout,
AsyncTestRunner(
ConsoleTestReporter.use( HtmlConsoleOutput )( stdout ),
program
)
),
common: ( program, stdout, runner ) =>
{
const reader = YamlTestReader
.use( DateResolver )
.use( ConstResolver( program ) )
@ -71,10 +85,4 @@ module.exports = {
}
} );
},
browser: ( program, stdout ) => module.exports.console(
program,
stdout,
ConsoleTestReporter.use( HtmlConsoleOutput )( stdout )
),
};