Skip to content

Commit

Permalink
Merge pull request #134 from MailOnline/next-release
Browse files Browse the repository at this point in the history
Next release
  • Loading branch information
carpasse authored Jan 25, 2019
2 parents bd2579b + 146815d commit c2a7c42
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
67 changes: 67 additions & 0 deletions src/runner/__tests__/runWaterfall.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,73 @@ describe('runWaterfall', () => {
timeout: 600
}));
});

it('must finish the ad run if it times out fetching an ad', async () => {
const deferred = defer();
const opts = {
timeout: 1000
};
const onAdStart = jest.fn();
const onError = jest.fn();

Date.now.mockReturnValueOnce(1000);
Date.now.mockReturnValueOnce(10000);

runWaterfall(adTag, placeholder, {
...opts,
onAdStart,
onError,
onRunFinish: () => deferred.resolve()
});

await deferred.promise;

expect(requestAd).toHaveBeenCalledTimes(1);
expect(requestAd).toHaveBeenCalledWith(adTag, expect.objectContaining({
...opts,
timeout: 1000
}));

expect(run).toHaveBeenCalledTimes(0);
expect(onAdStart).toHaveBeenCalledTimes(0);
expect(onError).toHaveBeenCalledTimes(1);
});

test('must not continue the waterfall if ad run has timed out', async () => {
const deferred = defer();
const opts = {
timeout: 1000
};
const onAdStart = jest.fn();
const onError = jest.fn();

Date.now.mockReturnValueOnce(1000);
Date.now.mockReturnValueOnce(1100);
Date.now.mockReturnValueOnce(2100);

run.mockReturnValue(Promise.reject(new Error('Ad start timeout simulation')));
requestAd.mockReturnValue(Promise.resolve(vastAdChain));
requestNextAd.mockReturnValueOnce(Promise.resolve(vastAdChain));

runWaterfall(adTag, placeholder, {
...opts,
onAdStart,
onError,
onRunFinish: () => deferred.resolve()
});

await deferred.promise;

expect(requestAd).toHaveBeenCalledTimes(1);
expect(requestAd).toHaveBeenCalledWith(adTag, expect.objectContaining({
...opts,
timeout: 1000
}));

expect(run).toHaveBeenCalledTimes(1);
expect(onAdStart).toHaveBeenCalledTimes(0);
expect(onError).toHaveBeenCalledTimes(1);
});
});

describe('cancel fn', () => {
Expand Down
17 changes: 10 additions & 7 deletions src/runner/runWaterfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const transformVastResponse = (vastChain, {hooks}) => {
return vastChain;
};

// eslint-disable-next-line complexity
const waterfall = async (fetchVastChain, placeholder, options, isCanceled) => {
let vastChain;
let runEpoch;
Expand Down Expand Up @@ -110,14 +111,16 @@ const waterfall = async (fetchVastChain, placeholder, options, isCanceled) => {
opts.timeout -= Date.now() - runEpoch;
}

waterfall(
() => requestNextAd(vastChain, opts),
placeholder,
{...opts},
isCanceled
);
if (!runEpoch || opts.timeout > 0) {
waterfall(
() => requestNextAd(vastChain, opts),
placeholder,
{...opts},
isCanceled
);

return;
return;
}
}

onRunFinish();
Expand Down

0 comments on commit c2a7c42

Please sign in to comment.