Skip to content

Commit

Permalink
RTD module: allow submodules to setBidRequestData without waitForIt (
Browse files Browse the repository at this point in the history
…prebid#7862)

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 prebid#7117
  • Loading branch information
dgirardi authored Jan 11, 2022
1 parent 69e8195 commit a13a04a
Show file tree
Hide file tree
Showing 2 changed files with 57 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 @@ -291,31 +291,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
53 changes: 51 additions & 2 deletions test/spec/modules/realTimeDataModule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,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 = 100;
return runSetBidRequestData().then(() => {
expect(withWait.cbRan).to.be.true;
expect(withoutWait.cbRan).to.be.false;
});
});
});
});

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

0 comments on commit a13a04a

Please sign in to comment.