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: fix cache error with geo country mock #4821

Merged
merged 15 commits into from
Jul 20, 2022
Merged

fix: fix cache error with geo country mock #4821

merged 15 commits into from
Jul 20, 2022

Conversation

JWhist
Copy link
Contributor

@JWhist JWhist commented Jul 18, 2022

🎉 Thanks for submitting a pull request! 🎉

Summary

Fixes error by validating that no geoCountry value is set before checking the cache.


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures your code follows our style guide and
    passes our tests.
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

@JWhist JWhist requested a review from a team as a code owner July 18, 2022 13:40
@JWhist JWhist self-assigned this Jul 18, 2022
@JWhist JWhist requested a review from MarcL July 18, 2022 13:40
@github-actions github-actions bot added the type: bug code to address defects in shipped code label Jul 18, 2022
@github-actions
Copy link

github-actions bot commented Jul 18, 2022

📊 Benchmark results

Comparing with c766d51

Package size: 221 MB

⬇️ 0.00% decrease vs. c766d51

^                  227 MB  227 MB  227 MB  227 MB  227 MB  227 MB  227 MB  227 MB                         
│  221 MB  221 MB   ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐   221 MB  221 MB  221 MB 
│   ┌──┐    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    ┌──┐    ┌──┐    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

@@ -43,7 +43,7 @@ const getGeoLocation = async ({ geoCountry, mode, offline, state }) => {

// If we have cached geolocation data and the `--geo` option is set to
// `cache`, let's try to use it.
if (cacheObject !== undefined && mode === 'cache') {
if (cacheObject !== undefined && mode === 'cache' && !geoCountry) {
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit of an edge case, but if you have something in the cache and it happens to be the same country as the one in geoCountry, it'd probably be better to return the cached content than the mock data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be confusing for someone to be mocking their own country but be consistently returned their own location though, it may lead them to believe the feature is not working. Maybe once the full country names and/or subdivisions are mockable this might be more feasible?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure there's ever any situation where you'd rather receive dummy data than an actual location from the country you're trying to emulate. But I'll defer to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, after thinking about it, I agree with you. I think I was originally looking down the road a bit more towards mocking subdivisions/states at which point the caching might be counterproductive. But seeing as that is probably either not going to happen, or a ways off at this point, I think your approach makes sense. I'll abstract out that logic into a function of its own, and it can be modified later if needed.

eduardoboucas
eduardoboucas previously approved these changes Jul 18, 2022
@JWhist
Copy link
Contributor Author

JWhist commented Jul 18, 2022

New caching function allows for the behavior like this in the event that a user mocks their own country:

  1. If a user passes --country alone with no --geo flag, then the mode will default to 'cache' and will use the cache object if the country matches that of the argument, otherwise the usual mock country.

  2. If a user passes --country --geo=mock then the cache will always be bypassed.

@@ -27,6 +27,9 @@ const mockLocation = {
subdivision: { code: 'CA', name: 'California' },
}

const isCacheEligible = (cacheObj, mode, country) =>
cacheObj !== undefined && mode === 'cache' && (cacheObj.data.country.code === country || !country)
Copy link
Member

Choose a reason for hiding this comment

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

If you run --country=XX, shouldn't mode become mock? That's what the docs say is happening, and I wonder if reflecting that in the code would make this simpler, because then you wouldn't need to change this check at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we change the mode to mock, I'll still have to change the conditional in order to use the cached location in the case of a match though, right? To something like this?

  const cacheObject = state.get(STATE_GEO_PROPERTY)
  if (geoCountry) mode = 'mock'

  // If we have cached geolocation data and the `--geo` option is set to
  // `cache`, let's try to use it.
  if (cacheObject !== undefined && (mode === 'cache' || cacheObject.data.country.code === geoCountry)) {
    const age = Date.now() - cacheObject.timestamp

@MarcL
Copy link
Contributor

MarcL commented Jul 19, 2022

New caching function allows for the behavior like this in the event that a user mocks their own country:

  1. If a user passes --country alone with no --geo flag, then the mode will default to 'cache' and will use the cache object if the country matches that of the argument, otherwise the usual mock country.
  2. If a user passes --country --geo=mock then the cache will always be bypassed.

Does this now need a documentation update @JWhist? The docs suggest that not passing the--geo flag will default to mock and not cache.

src/lib/geo-location.js Outdated Show resolved Hide resolved
src/lib/geo-location.js Show resolved Hide resolved
@eduardoboucas
Copy link
Member

Does this now need a documentation update @JWhist? The docs suggest that not passing the--geo flag will default to mock and not cache.

Personally, I think there's no need, because we're still respecting the country you set with --country and I think that's the most important thing. The only extra thing we're doing is saying that if that country matches something that you have in cache, we show the real data as opposed to some random street name etc.

Copy link
Contributor

@MarcL MarcL left a comment

Choose a reason for hiding this comment

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

👍🏻 Thanks for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect behaviour of "dev --country" command
3 participants