-
Notifications
You must be signed in to change notification settings - Fork 56
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
fix: Only request vendorlist once #209
Conversation
Pull Request Test Coverage Report for Build 779
💛 - Coveralls |
Does not seem like your test coverage works properly? The coverage report basically say everything in 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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! 👍
To be honest, I am not sure how exactly coveralls gets to this result. I'd say a decrease of "0.06%" is neglectable. |
Think I overcomplicated things a bit, the new commit makes it even simpler, I didn't think about And about the coveralls thing, it seems to me like nothing really works? In the screenshot below you can see everything is red in |
There was a problem hiding this 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.
Any updates on this? |
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? |
@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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(() => {}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
Thanks! This is merged and we will create a new release as well soon. |
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.