Пример #1
0
  /** Tests that we can save and load a persistent store in a repeatable way */
  @Test
  public void testLoadPersistentStore() throws IOException, InterruptedException {
    // initialise
    String cacheName = "testLoadPersistent";
    Store store = createPersistentDiskStore(cacheName);
    store.removeAll();
    waitShorter();

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      store.put(new Element("key" + (i + 100), data));
    }
    store.flush();
    waitShorter();
    assertEquals(ELEMENT_ON_DISK_SIZE * 100, store.getOnDiskSizeInBytes());
    assertEquals(100, store.getSize());
    manager.removeCache(cacheName);
    Thread.sleep(3000);
    // check that we can create and dispose several times with no problems and no lost data
    for (int i = 0; i < 10; i++) {
      store = createPersistentDiskStore(cacheName);
      File dataFile = ((DiskPersistentStore) store).getDataFile();
      assertTrue("File exists", dataFile.exists());
      assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length());
      assertEquals(100, store.getSize());

      manager.removeCache(cacheName);

      assertTrue("File exists", dataFile.exists());
      assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length());
    }
  }
Пример #2
0
  /**
   * 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());
  }