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

fix: Only request vendorlist once #209

Merged
merged 6 commits into from
Oct 2, 2018

Conversation

Fumler
Copy link
Contributor

@Fumler Fumler commented Sep 6, 2018

Fixes #206

This PR sets a flag whenever you do a request for the vendorlist, so if there is several calls to loadVendorList() before the request is resolved and cache is saved, we wait for the first request to resolve in the other calls.

@coveralls
Copy link

coveralls commented Sep 6, 2018

Pull Request Test Coverage Report for Build 779

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 32 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.7%) to 27.377%

Files with Coverage Reduction New Missed Lines %
dist/oil.bundle.js 32 27.38%
Totals Coverage Status
Change from base Build 732: 0.7%
Covered Lines: 1129
Relevant Lines: 3360

💛 - Coveralls

@Fumler
Copy link
Contributor Author

Fumler commented Sep 6, 2018

Does not seem like your test coverage works properly? The coverage report basically say everything in oil.bundle.js is red, so the 27% coverage is just variables etc?

I don't know how to fix this. Maybe one of you guys know where to start? 😅

resolve(cachedVendorList);
} else if (startedVendorlistRequest) {
// if a request for the vendorlist has been started, let's wait for it
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be simpler to just store the pending Promise in a variable named
pendingVendorlistPromise in line 41?
Return it in line 22 if it exists.
Nullify pendingVendorlistPromise when the result of fetchJsonData(iabVendorListUrl) is there and cachedVendorList is set.
The test for a timeout you could move inside the else block then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also a test for the timeout would be great! 👍

@phogel
Copy link
Contributor

phogel commented Sep 7, 2018

To be honest, I am not sure how exactly coveralls gets to this result. I'd say a decrease of "0.06%" is neglectable.

@Fumler
Copy link
Contributor Author

Fumler commented Sep 7, 2018

Think I overcomplicated things a bit, the new commit makes it even simpler, I didn't think about fetchJsonData being a promise, so we can just return that.

And about the coveralls thing, it seems to me like nothing really works? In the screenshot below you can see everything is red in loadVendorlist even though you have a million tests for it that should cover those lines. So the 0.06% is just because I add a few new lines to the bundle but the new test I added is not reflected in the coverage?

screen shot 2018-09-07 at 14 21 59

Copy link
Contributor

@phogel phogel left a comment

Choose a reason for hiding this comment

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

Greatly improved 👍
Just ran the tests and they're all green.

@Fumler
Copy link
Contributor Author

Fumler commented Sep 17, 2018

Any updates on this?

@Fumler
Copy link
Contributor Author

Fumler commented Sep 27, 2018

Sorry for nagging, but we really want this released. Does anyone have time to CR and merge this or should we start using a fork?

@ltparis2018 @tbtz @phogel

@marcelb
Copy link
Contributor

marcelb commented Sep 27, 2018

@Fumler Heya, I'm back in the team and will look into this today!


export function loadVendorList() {
return new Promise(function (resolve) {
if (cachedVendorList) {
resolve(cachedVendorList);
} else if (pendingVendorlistPromise) {
return pendingVendorlistPromise;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a test to check if this works correctly. I think this doesn't return function loadVendorList() but the inner anonymous function (resolve) {} unresolved instead. I would expect unexpected behavior where loadVendorList() is used. I think you want to return an unnested promise instead that behaves exactly like all the others.

Copy link
Contributor

@marcelb marcelb Sep 27, 2018

Choose a reason for hiding this comment

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

I would probably refactor the whole function that the whole scope isn't inside that huge Promise return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. The idea is:

  • On the first call to loadVendorList we return a promise, that when finished, will resolve to the vendorlist data.
  • On the second call to loadVendorList we also return a promise, that when finished, will resolve to the vendorlist data, as long as there is no cached version, and we have already started a promise
  • On the third call to loadVendorList we also return a promise, that when finished, will resolve to the vendorlist data, as long as there is no cached version, and we have already started a promise
  • .. etc

The second and third cases will only return the started promise if it exists.

Copy link
Contributor

Choose a reason for hiding this comment

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

"- On the second call to loadVendorList we also return a promise, that when finished, will resolve to the vendorlist data, as long as there is no cached version, and we have already started a promise"

Thats the one im not sure that it works as intended. There is a return statement returning the promise inside another promise (line 16). Not resolving, returning. (line 19)

Copy link
Contributor

@marcelb marcelb Sep 27, 2018

Choose a reason for hiding this comment

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

But anyway, the tests are looking fine and should fail, if its actually broken. Thank you!
The change makes a lot of sense, thank you for the contribution and sorry for the long delay!

Im not entirely sure yet though and can't create a new version today anymore and will be on a 1 day vacation tomorrow. @ltparis2018 @tbtz Can you have a look as well and merge it tomorrow? If not I'll be back on monday and finish this up. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

im on it again :) I think we will be merging it soon, but we might do a small refactoring on it later.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Fumler I did some tests on the code and I believe its still broken.

The test can't catch the errors though, because its also flawed. The tests done() function is executed before the Promises can resolve, so all the expects within this test wont run at all.
A quick fix is to move the done() in the last async method, which results in a failed test (because the last loadVendorList() never resolves).

There are two bugs within the function. One being returning the pendingVendorlistPromise instead of resolving it and the second one is the call to fetchJsonData which doesn't set the pendingVendorlistPromise variable probably. It resolves the right side first, resulting in a wrong value in the variable.

The test also assumes implicit timings of the code to work, which seems to be ok though, not raising any race conditions.

Please fix those remaining issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

This line should rather read
resolve(pendingVendorlistPromise);

This of course results in nested Promises, which works.

spyOn(CoreConfig, 'getIabVendorListUrl').and.returnValue("https://iab.vendor.list.url");

loadVendorList().then(() => {});
loadVendorList().then(() => {});
Copy link
Contributor

@marcelb marcelb Sep 27, 2018

Choose a reason for hiding this comment

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

Please make also sure that the second loadVendorList().then() is called before the first one resolves. I'm not sure whether the second to fourth call is not just returning the cached version and this couldn't have race conditions. If you are please explain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the tests.

});
expect(fetchSpy.calls.count()).toBe(1);

done();
Copy link
Contributor

Choose a reason for hiding this comment

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

This done() should be on line 77 instead to make the test work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

@marcelb marcelb merged commit 267e974 into as-ideas:master Oct 2, 2018
@marcelb
Copy link
Contributor

marcelb commented Oct 2, 2018

Thanks! This is merged and we will create a new release as well soon.

@Fumler Fumler deleted the fix/only-request-vendorlist-once branch October 2, 2018 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vendorlist is called multiple times
4 participants