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: add tests for observing purged tags + fix edge cases #5249

Merged
merged 6 commits into from
Jun 22, 2024

Conversation

usu
Copy link
Member

@usu usu commented May 28, 2024

I added some tests tests for observing and checking the purged cache tags during create/delete/update operations. In the end-to-end test we only implicitly check cache purging by verifying whether the next request is a cache hit or miss. Explicitly checking the list of purged cache tags gives us a more granular testing behavior.

At the moment, I only added this tests to Category and ContentNode. If we like the concept, I can add this to other endpoints as well.

For an overview of new tests, see 1st commit: 6bd7409

During writing these tests I noticed 2 edge cases, which are also fixed in this PR. Both these edge cases have no direct implication at the moment, but it's better to fix them already now.

Edge case 1: When purging collections, we also need to purge potential abstract parent classes

Example: When adding a new ColumnLayout, we already purge "/content_node/column_layouts". However, we also need to purge the generic endpoint "/content_nodes".

At the moment we don't cache the ContentNode endpoints. But better to fix this now to be prepared, once we want to cache ContentNodes.

This is fixed with the 2nd commit: 221b3a3

Edge case 2: Many-to-many relations are not properly purged

Example: When updating the property preferredContentTypes of a category, we should also purge the tag {contentTypeId}#categories

Category and ContentType are connected via Many-to-Many relation.

These tags were previously missed, because changes to ManyToMany-realations are not included in the entity changeSet computed by Doctrine via getEntityChangeSet(). Such changes need to be queried separeately via $uow->getScheduledCollectionUpdates().

For the specific example of preferredContentTypes, this is irrelevant: The categories property is not serialized on ContentType, hence the {contentTypeId}#categories tag is not included in the ContentType response (see the list of cache tags in the e2e test) . Nevertheless, better to fix this now to be prepared for other Many-To-Many relations.

This is fixed with 3rd commit:
f315baf

@usu usu marked this pull request as ready for review June 1, 2024 06:52
@BacLuc BacLuc added test-flaky-e2e! Add this label to a PR to run e2e tests multiple times api-performance-test! Run API Performance test labels Jun 3, 2024
/**
* mocks CacheManager and keeps track of purged cache tags.
*/
protected function &getPurgedCacheTags(): array {
Copy link
Contributor

Choose a reason for hiding this comment

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

to improve this further we could use a class which implements the cacheManager, but allows to get the purged cache tags, instead of sharing a reference to an array

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, good idea. I can give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed with 0d7296c

@@ -254,6 +254,21 @@ public function testCreateResponseStructureMatchesReadResponseStructure() {
assertThat($createArray, CompatibleHalResponse::isHalCompatibleWith($getItemResponse->toArray()));
}

public function testCreatePurgesCacheTags() {
$client = static::createClientWithCredentials();
$purgedCacheTags = &$this->getPurgedCacheTags();
Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't know the & (dereference) operator, it is difficult to know why this array would be mutated with the $client->request method.

And that it does not work anymore when you call the method a second time

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed with 0d7296c

@pmattmann pmattmann added this pull request to the merge queue Jun 22, 2024
Merged via the queue into ecamp:devel with commit 6ebd663 Jun 22, 2024
61 checks passed
@BacLuc BacLuc mentioned this pull request Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-performance-test! Run API Performance test test-flaky-e2e! Add this label to a PR to run e2e tests multiple times
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants