Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1298 fix fbf0840 #2067

Merged
merged 2 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,44 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels})
}
}

function doCallbacksIfTimedout(auctionInstance, bidResponse) {
if (bidResponse.timeToRespond > auctionInstance.getTimeout() + config.getConfig('timeoutBuffer')) {
auctionInstance.executeCallback(true);
}
}

// Add a bid to the auction.
function addBidToAuction(auctionInstance, bidResponse) {
events.emit(CONSTANTS.EVENTS.BID_RESPONSE, bidResponse);
auctionInstance.addBidReceived(bidResponse);

doCallbacksIfTimedout(auctionInstance, bidResponse);
}

// Video bids may fail if the cache is down, or there's trouble on the network.
function tryAddVideoBid(auctionInstance, bidResponse, bidRequest, vastUrl) {
if (config.getConfig('cache.url')) {
store([bidResponse], function(error, cacheIds) {
if (error) {
utils.logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`);

doCallbacksIfTimedout(auctionInstance, bidResponse);
} else {
bidResponse.videoCacheKey = cacheIds[0].uuid;
if (!vastUrl) {
bidResponse.vastUrl = getCacheUrl(bidResponse.videoCacheKey);
}
// only set this prop after the bid has been cached to avoid early ending auction early in bidsBackAll
bidRequest.doneCbCallCount += 1;
addBidToAuction(auctionInstance, bidResponse);
auctionInstance.bidsBackAll();
}
});
} else {
addBidToAuction(auctionInstance, bidResponse);
}
}

export const addBidResponse = createHook('asyncSeries', function(adUnitCode, bid) {
let auctionInstance = this;
let bidRequests = auctionInstance.getBidRequests();
Expand All @@ -230,46 +268,9 @@ export const addBidResponse = createHook('asyncSeries', function(adUnitCode, bid
let bidResponse = getPreparedBidForAuction({adUnitCode, bid, bidRequest, auctionId});

if (bidResponse.mediaType === 'video') {
tryAddVideoBid(bidResponse);
tryAddVideoBid(auctionInstance, bidResponse, bidRequest, bid.vastUrl);
} else {
doCallbacksIfNeeded();
addBidToAuction(bidResponse);
}

function doCallbacksIfNeeded() {
if (bidResponse.timeToRespond > auctionInstance.getTimeout() + config.getConfig('timeoutBuffer')) {
auctionInstance.executeCallback(true);
}
}

// Add a bid to the auction.
function addBidToAuction() {
events.emit(CONSTANTS.EVENTS.BID_RESPONSE, bidResponse);
auctionInstance.addBidReceived(bidResponse);
}

// Video bids may fail if the cache is down, or there's trouble on the network.
function tryAddVideoBid(bidResponse) {
if (config.getConfig('cache.url')) {
store([bidResponse], function(error, cacheIds) {
if (error) {
utils.logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`);
} else {
bidResponse.videoCacheKey = cacheIds[0].uuid;
if (!bid.vastUrl) {
bidResponse.vastUrl = getCacheUrl(bidResponse.videoCacheKey);
}
// only set this prop after the bid has been cached to avoid early ending auction early in bidsBackAll
bidRequest.doneCbCallCount += 1;
addBidToAuction(bidResponse);
auctionInstance.bidsBackAll();
}
doCallbacksIfNeeded();
});
} else {
addBidToAuction(bidResponse);
doCallbacksIfNeeded();
}
addBidToAuction(auctionInstance, bidResponse);
}
}, 'addBidResponse');

Expand Down
52 changes: 1 addition & 51 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ describe('auctionmanager.js', function () {
let auction;
let ajaxStub;
const BIDDER_CODE = 'sampleBidder';
const BIDDER_CODE1 = 'sampleBidder1';
let makeRequestsStub;
let bids = [{
'ad': 'creative',
Expand Down Expand Up @@ -643,57 +644,6 @@ describe('auctionmanager.js', function () {
assert.equal(addedBid.renderer.url, 'renderer.js');
});
});

describe('with auction timeout 20', () => {
let auction;
let adUnits;
let adUnitCodes;
let createAuctionStub;
let spec;
let getBidderRequestStub;
let eventsEmitSpy;

beforeEach(() => {
adUnits = [{
code: 'adUnit-code',
bids: [
{bidder: BIDDER_CODE, params: {placementId: 'id'}},
]
}];
adUnitCodes = ['adUnit-code'];
auction = auctionModule.newAuction({adUnits, adUnitCodes, callback: function() {}, cbTimeout: 20});
createAuctionStub = sinon.stub(auctionModule, 'newAuction');
createAuctionStub.returns(auction);
getBidderRequestStub = sinon.stub(utils, 'getBidderRequest');

let newBidRequest = Object.assign({}, bidRequests[0], {'start': 1000});
getBidderRequestStub.returns(newBidRequest);

spec = {
code: BIDDER_CODE,
isBidRequestValid: sinon.stub(),
buildRequests: sinon.stub(),
interpretResponse: sinon.stub(),
getUserSyncs: sinon.stub()
};
eventsEmitSpy = sinon.spy(events, 'emit');
});

afterEach(() => {
auctionModule.newAuction.restore();
utils.getBidderRequest.restore();
events.emit.restore();
});

it('should emit BID_TIMEOUT for timed out bids', () => {
registerBidder(spec);
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids();
assert.ok(eventsEmitSpy.calledWith(CONSTANTS.EVENTS.BID_TIMEOUT), 'emitted events BID_TIMEOUT');
});
});
});

describe('addBidResponse', () => {
Expand Down