-
Notifications
You must be signed in to change notification settings - Fork 10
Add test demonstrating cache collision #1771
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
Open
esoergel
wants to merge
1
commit into
master
Choose a base branch
from
es/cache-collision
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
src/test/java/org/commcare/formplayer/tests/CaseSearchCacheCollisionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package org.commcare.formplayer.tests; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.commcare.cases.instance.CaseInstanceTreeElement; | ||
| import org.commcare.cases.model.Case; | ||
| import org.commcare.cases.query.QueryContext; | ||
| import org.commcare.formplayer.sandbox.SqlStorage; | ||
| import org.commcare.modern.engine.cases.RecordObjectCache; | ||
| import org.javarosa.core.model.instance.InstanceBase; | ||
| import org.javarosa.core.services.storage.IStorageUtilityIndexed; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * Reproduces USH-6370: CaseInstanceTreeElement.getStorageCacheName() returns "casedb" | ||
| * for ALL instances, causing RecordObjectCache collisions between the user's casedb | ||
| * and case search results when they share a QueryContext. | ||
| */ | ||
| public class CaseSearchCacheCollisionTest { | ||
|
|
||
| private static final String DB_FILE = "test_cache_collision.db"; | ||
| private Connection connection; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws Exception { | ||
| new File(DB_FILE).delete(); | ||
| connection = DriverManager.getConnection("jdbc:sqlite:" + DB_FILE); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws Exception { | ||
| if (connection != null && !connection.isClosed()) { | ||
| connection.close(); | ||
| } | ||
| new File(DB_FILE).delete(); | ||
| } | ||
|
|
||
| /** | ||
| * Demonstrates that two CaseInstanceTreeElement instances — one for the user's | ||
| * casedb and one for case search results — return the same getStorageCacheName(). | ||
| * This shared cache key is the root cause of USH-6370. | ||
| */ | ||
| @Test | ||
| public void testStorageCacheNameCollision() { | ||
| SqlStorage<Case> casedbStorage = new SqlStorage<>( | ||
| () -> connection, Case.class, "user_casedb"); | ||
| SqlStorage<Case> searchResultsStorage = new SqlStorage<>( | ||
| () -> connection, Case.class, "search_results"); | ||
|
|
||
| CaseInstanceTreeElement casedbInstance = new CaseInstanceTreeElement( | ||
| new InstanceBase("casedb"), casedbStorage); | ||
| CaseInstanceTreeElement resultsInstance = new CaseInstanceTreeElement( | ||
| new InstanceBase("results"), searchResultsStorage); | ||
|
|
||
| // Both instances return "casedb" regardless of which storage they wrap | ||
| assertEquals(casedbInstance.getStorageCacheName(), resultsInstance.getStorageCacheName(), | ||
| "Both instances return the same storageCacheName, causing cache collisions"); | ||
| } | ||
|
|
||
| /** | ||
| * Demonstrates the full collision: when casedb records are bulk-loaded into the | ||
| * shared RecordObjectCache, a subsequent lookup for a search-results record with | ||
| * the same SQLite row ID returns the casedb record instead. | ||
| * | ||
| * In production this manifests as case search results showing data (case_type, | ||
| * case_name, etc.) from the user's casedb instead of from the search results. | ||
| */ | ||
| @Test | ||
| public void testRecordObjectCacheCrossContamination() { | ||
| // Two CaseInstanceTreeElement instances backed by different storages | ||
| IStorageUtilityIndexed<Case> casedbStorage = new SqlStorage<>( | ||
| () -> connection, Case.class, "user_casedb"); | ||
| IStorageUtilityIndexed<Case> searchResultsStorage = new SqlStorage<>( | ||
| () -> connection, Case.class, "search_results"); | ||
|
|
||
| CaseInstanceTreeElement casedbInstance = new CaseInstanceTreeElement( | ||
| new InstanceBase("casedb"), casedbStorage); | ||
| CaseInstanceTreeElement resultsInstance = new CaseInstanceTreeElement( | ||
| new InstanceBase("results"), searchResultsStorage); | ||
|
|
||
| // Create a shared QueryContext with scope above BULK_QUERY_THRESHOLD | ||
| // (in production, both instances share the EvaluationContext's QueryContext) | ||
| QueryContext context = new QueryContext(); | ||
| context = context.checkForDerivativeContextAndReturn(100); | ||
|
|
||
| // Simulate bulk loading from the casedb instance: a Case of type "person" | ||
| // gets loaded into the RecordObjectCache under key ("casedb", 1) | ||
| Case casedbCase = new Case("Alice", "person"); | ||
| casedbCase.setCaseId("casedb-case-1"); | ||
| casedbCase.setID(1); | ||
|
|
||
| RecordObjectCache<Case> recordCache = context.getQueryCache(RecordObjectCache.class); | ||
| String casedbCacheName = casedbInstance.getStorageCacheName(); | ||
| recordCache.getLoadedCaseMap(casedbCacheName).put(1, casedbCase); | ||
|
|
||
| // Now the search results instance has a different Case at record ID 1, | ||
| // type "facility" — but it will never see it through the cache | ||
| String resultsCacheName = resultsInstance.getStorageCacheName(); | ||
|
|
||
| // The cache keys are identical — this is the bug | ||
| assertEquals("casedb", casedbCacheName); | ||
| assertEquals("casedb", resultsCacheName); | ||
|
|
||
| // The results instance asks for record ID 1 and gets the casedb's Case | ||
| assertTrue(recordCache.isLoaded(resultsCacheName, 1), | ||
| "Cache reports record 1 as loaded for the results instance, " | ||
| + "even though it was loaded by the casedb instance"); | ||
|
|
||
| Case retrieved = recordCache.getLoadedRecordObject(resultsCacheName, 1); | ||
| assertEquals("person", retrieved.getTypeId(), | ||
| "Retrieved case has type 'person' (from casedb), not 'facility' (from search results)"); | ||
| assertEquals("Alice", retrieved.getName(), | ||
| "Retrieved case has name 'Alice' (from casedb), not the search result's name"); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The new test
CaseSearchCacheCollisionTest.javaasserts known incorrect behavior, verifying that two different storage instances produce the same cache name. This test will fail when the bug is fixed.Severity: LOW
Suggested Fix
The test should assert the correct behavior, which is that different storage instances have different cache names. Use
assertNotEqualsinstead ofassertEqualsto validate thatcasedbInstance.getStorageCacheName()andresultsInstance.getStorageCacheName()are distinct. Alternatively, if this test is intended to be temporary, add an@Disabledor@Ignoreannotation and a comment explaining that it should be updated or removed when the underlying bug is fixed.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.