EntityResourceTestBase::testGet() has some very specific testing of how many cache redirects there should be stored in DynamicPageCacheSubscriber (which uses the render cache behind the scenes).
By testing the caching layer when you needn't, you are making it harder to make changes to said caching layer as seemingly unrelated tests such as this one will go red when you change how the cache works. See #2551419-130: Abstract RenderCache into a separate service that is capable of cache redirects in a non-render array-specific way for an example of how it breaks unrelated patches.
The fix here is to adapt the code introduced in #2877778: Harden test EntityResource + Dynamic Page Cache test coverage to simply check if there are any redirects, not how many there are. This allows us to adjust how many redirects the cache might introduce without having seemingly unrelated test fails occur.
I'm still not happy that it tests whether the response is a redirect by using the code below, but for now I see no other easy way around that.
if (!isset($cached_data['#cache_redirect'])) {
$cached_response = $cached_data['#response'];
Comment | File | Size | Author |
---|---|---|---|
#14 | interdiff_13-14.txt | 673 bytes | vsujeetkumar |
#14 | 3082053-14.patch | 1.88 KB | vsujeetkumar |
#13 | 3082053-13.patch | 1.86 KB | vsujeetkumar |
#2 | drupal-3082053-2.patch | 2.38 KB | kristiaanvandeneynde |
Comments
Comment #2
kristiaanvandeneyndeAttached patch simplifies the check to merely see whether there are any cache items. It no longer checks if there are any redirects. I also simplified the query and loop because $cid was not being used.
Comment #3
Wim LeersI'm fine with not testing how many redirects there are. But testing that there are some redirects is necessary, that's what #2877778: Harden test EntityResource + Dynamic Page Cache test coverage introduced, at the request of a core committer.
Comment #4
kristiaanvandeneyndeWell, that's implicitly taken care of, no? We check that the DB returns one or more results, knowing that some of them might be redirects. That's what the
if (!isset($cached_data['#cache_redirect'])) {
further down is for.The whole reason we don't want to specifically check for the amount of redirects is because it's up to the cache to decide whether it needs to add them. So we should only make sure that if there are redirects, we handle them, which the code still does.
If we don't commit the patch as attached, this test will always go red when an optimization of RenderCache or a replacement (in my case) happens that manages to remove the need for redirects in some scenarios. And no-one likes it when tests from totally unrelated sections of the codebase go red when you're in the process of optimizing another layer, right?
The "Proposed resolution" from the original issue literally says: "Because there's always a cache redirect, assert the number of cache items." But that statement is dangerous because you cannot know this will always be the case. We still keep the
$found_cached_response
check and judging from the original issue, that one was the most important one.Edit: Even the current RenderCache will not issue a redirect if the pre-bubbling and post-bubbling element have the same cache ID. So it's really dangerous to assume that there will always be redirects.
Comment #5
Wim LeersThat is … very implicit. Tests should be explicit.
Except it's not "totally unrelated" when we're specifically asserting the storage characteristics to guarantee predictable performance.
Well, actually, we do. That's guaranteed to be the case in Dynamic Page Cache today. That's what you propose to change.
… but that can only ever occur in cached render fragments, not for cached REST/JSON:API responses. Because those responses always need to have access checking, so it means that at a minimum the
user.permissions
cache context will be present in the post-bubbling scenario. So this caveat you added only applies to the general case, not to the Dynamic Page Cache case.I'm fine with removing this, but then we need to make the test more clear. That's all.
Comment #6
kristiaanvandeneyndeHow would you suggest we change the test?
I agree with #5 that DPC will have redirects (for now at least) and that it wouldn't be an issue for this test specifically. But I still feel that we have too many tests in core that are testing behavior from other layers that is already covered in dedicated tests. Consider #4 more of a general concern about these types of tests rather than this one in specific.
Comment #8
Wim LeersI took another fresh look at this. I think you're right, actually :)
But here's one suggestion to at least retain the same spirit in testing:
Can't we change this to
?
That way we at least continue to test the upper bound, but we did remove the lower bound, which is what you care about :)
Comment #12
joachim CreditAttribution: joachim at Factorial GmbH commentedSetting to needs work based on #8.
Comment #13
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedRe-roll patch created for 9.3.x.
Comment #14
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedAddress #8, Please have a look.
Comment #17
borisson_With Wim's approval in #8 this looks good to go, #14 still applies (with some offset, but that should be fine?).
I agree that it is important that we test the upper bound of the amount of cache redirects, and the test still does that right now.
Comment #18
alexpottCommitted and pushed f8b4d7a105 to 10.1.x and 8d95ff2f1d to 10.0.x and 36cfac4274 to 9.5.x. Thanks!
Backported to 9.5.x to keep tests aligned.