Skip to content

Commit

Permalink
Fix prebid#3813 move auctionEnd events so it always executes when auc…
Browse files Browse the repository at this point in the history
…tion completes (prebid#3841)

* move auctionEnd events so it always executes

* remove some commented code
  • Loading branch information
jsnellbaker authored and Alex committed Aug 1, 2019
1 parent cd411d5 commit d1b75c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
clearTimeout(_timer);
}

if (_callback != null) {
if (_auctionEnd === undefined) {
let timedOutBidders = [];
if (timedOut) {
utils.logMessage(`Auction ${_auctionId} timedOut`);
Expand All @@ -150,17 +150,19 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
}
}

try {
_auctionStatus = AUCTION_COMPLETED;
_auctionEnd = Date.now();

events.emit(CONSTANTS.EVENTS.AUCTION_END, getProperties());
_auctionStatus = AUCTION_COMPLETED;
_auctionEnd = Date.now();

const adUnitCodes = _adUnitCodes;
const bids = _bidsReceived
.filter(utils.bind.call(adUnitsFilter, this, adUnitCodes))
.reduce(groupByPlacement, {});
_callback.apply($$PREBID_GLOBAL$$, [bids, timedOut]);
events.emit(CONSTANTS.EVENTS.AUCTION_END, getProperties());
try {
if (_callback != null) {
const adUnitCodes = _adUnitCodes;
const bids = _bidsReceived
.filter(utils.bind.call(adUnitsFilter, this, adUnitCodes))
.reduce(groupByPlacement, {});
_callback.apply($$PREBID_GLOBAL$$, [bids, timedOut]);
_callback = null;
}
} catch (e) {
utils.logError('Error executing bidsBackHandler', null, e);
} finally {
Expand All @@ -175,7 +177,6 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
syncUsers(userSyncConfig.syncDelay);
}
}
_callback = null;
}
}

Expand Down
11 changes: 9 additions & 2 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getKeyValueTargetingPairs, auctionCallbacks } from 'src/auction';
import { getKeyValueTargetingPairs, auctionCallbacks, AUCTION_COMPLETED } from 'src/auction';
import CONSTANTS from 'src/constants.json';
import { adjustBids } from 'src/auction';
import * as auctionModule from 'src/auction';
Expand Down Expand Up @@ -783,7 +783,8 @@ describe('auctionmanager.js', function () {
server.restore();
events.emit.restore();
});
it('should emit BID_TIMEOUT for timed out bids', function (done) {

it('should emit BID_TIMEOUT and AUCTION_END for timed out bids', function (done) {
const spec1 = mockBidder(BIDDER_CODE, [bids[0]]);
registerBidder(spec1);
const spec2 = mockBidder(BIDDER_CODE1, [bids[1]]);
Expand All @@ -797,6 +798,12 @@ describe('auctionmanager.js', function () {
const timedOutBids = bidTimeoutCall.args[1];
assert.equal(timedOutBids.length, 1);
assert.equal(timedOutBids[0].bidder, BIDDER_CODE1);

const auctionEndCall = eventsEmitSpy.withArgs(CONSTANTS.EVENTS.AUCTION_END).getCalls()[0];
const auctionProps = auctionEndCall.args[1];
assert.equal(auctionProps.adUnits, adUnits);
assert.equal(auctionProps.timeout, 20);
assert.equal(auctionProps.auctionStatus, AUCTION_COMPLETED)
done();
}
auction = auctionModule.newAuction({adUnits, adUnitCodes, callback: auctionCallback, cbTimeout: 20});
Expand Down

0 comments on commit d1b75c2

Please sign in to comment.