Skip to content

Commit

Permalink
prevent concurrent bid requests
Browse files Browse the repository at this point in the history
If an auction is running don't accept a new request for bids..
This adds a `clearAuction` function to set `auctionRunning` false and to clear `_bidsRequested` and `_bidsReceived`. This is a stopgap measure util #353
  • Loading branch information
protonate committed May 27, 2016
1 parent 83ea6e9 commit 2da8d84
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var eventValidators = {

/* Public vars */

pbjs._auctionRunning = false;
pbjs._bidsRequested = [];
pbjs._bidsReceived = [];

Expand Down Expand Up @@ -387,6 +388,13 @@ pbjs.removeAdUnit = function (adUnitCode) {
}
};

pbjs.clearAuction = function() {
pbjs._bidsRequested = [];
pbjs._bidsReceived = [];
pbjs._auctionRunning = false;
utils.logMessage('Prebid auction cleared');
};

/**
*
* @param bidsBackHandler
Expand All @@ -395,6 +403,14 @@ pbjs.removeAdUnit = function (adUnitCode) {
pbjs.requestBids = function ({ bidsBackHandler, timeout }) {
const cbTimeout = timeout || pbjs.bidderTimeout;

if (pbjs._auctionRunning) {
utils.logError('Prebid Error: `pbjs.requestBids` was called while a previous auction was' +
' still running. Resubmit this request.');
return;
} else {
pbjs._auctionRunning = true;
}

if (typeof bidsBackHandler === objectType_function) {
bidmanager.addOneTimeCallback(bidsBackHandler);
}
Expand Down
10 changes: 10 additions & 0 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pbjs = pbjs || {};
pbjs._bidsRequested = getBidRequests();
pbjs._bidsReceived = getBidResponses();

function resetAuction() {
pbjs.clearAuction();
pbjs._bidsRequested = getBidRequests();
pbjs._bidsReceived = getBidResponses();
}

var Slot = function Slot(elementId, pathId) {
var slot = {
getSlotElementId: function getSlotElementId() {
Expand Down Expand Up @@ -282,6 +288,7 @@ describe('Unit: Prebid Module', function () {
assert.ok(spyAddOneTimeCallBack.calledWith(requestObj.bidsBackHandler),
'called bidmanager.addOneTimeCallback');
bidmanager.addOneTimeCallback.restore();
resetAuction();
});

it('should log message when adUnits not configured', () => {
Expand All @@ -294,6 +301,7 @@ describe('Unit: Prebid Module', function () {
assert.ok(logMessageSpy.calledWith('No adUnits configured. No bids requested.'), 'expected message was logged');
utils.logMessage.restore();
pbjs.adUnits = adUnitsBackup;
resetAuction();
});

it('should execute callback after timeout', () => {
Expand All @@ -314,13 +322,15 @@ describe('Unit: Prebid Module', function () {

bidmanager.executeCallback.restore();
clock.restore();
resetAuction();
});

it('should call callBids function on adaptermanager', () => {
var spyCallBids = sinon.spy(adaptermanager, 'callBids');
pbjs.requestBids({});
assert.ok(spyCallBids.called, 'called adaptermanager.callBids');
adaptermanager.callBids.restore();
resetAuction();
});
});

Expand Down

0 comments on commit 2da8d84

Please sign in to comment.