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

Load one ad a time. #9808

Merged
merged 2 commits into from Jun 10, 2017
Merged

Load one ad a time. #9808

merged 2 commits into from Jun 10, 2017

Conversation

lannka
Copy link
Contributor

@lannka lannka commented Jun 8, 2017

Closes #9149

@lannka lannka requested a review from zhouyx June 8, 2017 22:03
win[LOADING_ADS_WIN_ID_]--;
}, 1000);
timerFor(win)
.timeoutPromise(1000, opt_loadingPromise || new Promise(() => {}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a never resolving promise, can't you just pass null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expect(is3pThrottled(win)).to.be.false;
incrementLoadingAds(win);
expect(is3pThrottled(win)).to.be.true;
clock.tick(999);
yield macroTask();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could return the promise from incrementLoadingAds.

Copy link
Contributor Author

@lannka lannka Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test then becomes:

clock.tick(999);
expect(is3pThrottled(win)).to.be.true;
clock.tick(1);
yield promise;
expect(is3pThrottled(win)).to.be.false;

the first check is meaningless here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How?

Copy link
Contributor Author

@lannka lannka Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's unfair to only yield on the 2nd tick. (and you can't yield on the first, which will run into a deadlock :-))

this is not testing the right thing:

clock.tick(1000);
expect(is3pThrottled(win)).to.be.true;
clock.tick(1);
yield promise;
expect(is3pThrottled(win)).to.be.false;

Copy link
Contributor

@jridgewell jridgewell Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, the macroTask isn't testing anything. You've frozen time in test's window.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should yield before doing each check.

Here we try to test that loading ads count will decrease by 1 after 1 sec, and only after 1 sec.
With yield promise, we can test that the ads count decrease by 1 after 1 sec.
However with

clock.tick(999);
expect(is3pThrottled(win)).to.be.true;

we can't tell if the promise is resolved only after 1sec. It is possible that the promise has already resolved after 999ms, but macroTasks not executed, loading ad count hasn't decresed by 1 when test code ran.

This is probably right to do:

clock.tick(999);
yield macroTask();
expect(is3pThrottled(win)).to.be.true;
clock.tick(1);
yield promise;
expect(is3pThrottled(win)).to.be.false;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, the macroTask isn't testing anything. You've frozen time in test's window.

@jridgewell here we want to yield an event loop so that if the timer has triggered the promise resolver, we wait till the resolution finish.

@zhouyx your suggestion is also treating the 2 ticks differently, which I don't like

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree with @zhouyx here, if you want to test specifically that it only resolves after 1s. I'm still against relying on macrotasks for promise changes to complete, though.

Copy link
Contributor Author

@lannka lannka Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want to treat the 2 ticks differently? isn't it cheating? are we doing a test to just get it passed?

we have to do 2 checks with all other conditions the same, if we really want to test the 1000ms triggering logic.

the test is translated to:

  1. clock tick 999ms
  2. ad should still be throttled in the next event loop (1ms to go!)
  3. clock tick 1ms
  4. ad should NOT be throttled starting from the next event loop

win[LOADING_ADS_WIN_ID_]--;
}, 1000);
timerFor(win)
.timeoutPromise(1000, opt_loadingPromise || new Promise(() => {}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeoutPromise() only require the timeout value to be defined. would timeoutPromise(1000, opt_loadingPromise) be enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@lannka lannka merged commit 5191dd8 into ampproject:master Jun 10, 2017
@lannka lannka deleted the ad-load-promise branch June 12, 2017 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants