-
Notifications
You must be signed in to change notification settings - Fork 55
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
marcelb
merged 6 commits into
as-ideas:master
from
Fumler:fix/only-request-vendorlist-once
Oct 2, 2018
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3682721
fix: Only request vendorlist once
Fumler e77dab6
refactor: Remove console.log from test
Fumler 9a0693c
refactor: Simplify checking for cached vendorlist
Fumler 9fff479
remove comments and add semicolon
phogel 24ed51d
refactor: Change tests to make sure we do not have cached version
Fumler 4dc66cd
fix: Resolve promise instead of return
Fumler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ import { | |
getVendors, | ||
getLimitedVendors, | ||
getVendorsToDisplay, | ||
loadVendorList | ||
loadVendorList, | ||
cachedVendorList, | ||
pendingVendorlistPromise | ||
} from '../../../src/scripts/core/core_vendor_information'; | ||
import VENDOR_LIST from '../../fixtures/vendorlist/simple_vendor_list.json'; | ||
import { resetOil } from '../../test-utils/utils_reset'; | ||
|
@@ -52,6 +54,33 @@ describe('core_vendor_information', () => { | |
}); | ||
}); | ||
|
||
it('should wait for cached vendor list if request is already started', (done) => { | ||
let fetchSpy = spyOn(CoreUtils, 'fetchJsonData').and.returnValue(new Promise((resolve) => resolve(VENDOR_LIST))); | ||
spyOn(CoreConfig, 'getIabVendorListUrl').and.returnValue("https://iab.vendor.list.url"); | ||
|
||
expect(pendingVendorlistPromise).toBeNull(); | ||
expect(cachedVendorList).toBeUndefined(); | ||
loadVendorList().then((retrievedVendorList) => { | ||
expect(retrievedVendorList.vendorListVersion).toEqual(VENDOR_LIST.vendorListVersion); | ||
expect(retrievedVendorList).toEqual(VENDOR_LIST); | ||
expect(cachedVendorList).toBeDefined(); | ||
}); | ||
expect(cachedVendorList).toBeUndefined(); | ||
expect(pendingVendorlistPromise).toBeDefined(); | ||
loadVendorList().then((retrievedVendorList) => { | ||
expect(retrievedVendorList.vendorListVersion).toEqual(VENDOR_LIST.vendorListVersion); | ||
expect(retrievedVendorList).toEqual(VENDOR_LIST); | ||
}); | ||
expect(cachedVendorList).toBeUndefined(); | ||
loadVendorList().then((retrievedVendorList) => { | ||
expect(retrievedVendorList.vendorListVersion).toEqual(VENDOR_LIST.vendorListVersion); | ||
expect(retrievedVendorList).toEqual(VENDOR_LIST); | ||
}); | ||
expect(fetchSpy.calls.count()).toBe(1); | ||
|
||
done(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
}); | ||
|
||
it('should use default vendor list if vendor list fetching fails', (done) => { | ||
spyOn(CoreUtils, 'fetchJsonData').and.returnValue(new Promise((resolve, reject) => reject(new Error("something went wrong")))); | ||
spyOn(CoreConfig, 'getIabVendorListUrl').and.returnValue("https://iab.vendor.list.url"); | ||
|
@@ -273,7 +302,7 @@ describe('core_vendor_information', () => { | |
}); | ||
|
||
describe('getLimitedVendors', function() { | ||
|
||
it('returns regular vendors when no whitelist or blacklist exists', function() { | ||
spyOn(CoreConfig, 'getShowLimitedVendors').and.returnValue(true); | ||
expect(getLimitedVendors().length).toEqual(DEFAULT_VENDOR_LIST.maxVendorId); | ||
|
@@ -294,7 +323,7 @@ describe('core_vendor_information', () => { | |
}); | ||
|
||
describe('getVendorsToDisplay', function() { | ||
|
||
it('should return full vendor list when configuration parameter show_limited_vendors_only is false', function() { | ||
spyOn(CoreConfig, 'getShowLimitedVendors').and.returnValue(false); | ||
let result = getVendorsToDisplay(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
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:
loadVendorList
we return a promise, that when finished, will resolve to the vendorlist data.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 promiseloadVendorList
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 promiseThe 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.