/** * Tests that we can save and load a persistent store in a repeatable way, and delete and add * data. */ @Test public void testFreeSpaceBehaviour() throws IOException, InterruptedException { // initialise String cacheName = "testPersistent"; Store diskStore = createPersistentDiskStore(cacheName); diskStore.removeAll(); byte[] data = new byte[1024]; for (int i = 0; i < 100; i++) { diskStore.put(new Element("key" + (i + 100), data)); } waitShorter(); assertEquals(ELEMENT_ON_DISK_SIZE * 100, diskStore.getOnDiskSizeInBytes()); assertEquals(100, diskStore.getSize()); manager.removeCache(cacheName); diskStore = createPersistentDiskStore(cacheName); File dataFile = ((DiskPersistentStore) diskStore).getDataFile(); assertTrue("File exists", dataFile.exists()); assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(100, diskStore.getSize()); diskStore.remove("key100"); diskStore.remove("key101"); diskStore.remove("key102"); diskStore.remove("key103"); diskStore.remove("key104"); diskStore.put(new Element("key100", data)); diskStore.put(new Element("key101", data)); waitShorter(); // The file does not shrink. assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(97, diskStore.getSize()); diskStore.put(new Element("key102", data)); diskStore.put(new Element("key103", data)); diskStore.put(new Element("key104", data)); diskStore.put(new Element("key201", data)); diskStore.put(new Element("key202", data)); waitShorter(); assertEquals(102 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(102, diskStore.getSize()); manager.removeCache(cacheName); assertTrue("File exists", dataFile.exists()); assertEquals(102 * ELEMENT_ON_DISK_SIZE, dataFile.length()); }
/** Tests bulk load. */ @Test public void testBulkLoad() throws Exception { final Random random = new Random(); StopWatch stopWatch = new StopWatch(); // Add a bunch of entries for (int i = 0; i < 500; i++) { // Use a random length value final String key = "key" + i; final String value = "value" + random.nextInt(1000); // Add an element, and make sure it is present Element element = new Element(key, value); store.put(element); element = store.get(key); assertNotNull(element); // Remove the element store.remove(key); element = store.get(key); assertNull(element); element = new Element(key, value); store.put(element); element = store.get(key); assertNotNull(element); } long time = stopWatch.getElapsedTime(); LOG.info("Time for Bulk Load: " + time); }
/** Tests bulk load. */ @Test public void testBulkLoad() throws Exception { final Store diskStore = createDiskStore(); final Random random = new Random(); // Add a bunch of entries for (int i = 0; i < 500; i++) { // Use a random length value final String key = "key" + i; final String value = "This is a value" + random.nextInt(1000); // Add an element, and make sure it is present Element element = new Element(key, value); diskStore.put(element); element = diskStore.get(key); assertNotNull(element); // Chuck in a delay, to give the spool thread a chance to catch up Thread.sleep(2); // Remove the element diskStore.remove(key); element = diskStore.get(key); assertNull(element); element = new Element(key, value); diskStore.put(element); element = diskStore.get(key); assertNotNull(element); // Chuck in a delay Thread.sleep(2); } }
/** Tests removing an entry. */ @Test public void testRemove() throws Exception { final Store diskStore = createDiskStore(); // Add the entry final String value = "value"; diskStore.put(new Element("key1", value)); diskStore.put(new Element("key2", value)); // Check the entry is there assertEquals(1, diskStore.getOnDiskSize()); assertEquals(2, diskStore.getSize()); assertNotNull(diskStore.get("key1")); assertNotNull(diskStore.get("key2")); // Remove it diskStore.remove("key1"); diskStore.remove("key2"); // Check the entry is not there assertEquals(0, diskStore.getOnDiskSize()); assertEquals(0, diskStore.getSize()); assertNull(diskStore.get("key1")); assertNull(diskStore.get("key2")); }
/** Test elements can be removed from the store */ protected void removeTest() throws IOException { Element element; element = new Element("key1", "value1"); store.put(element); assertEquals(1, store.getSize()); store.remove("key1"); assertEquals(0, store.getSize()); store.put(new Element("key2", "value2")); store.put(new Element("key3", "value3")); assertEquals(2, store.getSize()); assertNotNull(store.remove("key2")); assertEquals(1, store.getSize()); // Try to remove an object that is not there in the store assertNull(store.remove("key4")); assertEquals(1, store.getSize()); // check no NPE on key handling assertNull(store.remove(null)); }
/** Benchmark to test speed. */ @Test public void testBenchmarkPutGetRemove() throws Exception { final String key = "key"; byte[] value = new byte[500]; StopWatch stopWatch = new StopWatch(); // Add a bunch of entries for (int i = 0; i < 50000; i++) { Element element = new Element(key, value); store.put(element); store.get(key + i); } for (int i = 0; i < 50000; i++) { store.remove(key + i); } long time = stopWatch.getElapsedTime(); LOG.info("Time for benchmarkPutGetRemove: " + time); assertTrue("Too slow. Time was " + time, time < 500); }
/** * Tests that we can save and load a persistent store in a repeatable way, and delete and add * data. */ @Test public void testLoadPersistentStoreWithDelete() throws IOException, InterruptedException { // initialise String cacheName = "testPersistentWithDelete"; Store diskStore = createPersistentDiskStore(cacheName); diskStore.removeAll(); for (int i = 0; i < 100; i++) { byte[] data = new byte[1024]; diskStore.put(new Element("key" + (i + 100), data)); } waitShorter(); assertEquals(ELEMENT_ON_DISK_SIZE * 100, diskStore.getOnDiskSizeInBytes()); assertEquals(100, diskStore.getSize()); manager.removeCache(cacheName); diskStore = createPersistentDiskStore(cacheName); File dataFile = ((DiskPersistentStore) diskStore).getDataFile(); assertTrue("File exists", dataFile.exists()); assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(100, diskStore.getSize()); diskStore.remove("key100"); assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(99, diskStore.getSize()); manager.removeCache(cacheName); diskStore = createPersistentDiskStore(cacheName); assertTrue("File exists", dataFile.exists()); assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(99, diskStore.getSize()); diskStore.put(new Element("key100", new byte[1024])); diskStore.flush(); waitShorter(); assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length()); assertEquals(100, diskStore.getSize()); }
/** Tests removing an entry. */ @Test public void testRemove() throws Exception { final String value = "value"; final String key = "key"; // Add the entry Element element = new Element(key, value); store.put(element); // Check the entry is there assertEquals(1, store.getSize()); element = store.get(key); assertNotNull(element); // Remove it store.remove(key); // Check the entry is not there assertEquals(0, store.getSize()); element = store.get(key); assertNull(element); }
/** Check no NPE on remove */ @Test public void testNullRemove() throws IOException { assertNull(store.remove(null)); }