Skip to content

Commit

Permalink
Removed the prebid-cache default call. (prebid#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbemiller authored Aug 10, 2017
1 parent 3351492 commit f4acee9
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 74 deletions.
22 changes: 14 additions & 8 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {getPriceBucketString} from './cpmBucketManager';
import {NATIVE_KEYS, nativeBidIsValid} from './native';
import { store } from './videoCache';
import { Renderer } from 'src/Renderer';
import { config } from 'src/config';

var CONSTANTS = require('./constants.json');
var AUCTION_END = CONSTANTS.EVENTS.AUCTION_END;
Expand Down Expand Up @@ -224,15 +225,20 @@ exports.addBidResponse = function (adUnitCode, bid) {

// Video bids may fail if the cache is down, or there's trouble on the network.
function tryAddVideoBid(bid) {
store([bid], function(error, cacheIds) {
if (error) {
utils.logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`);
} else {
bid.videoCacheKey = cacheIds[0].uuid;
addBidToAuction(bid);
}
if (config.getConfig('usePrebidCache')) {
store([bid], function(error, cacheIds) {
if (error) {
utils.logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`);
} else {
bid.videoCacheKey = cacheIds[0].uuid;
addBidToAuction(bid);
}
doCallbacksIfNeeded();
});
} else {
addBidToAuction(bid);
doCallbacksIfNeeded();
});
}
}
};

Expand Down
9 changes: 9 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const DEFAULT_ENABLE_SEND_ALL_BIDS = false;

const ALL_TOPICS = '*';

/**
* @typedef {object} PrebidConfig
*
* @property {bool} usePrebidCache True if we should use prebid-cache to store video bids before adding
* bids to the auction, and false otherwise. **NOTE** This must be true if you want to use the
* dfpAdServerVideo module.
*/

export function newConfig() {
let listeners = [];

Expand Down Expand Up @@ -82,6 +90,7 @@ export function newConfig() {
set s2sConfig(val) {
$$PREBID_GLOBAL$$.setS2SConfig(val);
},

};

/*
Expand Down
167 changes: 101 additions & 66 deletions test/spec/unit/bidmanager_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { config } from 'src/config';
import constants from 'src/constants';
import events from 'src/events';

Expand Down Expand Up @@ -28,6 +29,7 @@ describe('The Bid Manager', () => {
*/
function testAddVideoBid(expectBidAdded, expectCallbackCalled, videoCacheStubProvider) {
return function() {
const usePrebidCache = config.getConfig('usePrebidCache')
const mockResponse = Object.assign({}, bidResponse);
const callback = sinon.spy();
bidManager.addOneTimeCallback(callback);
Expand All @@ -41,8 +43,12 @@ describe('The Bid Manager', () => {
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(expectedBidsReceived);

const storeStub = videoCacheStubProvider().store;
expect(storeStub.calledOnce).to.equal(true);
expect(storeStub.getCall(0).args[0][0]).to.equal(mockResponse);
if (usePrebidCache) {
expect(storeStub.calledOnce).to.equal(true);
expect(storeStub.getCall(0).args[0][0]).to.equal(mockResponse);
} else {
expect(storeStub.called).to.equal(false);
}

if (expectedBidsReceived === 1) {
const bid = $$PREBID_GLOBAL$$._bidsReceived[0];
Expand All @@ -51,7 +57,10 @@ describe('The Bid Manager', () => {
expect(bid.cpm).to.equal(adjustCpm(0.1));

expect(bid.vastUrl).to.equal('www.myVastUrl.com');
expect(bid.videoCacheKey).to.equal('FAKE_UUID');

if (usePrebidCache) {
expect(bid.videoCacheKey).to.equal('FAKE_UUID');
}
}
if (expectCallbackCalled) {
expect(callback.calledOnce).to.equal(true);
Expand Down Expand Up @@ -113,12 +122,8 @@ describe('The Bid Manager', () => {
: new Date().getTime();
}

describe('when the cache is functioning properly', () => {
let stubProvider = useVideoCacheStubs({
store: [{ uuid: 'FAKE_UUID' }],
});

describe('when more bids are expected after this one', () => {
function testAddExpectMoreBids(stubProvider) {
return () => {
// Set up the global state so that we expect two bids, and the auction started just now
// (so as to reduce the chance of timeout. This assumes that the unit test runs run in < 5000 ms).
prepAuction(
Expand All @@ -131,83 +136,113 @@ describe('The Bid Manager', () => {

it("should add video bids, but shouldn't call the end-of-auction callbacks yet",
testAddVideoBid(true, false, stubProvider));
});
};
}

describe('when this is the last bid expected in the auction', () => {
// Set up the global state so that we expect only one bid, and the auction started just now
// (so as to reduce the chance of timeout. This assumes that the unit test runs run in < 5000 ms).
prepAuction([adUnit], (bidRequest) => bidRequest.start = auctionStart(false));
describe('when prebid-cache is enabled', () => {
before(() => {
config.setConfig({
usePrebidCache: true,
});
});

it("shouldn't add invalid bids", () => {
bidManager.addBidResponse('', { });
bidManager.addBidResponse('testCode', { mediaType: 'video' });
bidManager.addBidResponse('testCode', { mediaType: 'native' });
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
describe('when the cache is functioning properly', () => {
let stubProvider = useVideoCacheStubs({
store: [{ uuid: 'FAKE_UUID' }],
});

it('should add valid video bids and then execute the callbacks signaling the end of the auction',
testAddVideoBid(true, true, stubProvider));
describe('when more bids are expected after this one', testAddExpectMoreBids(stubProvider));

it('should gracefully do nothing when adUnitCode is undefined', () => {
bidManager.addBidResponse(undefined, {});
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
});
describe('when this is the last bid expected in the auction', () => {
// Set up the global state so that we expect only one bid, and the auction started just now
// (so as to reduce the chance of timeout. This assumes that the unit test runs run in < 5000 ms).
prepAuction([adUnit], (bidRequest) => bidRequest.start = auctionStart(false));

it('should gracefully do nothing when bid is undefined', () => {
bidManager.addBidResponse('mock/code');
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
it("shouldn't add invalid bids", () => {
bidManager.addBidResponse('', { });
bidManager.addBidResponse('testCode', { mediaType: 'video' });
bidManager.addBidResponse('testCode', { mediaType: 'native' });
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
});

it('should add valid video bids and then execute the callbacks signaling the end of the auction',
testAddVideoBid(true, true, stubProvider));

it('should gracefully do nothing when adUnitCode is undefined', () => {
bidManager.addBidResponse(undefined, {});
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
});

it('should gracefully do nothing when bid is undefined', () => {
bidManager.addBidResponse('mock/code');
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
});

it('should attach properties for analytics *before* the BID_ADJUSTMENT event listeners are called', () => {
const copy = Object.assign({}, bidResponse);
copy.getSize = function() {
return `${this.height}x${this.width}`;
};
delete copy.cpm;
bidManager.addBidResponse('mock/code', copy);
expect(copy).to.have.property('hadCpmDuringBidAdjustment', true);
expect(copy).to.have.property('hadAdUnitCodeDuringBidAdjustment', true);
expect(copy).to.have.property('hadTimeToRespondDuringBidAdjustment', true);
expect(copy).to.have.property('hadRequestTimestampDuringBidAdjustment', true);
expect(copy).to.have.property('hadResponseTimestampDuringBidAdjustment', true);
});
});

it('should attach properties for analytics *before* the BID_ADJUSTMENT event listeners are called', () => {
const copy = Object.assign({}, bidResponse);
copy.getSize = function() {
return `${this.height}x${this.width}`;
};
delete copy.cpm;
bidManager.addBidResponse('mock/code', copy);
expect(copy).to.have.property('hadCpmDuringBidAdjustment', true);
expect(copy).to.have.property('hadAdUnitCodeDuringBidAdjustment', true);
expect(copy).to.have.property('hadTimeToRespondDuringBidAdjustment', true);
expect(copy).to.have.property('hadRequestTimestampDuringBidAdjustment', true);
expect(copy).to.have.property('hadResponseTimestampDuringBidAdjustment', true);
describe('when the auction has timed out', () => {
// Set up the global state to expect two bids, and mock an auction which happened long enough
// in the past that it will *seem* like this bid is arriving after the timeouts.
prepAuction(
[adUnit, Object.assign({}, adUnit, { code: 'video2' })],
(bidRequest) => {
const tweakedBidRequestBid = Object.assign({}, bidRequest.bids[0], { placementCode: 'video2' });
bidRequest.bids.push(tweakedBidRequestBid);
bidRequest.start = auctionStart(true);
});

// Because of the preconditions, this makes sure that the end-of-auction callbacks get called when
// the auction hits the timeout.
it('should add the bid, but also execute the callbacks signaling the end of the auction',
testAddVideoBid(true, true, stubProvider));
});
});

describe('when the auction has timed out', () => {
// Set up the global state to expect two bids, and mock an auction which happened long enough
// in the past that it will *seem* like this bid is arriving after the timeouts.
prepAuction(
[adUnit, Object.assign({}, adUnit, { code: 'video2' })],
(bidRequest) => {
const tweakedBidRequestBid = Object.assign({}, bidRequest.bids[0], { placementCode: 'video2' });
bidRequest.bids.push(tweakedBidRequestBid);
bidRequest.start = auctionStart(true);
});
describe('when the cache is failing for some reason,', () => {
let stubProvider = useVideoCacheStubs({
store: new Error('Unable to save to the cache'),
});

describe('when the auction still has time left', () => {
prepAuction([adUnit], (bidRequest) => bidRequest.start = auctionStart(false));

it("shouldn't add the bid to the auction, and shouldn't execute the end-of-auction callbacks",
testAddVideoBid(false, false, stubProvider));
});

// Because of the preconditions, this makes sure that the end-of-auction callbacks get called when
// the auction hits the timeout.
it('should add the bid, but also execute the callbacks signaling the end of the auction',
testAddVideoBid(true, true, stubProvider));
describe('when the auction has timed out', () => {
prepAuction([adUnit], (bidRequest) => bidRequest.start = auctionStart(true));
it("shouldn't add the bid to the auction, but should execute the end-of-auction callbacks",
testAddVideoBid(false, true, stubProvider));
})
});
});

describe('when the cache is failing for some reason,', () => {
describe('when prebid-cache is disabled', () => {
let stubProvider = useVideoCacheStubs({
store: new Error('Unable to save to the cache'),
store: [{ uuid: 'FAKE_UUID' }],
});

describe('when the auction still has time left', () => {
prepAuction([adUnit], (bidRequest) => bidRequest.start = auctionStart(false));

it("shouldn't add the bid to the auction, and shouldn't execute the end-of-auction callbacks",
testAddVideoBid(false, false, stubProvider));
before(() => {
config.setConfig({
usePrebidCache: false,
});
});

describe('when the auction has timed out', () => {
prepAuction([adUnit], (bidRequest) => bidRequest.start = auctionStart(true));
it("shouldn't add the bid to the auction, but should execute the end-of-auction callbacks",
testAddVideoBid(false, true, stubProvider));
})
describe('when more bids are expected after this one', testAddExpectMoreBids(stubProvider));
});
});
});

0 comments on commit f4acee9

Please sign in to comment.