fix KeyCacheTest to work with globalized key cache

Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/422d2236
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/422d2236
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/422d2236

Branch: refs/heads/cassandra-1.2
Commit: 422d2236b59b80e3cafef9fb8cad3235280e8626
Parents: ce4e4b9
Author: Yuki Morishita <yu...@apache.org>
Authored: Wed Jul 24 10:58:07 2013 -0500
Committer: Yuki Morishita <yu...@apache.org>
Committed: Wed Jul 24 11:00:05 2013 -0500

----------------------------------------------------------------------
 .../org/apache/cassandra/db/KeyCacheTest.java   | 75 +++++++++++++-------
 1 file changed, 49 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/422d2236/test/unit/org/apache/cassandra/db/KeyCacheTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/KeyCacheTest.java 
b/test/unit/org/apache/cassandra/db/KeyCacheTest.java
index 93f1fea..3458961 100644
--- a/test/unit/org/apache/cassandra/db/KeyCacheTest.java
+++ b/test/unit/org/apache/cassandra/db/KeyCacheTest.java
@@ -1,6 +1,4 @@
-package org.apache.cassandra.db;
 /*
- *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -9,35 +7,34 @@ package org.apache.cassandra.db;
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
+package org.apache.cassandra.db;
 
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
 
-import org.apache.cassandra.cache.KeyCacheKey;
-import org.apache.cassandra.db.filter.QueryFilter;
-import org.apache.cassandra.service.CacheService;
-import org.apache.cassandra.thrift.ColumnParent;
-
 import org.junit.AfterClass;
 import org.junit.Test;
 
 import org.apache.cassandra.SchemaLoader;
 import org.apache.cassandra.Util;
-import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.cache.KeyCacheKey;
 import org.apache.cassandra.db.compaction.CompactionManager;
+import org.apache.cassandra.db.filter.QueryFilter;
+import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.service.CacheService;
+import org.apache.cassandra.thrift.ColumnParent;
 import org.apache.cassandra.utils.ByteBufferUtil;
+
 import static junit.framework.Assert.assertEquals;
 
 public class KeyCacheTest extends SchemaLoader
@@ -61,7 +58,7 @@ public class KeyCacheTest extends SchemaLoader
 
         // empty the cache
         CacheService.instance.invalidateKeyCache();
-        assert CacheService.instance.keyCache.size() == 0;
+        assertKeyCacheSize(0, TABLE1, COLUMN_FAMILY2);
 
         // insert data and force to disk
         insertData(TABLE1, COLUMN_FAMILY2, 0, 100);
@@ -69,20 +66,37 @@ public class KeyCacheTest extends SchemaLoader
 
         // populate the cache
         readData(TABLE1, COLUMN_FAMILY2, 0, 100);
-        assertEquals(100, CacheService.instance.keyCache.size());
+        assertKeyCacheSize(100, TABLE1, COLUMN_FAMILY2);
 
         // really? our caches don't implement the map interface? (hence no 
.addAll)
         Map<KeyCacheKey, RowIndexEntry> savedMap = new HashMap<KeyCacheKey, 
RowIndexEntry>();
         for (KeyCacheKey k : CacheService.instance.keyCache.getKeySet())
         {
-            savedMap.put(k, CacheService.instance.keyCache.get(k));
+            if (k.desc.ksname.equals(TABLE1) && 
k.desc.cfname.equals(COLUMN_FAMILY2))
+                savedMap.put(k, CacheService.instance.keyCache.get(k));
         }
 
         // force the cache to disk
         CacheService.instance.keyCache.submitWrite(Integer.MAX_VALUE).get();
 
         CacheService.instance.invalidateKeyCache();
-        assert CacheService.instance.keyCache.size() == 0;
+        assertKeyCacheSize(0, TABLE1, COLUMN_FAMILY2);
+
+        CacheService.instance.keyCache.loadSaved(store);
+        assertKeyCacheSize(savedMap.size(), TABLE1, COLUMN_FAMILY2);
+
+        // probably it's better to add equals/hashCode to RowIndexEntry...
+        for (Map.Entry<KeyCacheKey, RowIndexEntry> entry : savedMap.entrySet())
+        {
+            RowIndexEntry expected = entry.getValue();
+            RowIndexEntry actual = 
CacheService.instance.keyCache.get(entry.getKey());
+            assertEquals(expected.position, actual.position);
+            assertEquals(expected.columnsIndex(), actual.columnsIndex());
+            if (expected.isIndexed())
+            {
+                assertEquals(expected.deletionTime(), actual.deletionTime());
+            }
+        }
     }
 
     @Test
@@ -97,8 +111,7 @@ public class KeyCacheTest extends SchemaLoader
         CacheService.instance.invalidateKeyCache();
 
         // KeyCache should start at size 0 if we're caching X% of zero data.
-        int keyCacheSize = CacheService.instance.keyCache.size();
-        assert keyCacheSize == 0 : keyCacheSize;
+        assertKeyCacheSize(0, TABLE1, COLUMN_FAMILY1);
 
         DecoratedKey key1 = Util.dk("key1");
         DecoratedKey key2 = Util.dk("key2");
@@ -130,13 +143,12 @@ public class KeyCacheTest extends SchemaLoader
                                                        false,
                                                        10));
 
-        assert CacheService.instance.keyCache.size() == 2;
+        assertKeyCacheSize(2, TABLE1, COLUMN_FAMILY1);
 
         Util.compactAll(cfs).get();
-        keyCacheSize = CacheService.instance.keyCache.size();
         // after compaction cache should have entries for
         // new SSTables, if we had 2 keys in cache previously it should become 
4
-        assert keyCacheSize == 4 : keyCacheSize;
+        assertKeyCacheSize(4, TABLE1, COLUMN_FAMILY1);
 
         // re-read same keys to verify that key cache didn't grow further
         cfs.getColumnFamily(QueryFilter.getSliceFilter(key1,
@@ -153,6 +165,17 @@ public class KeyCacheTest extends SchemaLoader
                                                        false,
                                                        10));
 
-        assert CacheService.instance.keyCache.size() == 4;
+        assertKeyCacheSize(4, TABLE1, COLUMN_FAMILY1);
+    }
+
+    private void assertKeyCacheSize(int expected, String keyspace, String 
columnFamily)
+    {
+        int size = 0;
+        for (KeyCacheKey k : CacheService.instance.keyCache.getKeySet())
+        {
+            if (k.desc.ksname.equals(keyspace) && 
k.desc.cfname.equals(columnFamily))
+                size++;
+        }
+        assertEquals(expected, size);
     }
 }

Reply via email to