From 2ac393070aa2490f7eeba5160fef62dc7eec14e7 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 21 Feb 2017 16:21:18 -0500 Subject: [PATCH] DataValidator: properly chain queue This was not properly chain the promises---it was always chaining on the original _pending (so, the first request) and never clearing _pending after that point. * src/validate/DataValidator.js (_onceReady): Properly chain. * test/validate/DataValidatorTest.js: Updated test. Don't ask. --- src/validate/DataValidator.js | 6 +++-- test/validate/DataValidatorTest.js | 36 ++++++++++++++++-------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/validate/DataValidator.js b/src/validate/DataValidator.js index 56ae480..1a52c89 100644 --- a/src/validate/DataValidator.js +++ b/src/validate/DataValidator.js @@ -227,8 +227,10 @@ module.exports = Class( 'DataValidator', { if ( this._pending ) { - this._pending.then( callback ); - return this._pending; + // we become the end of the chain + return this._pending = this._pending.then( () => + this._onceReady( callback ) + ); } return this._pending = callback() diff --git a/test/validate/DataValidatorTest.js b/test/validate/DataValidatorTest.js index ed9a932..7bf5426 100644 --- a/test/validate/DataValidatorTest.js +++ b/test/validate/DataValidatorTest.js @@ -325,32 +325,34 @@ describe( 'DataValidator', () => } ); const { bstore } = getStore(); - const faila = {}; - const failb = {}; + // linked list (to previous) + const fails = [ {}, {}, {} ]; + fails.forEach( ( fail, i ) => fail.prev = fails[ i - 1 ] ); - let running_first = true; + const failcalls = []; vmonitor.update = ( _, fail ) => { - if ( fail === failb ) - { - if ( running_first === true ) - { - return Promise.reject( Error( - "Request not queued" - ) ); - } - } - + failcalls.push( fail ); return Promise.resolve( true ); }; return Promise.all( [ - sut.updateFailures( {}, faila ) - .then( () => running_first = false ), + sut.updateFailures( {}, fails[ 0 ] ) + .then( () => expect( failcalls[ 0 ] ).to.equal( fails[ 0 ] ) ), - sut.updateFailures( {}, failb ), - ] ); + sut.updateFailures( {}, fails[ 1 ] ) + .then( () => expect( failcalls[ 1 ] ).to.equal( fails[ 1 ] ) ), + + sut.updateFailures( {}, fails[ 2 ] ) + .then( () => expect( failcalls[ 2 ] ).to.equal( fails[ 2 ] ) ), + ] ) + .then( () => { + // sanity check to make sure the above stuff was + // actually called + expect( failcalls.length ) + .to.equal( fails.length ) + } ); } ); } );