diff --git a/src/auction.js b/src/auction.js index 881dee9f2de..c7780081d1b 100644 --- a/src/auction.js +++ b/src/auction.js @@ -163,11 +163,17 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a let _nonBids = []; function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests); } - function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); } + function addBidReceived(bidResponse) { _bidsReceived = _bidsReceived.concat(bidResponse); purgeBidOnExpiration(bidResponse); } function addBidRejected(bidsRejected) { _bidsRejected = _bidsRejected.concat(bidsRejected); } function addNoBid(noBid) { _noBids = _noBids.concat(noBid); } function addNonBids(seatnonbids) { _nonBids = _nonBids.concat(seatnonbids); } + function purgeBidOnExpiration(bidResponse) { + setTimeout(() => { + _bidsReceived = _bidsReceived.filter(bid => bid.adId !== bidResponse.adId); + }, bidResponse.ttl * 1000); + } + function getProperties() { return { auctionId: _auctionId, diff --git a/test/spec/auctionmanager_spec.js b/test/spec/auctionmanager_spec.js index e5cdb66e75f..f9d6e0076ee 100644 --- a/test/spec/auctionmanager_spec.js +++ b/test/spec/auctionmanager_spec.js @@ -852,16 +852,40 @@ describe('auctionmanager.js', function () { config.resetConfig(); }); - it('are dropped after their last bid becomes stale (if minBidCacheTTL is set)', () => { + it('are dropped after `minBidCacheTTL` seconds if they had no bid', () => { + auction.callBids(); config.setConfig({ - minBidCacheTTL: 0 + minBidCacheTTL: 2 }); + return auction.end.then(() => { + expect(auctionManager.getNoBids().length).to.eql(1); + clock.tick(10 * 10000); + expect(auctionManager.getNoBids().length).to.eql(0); + }) + }); + }); + + describe('stale bids', () => { + let clock, auction; + beforeEach(() => { + clock = sinon.useFakeTimers(); + auction = auctionManager.createAuction({adUnits}); + indexAuctions.push(auction); + }); + afterEach(() => { + clock.restore(); + config.resetConfig(); + }); + + it('are dropped after they expire', () => { bids = [ { + adId: '1', adUnitCode: ADUNIT_CODE, adUnitId: ADUNIT_CODE, ttl: 10 }, { + adId: '2', adUnitCode: ADUNIT_CODE, adUnitId: ADUNIT_CODE, ttl: 100 @@ -869,49 +893,13 @@ describe('auctionmanager.js', function () { ]; auction.callBids(); return auction.end.then(() => { - clock.tick(50 * 1000); - expect(auctionManager.getBidsReceived().length).to.equal(2); - clock.tick(56 * 1000); + clock.tick(10 * 1000); + expect(auctionManager.getBidsReceived().length).to.equal(1); + clock.tick(90 * 1000); expect(auctionManager.getBidsReceived()).to.eql([]); }); }); - - it('are dropped after `minBidCacheTTL` seconds if they had no bid', () => { - auction.callBids(); - config.setConfig({ - minBidCacheTTL: 2 - }); - return auction.end.then(() => { - expect(auctionManager.getNoBids().length).to.eql(1); - clock.tick(10 * 10000); - expect(auctionManager.getNoBids().length).to.eql(0); - }) - }); - - Object.entries({ - 'bids': { - bd: [{ - adUnitCode: ADUNIT_CODE, - adUnitId: ADUNIT_CODE, - ttl: 10 - }], - entries: () => auctionManager.getBidsReceived() - }, - 'no bids': { - bd: [], - entries: () => auctionManager.getNoBids() - } - }).forEach(([t, {bd, entries}]) => { - it(`with ${t} are never dropped if minBidCacheTTL is not set`, () => { - bids = bd; - auction.callBids(); - return auction.end.then(() => { - clock.tick(100 * 1000); - expect(entries().length > 0).to.be.true; - }) - }) - }); - }) + }); }); describe('addBidResponse #1', function () {