Skip to content

Commit

Permalink
RTD module: allow submodules to setBidRequestData without waitForIt
Browse files Browse the repository at this point in the history
This change allows all RTD submodules at least one shot to see the `setBidRequestData` hook even if they are not flagged `waitForIt` (previously they would not be run at all in some cases).

Addresses #7117
  • Loading branch information
dgirardi committed Dec 14, 2021
1 parent f542ff9 commit a29ae1b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
15 changes: 6 additions & 9 deletions modules/rtdModule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,31 +289,28 @@ export function setBidRequestsData(fn, reqBidsConfigObj) {
return exitHook();
}

if (shouldDelayAuction) {
waitTimeout = setTimeout(exitHook, _moduleConfig.auctionDelay);
}
waitTimeout = setTimeout(exitHook, shouldDelayAuction ? _moduleConfig.auctionDelay : 0);

relevantSubModules.forEach(sm => {
sm.getBidRequestData(reqBidsConfigObj, onGetBidRequestDataCallback.bind(sm), sm.config, _userConsent)
});

if (!shouldDelayAuction) {
return exitHook();
}

function onGetBidRequestDataCallback() {
if (isDone) {
return;
}
if (this.config && this.config.waitForIt) {
callbacksExpected--;
}
if (callbacksExpected <= 0) {
return exitHook();
if (callbacksExpected === 0) {
setTimeout(exitHook, 0);
}
}

function exitHook() {
if (isDone) {
return;
}
isDone = true;
clearTimeout(waitTimeout);
fn.call(this, reqBidsConfigObj);
Expand Down
54 changes: 52 additions & 2 deletions test/spec/modules/realTimeDataModule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {config} from 'src/config.js';
import * as sinon from 'sinon';
import {default as CONSTANTS} from '../../../src/constants.json';
import {default as events} from '../../../src/events.js';
import 'src/prebid.js';

const getBidRequestDataSpy = sinon.spy();

Expand Down Expand Up @@ -140,7 +141,56 @@ describe('Real time module', function () {
const adUnits = rtdModule.getAdUnitTargeting(auction);
assert.deepEqual(expectedAdUnits, adUnits)
done();
})
});

describe('setBidRequestData', () => {
let withWait, withoutWait;

function runSetBidRequestData() {
return new Promise((resolve) => {
rtdModule.setBidRequestsData(resolve, {bidRequest: {}});
});
}

beforeEach(() => {
withWait = {
submod: validSMWait,
cbTime: 0,
cbRan: false
};
withoutWait = {
submod: validSM,
cbTime: 0,
cbRan: false
};

[withWait, withoutWait].forEach((c) => {
c.submod.getBidRequestData = sinon.stub().callsFake((_, cb) => {
setTimeout(() => {
c.cbRan = true;
cb();
}, c.cbTime);
});
});
});

it('should allow non-priority submodules to run synchronously', () => {
withWait.cbTime = withoutWait.cbTime = 0;
return runSetBidRequestData().then(() => {
expect(withWait.cbRan).to.be.true;
expect(withoutWait.cbRan).to.be.true;
})
});

it('should not wait for non-priority submodules if priority ones complete first', () => {
withWait.cbTime = 10;
withoutWait.cbTime = 20;
return runSetBidRequestData().then(() => {
expect(withWait.cbRan).to.be.true;
expect(withoutWait.cbRan).to.be.false;
});
});
});
});

it('deep merge object', function () {
Expand Down Expand Up @@ -242,5 +292,5 @@ describe('Real time module', function () {
expect(providers[1][hook].called).to.be.true;
});
});
})
});
});

0 comments on commit a29ae1b

Please sign in to comment.