Skip to content

Commit

Permalink
TESTBOX-221 #resolve
Browse files Browse the repository at this point in the history
Complete refactoring of around,before,after each executions to support concurrency
  • Loading branch information
lmajano committed Apr 13, 2018
1 parent 9fcda48 commit dd88b07
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 78 deletions.
120 changes: 62 additions & 58 deletions system/BaseSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -759,29 +759,29 @@ component{
parentSuite = parentSuite.parentRef;
}

var annotationMethods = this.$utility.getAnnotatedMethods(
// Incorporate annotated methods
this.$utility.getAnnotatedMethods(
annotation = "beforeEach",
metadata = getMetadata( this )
);

for( var method in annotationMethods ){
arrayAppend( reverseTree, {
beforeEach = this[ method.name ],
).each( function( item ){
reverseTree.append( {
beforeEach = this[ arguments.item.name ],
beforeEachData = {}
} );
}

// Execute reverse tree
var treeLen = arrayLen( reverseTree );
if( treeLen gt 0 ){
for( var x=treeLen; x gte 1; x-- ){
var thisContext = reverseTree[ x ];
thisContext.beforeEach(
currentSpec = arguments.spec.name,
data = thisContext.beforeEachData
);
}
}
} );

// sort tree backwards
reverseTree.sort( function( a, b ){
return -1;
} );

// Execute it
reverseTree.each( function( item ){
item.beforeEach(
currentSpec = spec.name,
data = item.beforeEachData
);
});

// execute beforeEach()
arguments.suite.beforeEach(
Expand Down Expand Up @@ -812,50 +812,49 @@ component{
// do we have nested suites? If so, traverse the tree to build reverse execution map
var parentSuite = arguments.suite.parentRef;
while( !isSimpleValue( parentSuite ) ){
arrayAppend( reverseTree, {
reverseTree.append( {
name = parentSuite.name,
body = parentSuite.aroundEach,
data = parentSuite.aroundEachData,
labels = parentSuite.labels,
order = 0,
skip = parentSuite.skip
} );
} );
// go deep
parentSuite = parentSuite.parentRef;
}

var annotationMethods = this.$utility.getAnnotatedMethods(
annotatedMethods = this.$utility.getAnnotatedMethods(
annotation = "aroundEach",
metadata = getMetadata( this )
);

for( var method in annotationMethods ){
arrayAppend( reverseTree, {
name = method.name,
body = this[ method.name ],
// Discover annotated methods and add to reverseTree
this.$utility.getAnnotatedMethods(
annotation = "aroundEach",
metadata = getMetadata( this )
).each( function( item ){
reverseTree.append( {
name = arguments.item.name,
body = this[ arguments.item.name ],
data = {},
labels = {},
order = 0,
skip = false
} );
}
} );

// Sort the closures from the oldest parent down to the current spec
var correctOrderTree = [];
var treeLen = arrayLen( reverseTree );
if( treeLen gt 0 ){
for( var x = treeLen; x gte 1; x-- ){
arrayAppend( correctOrderTree, reverseTree[ x ] );
}
}
reverseTree.sort( function( a, b ){
return -1;
} );

// Build a function that will execute down the tree
var specStack = generateAroundEachClosuresStack(
closures = correctOrderTree,
closures = reverseTree,
suite = arguments.suite,
spec = arguments.spec
);

// Run the specs
specStack();

Expand All @@ -868,18 +867,17 @@ component{
* @suite The target suite
* @spec The target spec
*/
function generateAroundEachClosuresStack( array closures, required suite, required spec ) {
function generateAroundEachClosuresStack( array closures, required suite, required spec, closureIndex=1 ) {

thread.closures = arguments.closures;
thread.suite = arguments.suite;
thread.spec = arguments.spec;
thread.closures = arguments.closures;
thread.suite = arguments.suite;
thread.spec = arguments.spec;

// Get closure data from stack and pop it
var nextClosure = thread.closures[ 1 ];
arrayDeleteAt( thread.closures, 1 );
var nextClosure = thread.closures[ closureIndex ];

// Check if we have more in the stack or empty
if( arrayLen( thread.closures ) == 0 ){
if( thread.closures.len() == closureIndex ){
// Return the closure of execution for a single spec ONLY
return function(){
// Execute the body of the spec
Expand All @@ -888,23 +886,25 @@ component{
}

// Get next Spec in stack
var nextSpecInfo = thread.closures[ 1 ];
var nextSpecInfo = thread.closures[ ++closureIndex ];
// Return generated closure
return function() {
nextClosure.body(
{
name = nextSpecInfo.name,
body = generateAroundEachClosuresStack(
spec = {
name = nextSpecInfo.name,
body = generateAroundEachClosuresStack(
thread.closures,
thread.suite,
thread.spec
thread.spec,
closureIndex
),
data = nextSpecInfo.data,
data = nextSpecInfo.data,
labels = nextSpecInfo.labels,
order = nextSpecInfo.order,
skip = nextSpecInfo.skip
order = nextSpecInfo.order,
skip = nextSpecInfo.skip
},
thread.suite
suite = thread.suite,
data = nextClosure.data
);
};
}
Expand Down Expand Up @@ -934,12 +934,16 @@ component{
var annotationMethods = this.$utility.getAnnotatedMethods(
annotation = "afterEach",
metadata = getMetadata( this )
);

for( var method in annotationMethods ){
var afterEachMethod = this[ method.name ];
afterEachMethod( currentSpec = arguments.spec.name, data = {} );
}
).each( function( item ){
invoke(
this,
item.name,
{
currentSpec = spec.name,
data = {}
}
);
} );

return this;
}
Expand Down
3 changes: 1 addition & 2 deletions system/runners/BDDRunner.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ component extends="testbox.system.runners.BaseRunner" implements="testbox.system
}

// Iterate over found test suites and test them, if nested suites, then this will recurse as well.
//writeDump( var=testSuites );abort;
for( var thisSuite in testSuites ){
// verify call backs
if( structKeyExists( arguments.callbacks, "onSuiteStart" ) ){
arguments.callbacks.onSuiteStart( arguments.target, arguments.testResults, thisSuite );
}

// Test Suite
testSuite(
target=arguments.target,
Expand Down Expand Up @@ -171,6 +169,7 @@ component extends="testbox.system.runners.BaseRunner" implements="testbox.system
thread.testResults = arguments.testResults;
thread.suiteStats = suiteStats;
thread.target = arguments.target;

// iterate over suite specs and test them
for( var thisSpec in arguments.suite.specs ){

Expand Down
26 changes: 13 additions & 13 deletions system/util/Util.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,23 @@ component{
) {
var lifecycleMethods = [];

if( StructKeyExists( arguments.metadata, "functions" ) ){
var funcs = arguments.metadata.functions;
for ( var func in funcs ){
if ( StructKeyExists( func, annotation ) ){
ArrayAppend( lifecycleMethods, func );
if( structKeyExists( arguments.metadata, "functions" ) ){

for( var thisFunction in arguments.metadata.functions ){
if( structKeyExists( thisFunction, annotation ) ){
lifecycleMethods.append( thisFunction );
}
}

}

if( StructKeyExists( arguments.metadata, "extends" ) ){
// recursively call up the inheritance chain
lifecycleMethods.addAll(
getAnnotatedMethods(
arguments.annotation,
arguments.metadata.extends
)
);
if( structKeyExists( arguments.metadata, "extends" ) ){
getAnnotatedMethods(
arguments.annotation,
arguments.metadata.extends
).each( function( item ){
lifecycleMethods.append( arguments.item );
} );
}

return lifecycleMethods;
Expand Down
1 change: 1 addition & 0 deletions tests/specs/BDDLifecycleAnnotationsTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ component extends="tests.utils.ExampleParentTestCase"{

function beforeAll(){
variables.counter = 0;
// After annotated class this should be 1
}

function afterAll(){
Expand Down
10 changes: 6 additions & 4 deletions tests/specs/BDDLifecycleTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ component extends="testbox.system.BaseSpec"{
// before each spec in THIS suite group
beforeEach(function( currentSpec ){
coldbox++;
debug( "beforeEach #arguments.currentSpec#: coldbox = #coldbox#" );
debug( "beforeEach (A Suite) #arguments.currentSpec#" );
});

// after each spec in THIS suite group
afterEach(function( currentSpec ){
debug( "afterEach #arguments.currentSpec#: coldbox = #coldbox#" );
debug( "afterEach (A Suite) #arguments.currentSpec#: coldbox = #coldbox#" );
});

// around each spec in THIS suite group
aroundEach(function( spec ){
debug( "aroundEach (A Suite) #arguments.spec.name#" );
// execute the spec manually now, we can decorate things here too.
spec.body();
});
Expand All @@ -45,11 +46,12 @@ component extends="testbox.system.BaseSpec"{
// before each spec in THIS suite group
beforeEach(function( currentSpec ){
coldbox = coldbox * 2;
debug( "beforeEach #arguments.currentSpec#: coldbox = #coldbox#" );
debug( "beforeEach (A nested suite) #arguments.currentSpec#: coldbox = #coldbox#" );
});

// around each spec in THIS suite group
aroundEach(function( spec ){
debug( "aroundEach (A nested suite) #arguments.spec.name#" );
// execute the spec manually now, we can decorate things here too.
spec.body();
});
Expand All @@ -62,7 +64,7 @@ component extends="testbox.system.BaseSpec"{
// before each spec in THIS suite group
beforeEach(function( currentSpec ){
coldbox++;
debug( "beforeEach #arguments.currentSpec#: coldbox = #coldbox#" );
debug( "beforeEach (Another nested suite) #arguments.currentSpec#: coldbox = #coldbox#" );
});

it( "before should be 11", function(){
Expand Down
4 changes: 3 additions & 1 deletion tests/specs/NestedDescribeTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ component extends="testbox.system.BaseSpec"{
{ name="Avalon", Cruise_ID="767012", CrzCode="AV", API_Sail_ID="WDB091010APS", API_Length=7, API_Port_Count=9, API_Sail_Date="10/10/2019" },
{ name="Azamara", Cruise_ID="721541", CrzCode="AZ", API_Sail_ID="JR01191027JR07M398", API_Length=7, API_Port_Count=7, API_Sail_Date="10/27/2019" },
{ name="Celebrity", Cruise_ID="708821", CrzCode="CB", API_Sail_ID="SL01190117SL14K095", API_Length=14, API_Port_Count=9, API_Sail_Date="01/17/2019" }
].each( function( item ){
];

sailingsData.each( function( item ){

given( "[#item.name#] valid incoming arguments for a #item.name# sailing", function(){
then( then="[#item.name#] I get a response with inErrorStatus=false", data=item, body=function( data ){
Expand Down

0 comments on commit dd88b07

Please sign in to comment.