Skip to content

Example of aggressive API cache as well as operation order issue#1249

Closed
taquitos wants to merge 1 commit into
mainfrom
api_cache_tests
Closed

Example of aggressive API cache as well as operation order issue#1249
taquitos wants to merge 1 commit into
mainfrom
api_cache_tests

Conversation

@joshdholtz

Copy link
Copy Markdown
Member

Yessssss! This successfully failed 🙌

@NachoSoto

Copy link
Copy Markdown
Contributor

Fixed in #1292.

@NachoSoto NachoSoto closed this Feb 14, 2022
@NachoSoto NachoSoto deleted the api_cache_tests branch February 14, 2022 20:01
NachoSoto added a commit that referenced this pull request Feb 15, 2022
Fixes [sc-12802].

Before this change, the sequence illustrated by the new test `testGetsUpdatedSubscriberInfoAfterPost` (started in #1249, with a few modifications here) would fail:
- GET /subscribers/
- POST /receipts
- GET /subscribers/

However, when the first GET request finished, the second one shared the cache key, so it received the (soon to be) outdated `CustomerInfo`.

Unfortunately this fix is isolated for this specific issue, and not a generalized one. A more comprehensive rewrite of `HTTPClient` is needed ([sc-9521]).
I came up with several other alternatives, but what I ended up going with here involves the least amount of moving pieces.

If a `GetCustomerInfoOperation` request is enqueued with `PostReceiptDataOperation` in the queue, then a new unique cache key is created to avoid past `GetCustomerInfoOperation` to share outdata data.

If we simply added a random number to the `GetCustomerInfoOperation` cache key, the new test would have also passed. No other test verified the caching behavior, which is what the new `testGetSubscriberInfoDoesNotMakeTwoRequests` checks.
NachoSoto added a commit that referenced this pull request Feb 15, 2022
Fixes [sc-12802].

## Background

Before this change, the sequence illustrated by the new test `testGetsUpdatedSubscriberInfoAfterPost` (started in #1249, with a few modifications here) would fail:
- GET /subscribers/
- POST /receipts
- GET /subscribers/

#1261 fixed the race condition in the test, making it so that operations are actually run serially.
However, when the first GET request finished, the second one shared the cache key, so it received the (soon to be) outdated `CustomerInfo`.

## The fix

Unfortunately this fix is isolated for this specific issue, and not a generalized one. A more comprehensive rewrite of `HTTPClient` is needed ([sc-9521]).
I came up with several other alternatives, but what I ended up going with here involves the least amount of moving pieces.

If a `GetCustomerInfoOperation` request is enqueued with `PostReceiptDataOperation` in the queue, then a new unique cache key is created to avoid past `GetCustomerInfoOperation` to share outdated data.

If we simply added a random number to the `GetCustomerInfoOperation` cache key, the new test would have also passed. No other test verified the caching behavior, which is what the new `testGetSubscriberInfoDoesNotMakeTwoRequests` checks.
NachoSoto added a commit to RevenueCat/purchases-android that referenced this pull request Feb 22, 2022
Android equivalent to RevenueCat/purchases-ios#1249 and RevenueCat/purchases-ios#1292.
Finishes [sc-12802].

## Background

Before this change, the sequence illustrated by the new test `"gets updated customer after posting receipt data"` would fail:
- GET /subscribers/
- POST /receipts
- GET /subscribers/

When the first GET request finishes, the second one shares the cache key, so it receives the (soon to be) outdated `CustomerInfo`.

## The fix

Unfortunately this fix is isolated for this specific issue, and not a generalized one.
I came up with several other alternatives, but what I ended up going with here involves the least amount of moving pieces.

If a `getCustomerInfo` request is enqueued with `postReceiptData` in the queue, then a new unique cache key is created to avoid past `getCustomerInfo` to share outdated data.

If we simply added a random number to the `getCustomerInfo` cache key,  the test `"given multiple get calls for same subscriber, only one is triggered"` fails, so that's still verifying the previous behavior works.
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.

3 participants