Skip to content

Commit

Permalink
s2sTesting: random number moved to global (prebid#3851)
Browse files Browse the repository at this point in the history
* s2sTesting: random number moved to global

We got a request that if if 10% of bidderA and 10% of bidderB should be server-side, they should be the same 10% of requests.

This is a simple approach to the change that implies that the same page experience only throws one random number for the purposes of s2sTesting. i.e. if there are dynamic ads on the page, all newly created ads on the page will continue to use the same number. This is ok from the purposes of the s2sTesting module.

* Added get for randNum + fix lint & tests

* update to remove getter

* remove log

* update test
  • Loading branch information
bretg authored and sa1omon committed Nov 28, 2019
1 parent 5f2a8c7 commit b6defb6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
3 changes: 2 additions & 1 deletion modules/s2sTesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ s2sTesting.CLIENT = CLIENT;

var testing = false; // whether testing is turned on
var bidSource = {}; // store bidder sources determined from s2sConfing 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 => {
Expand Down Expand Up @@ -82,7 +83,7 @@ s2sTesting.getSource = function(sourceWeights = {}, bidSources = [SERVER, CLIENT
});
if (!totWeight) return; // bail if no source weights
// choose a source randomly based on weights
var rndWeight = Math.random() * totWeight;
var rndWeight = s2sTesting.globalRand * totWeight;
for (var i = 0; i < bidSources.length; i++) {
let source = bidSources[i];
// choose the first source with an incremental weight > random weight
Expand Down
68 changes: 48 additions & 20 deletions test/spec/modules/s2sTesting_spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import s2sTesting from 'modules/s2sTesting';
import { config } from 'src/config';
import find from 'core-js/library/fn/array/find';

var events = require('src/events');
var CONSTANTS = require('src/constants.json');
const BID_ADJUSTMENT = CONSTANTS.EVENTS.BID_ADJUSTMENT;

var expect = require('chai').expect;

describe('s2sTesting', function () {
let mathRandomStub;
let randomNumber = 0;

beforeEach(function () {
mathRandomStub = sinon.stub(Math, 'random').callsFake(() => { return randomNumber; });
});

afterEach(function () {
mathRandomStub.restore();
});

describe('s2sTesting.getSource', function () {
// helper function to set random number and get the source
function getExpectedSource(randNumber, sourceWeights, sources) {
// set random number for testing
randomNumber = randNumber;
s2sTesting.globalRand = randNumber;
return s2sTesting.getSource(sourceWeights, sources);
}

Expand Down Expand Up @@ -93,7 +77,7 @@ describe('s2sTesting', function () {
describe('setting source through s2sConfig', function () {
beforeEach(function () {
// set random number for testing
randomNumber = 0.7;
s2sTesting.globalRand = 0.7;
});

it('does not work if testing is "false"', function () {
Expand Down Expand Up @@ -155,14 +139,58 @@ describe('s2sTesting', function () {
expect(serverClientBidders.server).to.eql(['rubicon']);
expect(serverClientBidders.client).to.have.members(['appnexus']);
});

it('sends both bidders to same source when weights are the same', function () {
s2sTesting.globalRand = 0.01;

config.setConfig({s2sConfig: {
bidders: ['rubicon', 'appnexus'],
testing: true,
bidderControl: {
rubicon: {bidSource: {server: 1, client: 99}},
appnexus: {bidSource: {server: 1, client: 99}}
}}});
expect(s2sTesting.getSourceBidderMap()).to.eql({
client: ['rubicon', 'appnexus'],
server: []
});
expect(s2sTesting.getSourceBidderMap()).to.eql({
client: ['rubicon', 'appnexus'],
server: []
});
expect(s2sTesting.getSourceBidderMap()).to.eql({
client: ['rubicon', 'appnexus'],
server: []
});

config.setConfig({s2sConfig: {
bidders: ['rubicon', 'appnexus'],
testing: true,
bidderControl: {
rubicon: {bidSource: {server: 99, client: 1}},
appnexus: {bidSource: {server: 99, client: 1}}
}}});
expect(s2sTesting.getSourceBidderMap()).to.eql({
server: ['rubicon', 'appnexus'],
client: []
});
expect(s2sTesting.getSourceBidderMap()).to.eql({
server: ['rubicon', 'appnexus'],
client: []
});
expect(s2sTesting.getSourceBidderMap()).to.eql({
server: ['rubicon', 'appnexus'],
client: []
});
});
});

describe('setting source through adUnits', function () {
beforeEach(function () {
// reset s2sconfig bid sources
config.setConfig({s2sConfig: {testing: true}});
// set random number for testing
randomNumber = 0.7;
s2sTesting.globalRand = 0.7;
});

it('sets one bidder source from one adUnit', function () {
Expand Down Expand Up @@ -280,7 +308,7 @@ describe('s2sTesting', function () {
// reset s2sconfig bid sources
config.setConfig({s2sConfig: {testing: true}});
// set random number for testing
randomNumber = 0.7;
s2sTesting.globalRand = 0.7;
});

it('should get sources from both', function () {
Expand Down

0 comments on commit b6defb6

Please sign in to comment.