diff options
author | Suprabh Shukla <suprabh@google.com> | 2016-09-19 16:35:35 -0700 |
---|---|---|
committer | Suprabh Shukla <suprabh@google.com> | 2016-09-26 17:01:38 -0700 |
commit | a2286cca9eb5e5e31a2803345438145b3f0641d1 (patch) | |
tree | f471e1613169cbe04f7ff3d512f5d4c38d57673c /tests | |
parent | d1b4bef03452af250ca3930cbfb2a52ad0ab0ab4 (diff) | |
download | ContactsProvider-a2286cca9eb5e5e31a2803345438145b3f0641d1.tar.gz |
Removing mimetype and package cache
We need to remove caches since after enabling WAL, we will have writers
work in non-exclusive transactions (b/31600627) which might lead to
readers reading uncommitted values if the cache is updated and the
transaction fails.
Test: Added a test testGetCommonMimeTypeIds to test the prepopulated
cache for common mimetypes added in this change. Verified that existing
tests in ContactsProvider2Test and ContactsDatabaseHelperTest pass.
Bug: 31599660
Change-Id: I804ae9e952029a7f1b702347f866cb5e5d951f3e
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/com/android/providers/contacts/ContactsDatabaseHelperTest.java | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/tests/src/com/android/providers/contacts/ContactsDatabaseHelperTest.java b/tests/src/com/android/providers/contacts/ContactsDatabaseHelperTest.java index ff6a7a2c..10cd2fb4 100644 --- a/tests/src/com/android/providers/contacts/ContactsDatabaseHelperTest.java +++ b/tests/src/com/android/providers/contacts/ContactsDatabaseHelperTest.java @@ -156,13 +156,9 @@ public class ContactsDatabaseHelperTest extends BaseContactsProvider2Test { } /** - * Test for {@link ContactsDatabaseHelper#getPackageId(String)} and - * {@link ContactsDatabaseHelper#getMimeTypeId(String)}. - * - * We test them at the same time here, to make sure they're not mixing up the caches. + * Test for {@link ContactsDatabaseHelper#getPackageId(String)} */ - public void testGetPackageId_getMimeTypeId() { - + public void testGetPackageId() { // Test for getPackageId. final long packageId1 = mDbHelper.getPackageId("value1"); final long packageId2 = mDbHelper.getPackageId("value2"); @@ -173,44 +169,51 @@ public class ContactsDatabaseHelperTest extends BaseContactsProvider2Test { set.add(packageId1); set.add(packageId2); set.add(packageId3); - assertEquals(3, set.size()); + // Make sure that repeated calls return the same value + assertEquals(packageId1, mDbHelper.getPackageId("value1")); + } + + /** + * Test for {@link ContactsDatabaseHelper#getMimeTypeId(String)} + */ + public void testGetMimeTypeId() { // Test for getMimeTypeId. final long mimetypeId1 = mDbHelper.getMimeTypeId("value1"); final long mimetypeId2 = mDbHelper.getMimeTypeId("value2"); final long mimetypeId3 = mDbHelper.getMimeTypeId("value3"); // Make sure they're all different. + final HashSet<Long> set = new HashSet<>(); set.clear(); set.add(mimetypeId1); set.add(mimetypeId2); set.add(mimetypeId3); - assertEquals(3, set.size()); - // Call with the same values and make sure they return the cached value. - final long packageId1b = mDbHelper.getPackageId("value1"); - final long mimetypeId1b = mDbHelper.getMimeTypeId("value1"); - - assertEquals(packageId1, packageId1b); - assertEquals(mimetypeId1, mimetypeId1b); - - // Make sure the caches are also updated. - assertEquals(packageId2, (long) mDbHelper.mPackageCache.get("value2")); - assertEquals(mimetypeId2, (long) mDbHelper.mMimetypeCache.get("value2")); - - // Clear the cache, but they should still return the values, selecting from the database. - mDbHelper.mPackageCache.clear(); - mDbHelper.mMimetypeCache.clear(); - assertEquals(packageId1, mDbHelper.getPackageId("value1")); + // Make sure repeated calls return the same value assertEquals(mimetypeId1, mDbHelper.getMimeTypeId("value1")); + } + + /** + * Test for cache {@link ContactsDatabaseHelper#mCommonMimeTypeIdsCache} which stores ids for + * common mime types for faster access. + */ + public void testGetCommonMimeTypeIds() { + // getMimeTypeId should return the same value as the value stored in the cache + for (String commonMimeType : ContactsDatabaseHelper.COMMON_MIME_TYPES) { + assertEquals(mDbHelper.mCommonMimeTypeIdsCache.get(commonMimeType).longValue(), + mDbHelper.getMimeTypeId(commonMimeType)); + } - // Empty the table - mDb.execSQL("DELETE FROM " + Tables.MIMETYPES); + // The ids should be available even after deleting them from the table + mDb.execSQL("DELETE FROM " + Tables.MIMETYPES + ";"); - // We should still have the cached value. - assertEquals(mimetypeId1, mDbHelper.getMimeTypeId("value1")); + for (String commonMimeType : ContactsDatabaseHelper.COMMON_MIME_TYPES) { + assertEquals(mDbHelper.mCommonMimeTypeIdsCache.get(commonMimeType).longValue(), + mDbHelper.getMimeTypeId(commonMimeType)); + } } /** |