Skip to content

Commit

Permalink
adjusted testing module to work with mutliple configs
Browse files Browse the repository at this point in the history
  • Loading branch information
RRSchweitzer committed Jan 4, 2021
1 parent 2ad241e commit 0e808fa
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 117 deletions.
46 changes: 14 additions & 32 deletions modules/s2sTesting.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { config } from '../src/config.js';
import { setS2STestingModule } from '../src/adapterManager.js';

let s2sTesting = {};
Expand All @@ -9,42 +8,31 @@ const CLIENT = 'client';
s2sTesting.SERVER = SERVER;
s2sTesting.CLIENT = CLIENT;

var testing = false; // whether testing is turned on
var bidSource = {}; // store bidder sources determined from s2sConfing bidderControl
s2sTesting.bidSource = {}; // store bidder sources determined from s2sConfig bidderControl
s2sTesting.globalRand = Math.random(); // if 10% of bidderA and 10% of bidderB should be server-side, make it the same 10%

// load s2sConfig
config.getConfig('s2sConfig', config => {
if (config && config.s2sConfig) {
const s2sConfigs = Array.isArray(config.s2sConfig) ? config.s2sConfig : [config.s2sConfig];
testing = s2sConfigs.some(i => i.testing);
s2sTesting.calculateBidSources(s2sConfigs);
}
});

s2sTesting.getSourceBidderMap = function(adUnits = []) {
s2sTesting.getSourceBidderMap = function(adUnits = [], allS2SBidders = []) {
var sourceBidders = {[SERVER]: {}, [CLIENT]: {}};

// bail if testing is not turned on
if (!testing) {
return {[SERVER]: [], [CLIENT]: []};
}

adUnits.forEach((adUnit) => {
// if any adUnit bidders specify a bidSource, include them
(adUnit.bids || []).forEach((bid) => {
// When a s2sConfig does not have testing=true and did not calc bid sources
if (allS2SBidders.indexOf(bid.bidder) > -1 && !s2sTesting.bidSource[bid.bidder]) {
s2sTesting.bidSource[bid.bidder] = SERVER;
}
// calculate the source once and store on bid object
bid.calcSource = bid.calcSource || s2sTesting.getSource(bid.bidSource);
// if no bidSource at bid level, default to bidSource from bidder
bid.finalSource = bid.calcSource || bidSource[bid.bidder] || CLIENT; // default to client
bid.finalSource = bid.calcSource || s2sTesting.bidSource[bid.bidder] || CLIENT; // default to client
// add bidder to sourceBidders data structure
sourceBidders[bid.finalSource][bid.bidder] = true;
});
});

// make sure all bidders in bidSource are in sourceBidders
Object.keys(bidSource).forEach((bidder) => {
sourceBidders[bidSource[bidder]][bidder] = true;
Object.keys(s2sTesting.bidSource).forEach((bidder) => {
sourceBidders[s2sTesting.bidSource[bidder]][bidder] = true;
});

// return map of source => array of bidders
Expand All @@ -58,18 +46,12 @@ s2sTesting.getSourceBidderMap = function(adUnits = []) {
* @function calculateBidSources determines the source for each s2s bidder based on bidderControl weightings. these can be overridden at the adUnit level
* @param s2sConfigs server-to-server configuration
*/
s2sTesting.calculateBidSources = function(s2sConfigs) {
// bail if testing is not turned on
if (!testing) {
return;
}
bidSource = {}; // reset bid sources
s2sTesting.calculateBidSources = function(s2sConfig = {}) {
// calculate bid source (server/client) for each s2s bidder
s2sConfigs.forEach(s2sConfig => {
var bidderControl = s2sConfig.bidderControl || {};
(s2sConfig.bidders || []).forEach((bidder) => {
bidSource[bidder] = s2sTesting.getSource(bidderControl[bidder] && bidderControl[bidder].bidSource) || SERVER; // default to server
});

var bidderControl = s2sConfig.bidderControl || {};
(s2sConfig.bidders || []).forEach((bidder) => {
s2sTesting.bidSource[bidder] = s2sTesting.getSource(bidderControl[bidder] && bidderControl[bidder].bidSource) || SERVER; // default to server
});
};

Expand Down
59 changes: 38 additions & 21 deletions src/adapterManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,20 @@ export let uspDataHandler = {
}
};

let clientTestAdapters = [];
// export for testing
export let clientTestAdapters = [];
export const allS2SBidders = [];

export function getAllS2SBidders() {
adapterManager.s2STestingEnabled = false;
_s2sConfigs.forEach(s2sConfig => {
if (s2sConfig && s2sConfig.enabled) {
if (s2sConfig.bidders && s2sConfig.bidders.length) {
allS2SBidders.push(...s2sConfig.bidders);
}
}
})
}

adapterManager.makeBidRequests = hook('sync', function (adUnits, auctionStart, auctionId, cbTimeout, labels) {
/**
Expand All @@ -197,41 +210,44 @@ adapterManager.makeBidRequests = hook('sync', function (adUnits, auctionStart, a
let clientBidderCodes = bidderCodes;

let bidRequests = [];
const allS2SBidders = [];

if (allS2SBidders.length === 0) {
getAllS2SBidders();
}

_s2sConfigs.forEach(s2sConfig => {
if (s2sConfig && s2sConfig.enabled) {
if (s2sConfig.bidders && s2sConfig.bidders.length) {
allS2SBidders.push(...s2sConfig.bidders);
}

// if s2sConfig.bidderControl testing is turned on
if (doingS2STesting(s2sConfig)) {
const bidderMap = s2sTestingModule.getSourceBidderMap(adUnits);
s2sTestingModule.calculateBidSources(s2sConfig);
const bidderMap = s2sTestingModule.getSourceBidderMap(adUnits, allS2SBidders);
// get all adapters doing client testing
clientTestAdapters = bidderMap[s2sTestingModule.CLIENT];
bidderMap[s2sTestingModule.CLIENT].forEach(bidder => {
if (!includes(clientTestAdapters, bidder)) {
clientTestAdapters.push(bidder);
}
})
}
}
});
})

// don't call these client side (unless client request is needed for testing)
clientBidderCodes = bidderCodes.filter(bidderCode => {
return !includes(allS2SBidders, bidderCode) || includes(clientTestAdapters, bidderCode)
});

_s2sConfigs.forEach(s2sConfig => {
if (s2sConfig && s2sConfig.enabled) {
// these are called on the s2s adapter
let adaptersServerSide = s2sConfig.bidders;
// these are called on the s2s adapter
let adaptersServerSide = allS2SBidders;

const adUnitsContainServerRequests = (adUnits, s2sConfig) => Boolean(
find(adUnits, adUnit => find(adUnit.bids, bid => (
bid.bidSource ||
(s2sConfig.bidderControl && s2sConfig.bidderControl[bid.bidder])
) && bid.finalSource === s2sTestingModule.SERVER))
);
const adUnitsContainServerRequests = (adUnits, s2sConfig) => Boolean(
find(adUnits, adUnit => find(adUnit.bids, bid => (
bid.bidSource ||
(s2sConfig.bidderControl && s2sConfig.bidderControl[bid.bidder])
) && bid.finalSource === s2sTestingModule.SERVER))
);

if (isTestingServerOnly(s2sConfig) && adUnitsContainServerRequests(adUnits, s2sConfig)) {
_s2sConfigs.forEach(s2sConfig => {
if (s2sConfig && s2sConfig.enabled) {
if ((isTestingServerOnly(s2sConfig) && adUnitsContainServerRequests(adUnits, s2sConfig))) {
utils.logWarn('testServerOnly: True. All client requests will be suppressed.');
clientBidderCodes.length = 0;
}
Expand Down Expand Up @@ -272,6 +288,7 @@ adapterManager.makeBidRequests = hook('sync', function (adUnits, auctionStart, a
});
}
})

// client adapters
let adUnitsClientCopy = getAdUnitCopyForClientAdapters(adUnits);
clientBidderCodes.forEach(bidderCode => {
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,13 @@ export function getAdUnits() {
'bidder': 'appnexus',
'params': {
'placementId': '543221',
'test': 'me'
}
},
{
'bidder': 'pubmatic',
'params': {
'publisherId': 1234567,
'adSlot': '1234567@728x90'
}
}
]
Expand Down
Loading

0 comments on commit 0e808fa

Please sign in to comment.