(dev/core#174) CRM_Utils_Cache_SqlGroup - Refine trivial TTL handling to stabilize tests #12427
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.
Overview
Since incorporting
E2E_Cache_SqlGroupTest
, we've observed periodic flakiness in someof the test-runs around code like this:
This patch makes the tests more reliable by refining the way
SqlGroup
handles trivially short TTLs.Before
Cache items with
$ttl=1
may be expired before you ever get a chance to read them.After
Cache items with
$ttl=1
may sometimes last slightly longer than a second(if recorded late in the clock's current second), which improves the odds that
you'll be able to read the cache.
Technical Details
To reproduce the problem, run this script a few times via
cv scr
:Given that
$cache->set()
and$cache->get()
are right next to each other,you would expect (and the test authors did evidently expect) that cache would be valid
long enough to read content. And yet we periodically have a cache-miss (particularly with
SqlGroup
, which is a bit slower to handle writes).For example, suppose
microtime()===100.95
. If you compute expiration as$expires=time()+$ttl
, then$expires===101
. However, this means thatthe cache's effective validity period is more like
~0.05
seconds ratherthan a full second.
A more correct solution would be to track expiration times as microseconds.
However, that's a bigger patch, and we don't really need that level of
precision -- typical TTLs for us are more like "a week" or "a day" or "an
hour" or maybe "a minute". The
$ttl=1
is a trivial scenario that onlymakes sense for unit-testing.
This patch just rounds up the calculation of
$expires
-- in ourhypothetical at
microtime()===100.95
, that makes$expires===102
and aneffective validity period of
~1.05
seconds.