Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTD module: allow submodules to setBidRequestData without waitForIt #7862

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 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 @@ -242,5 +292,5 @@ describe('Real time module', function () {
expect(providers[1][hook].called).to.be.true;
});
});
})
});
});