Esempio n. 1
0
  /** 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);
    }
  }
Esempio n. 2
0
  /** Tests the loading of classes */
  @Test
  public void testClassloading() throws Exception {
    final Store diskStore = createDiskStore();

    Long value = Long.valueOf(123L);
    diskStore.put(new Element("key1", value));
    diskStore.put(new Element("key2", value));
    Thread.sleep(1000);
    Element element1 = diskStore.get("key1");
    Element element2 = diskStore.get("key2");
    assertEquals(value, element1.getObjectValue());
    assertEquals(value, element2.getObjectValue());

    Primitive primitive = new Primitive();
    primitive.integerPrimitive = 123;
    primitive.longPrimitive = 456L;
    primitive.bytePrimitive = "a".getBytes()[0];
    primitive.charPrimitive = 'B';
    primitive.booleanPrimitive = false;

    // test Serializability
    ByteArrayOutputStream outstr = new ByteArrayOutputStream();
    ObjectOutputStream objstr = new ObjectOutputStream(outstr);
    objstr.writeObject(new Element("key", value));
    objstr.close();

    diskStore.put(new Element("primitive1", primitive));
    diskStore.put(new Element("primitive2", primitive));
    Thread.sleep(1000);
    Element primitive1 = diskStore.get("primitive1");
    Element primitive2 = diskStore.get("primitive2");
    assertEquals(primitive, primitive1.getObjectValue());
    assertEquals(primitive, primitive2.getObjectValue());
  }
Esempio n. 3
0
  /** 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);
  }
Esempio n. 4
0
  /** 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"));
  }
Esempio n. 5
0
  /** Tests for element expiry. */
  @Test
  public void testExpiry() throws Exception {
    // Create a diskStore with a cranked up expiry thread
    final Store diskStore = createDiskStore();

    // Add an element that will expire.
    diskStore.put(new Element("key1", "value", false, 1, 1));
    diskStore.put(new Element("key2", "value", false, 1, 1));
    assertNotNull(diskStore.get("key1"));
    assertNotNull(diskStore.get("key2"));
    assertEquals(2, diskStore.getSize());
    assertEquals(1, diskStore.getOnDiskSize());

    // Wait a couple of seconds
    Thread.sleep(3000);

    Element e1 = diskStore.get("key1");
    Element e2 = diskStore.get("key2");

    if (e1 != null) {
      assertNull(e2);
    } else if (e2 != null) {
      assertNull(e1);
    } else {
      fail("One of the Elements should have been in memory - and hence not be expired");
    }
  }
Esempio n. 6
0
  @Test
  public void testLFUEvictionFromDiskStore() throws IOException, InterruptedException {
    Cache cache =
        new Cache(
            "testNonPersistent",
            1,
            MemoryStoreEvictionPolicy.LFU,
            true,
            null,
            false,
            2000,
            1000,
            false,
            1,
            null,
            null,
            10);
    manager.addCache(cache);
    Store store = cache.getStore();

    Element element;

    assertEquals(0, store.getSize());

    for (int i = 0; i < 11; i++) {
      element = new Element("key" + i, "value" + i);
      cache.put(element);
      if (i > 0) {
        cache.get("key" + i);
        cache.get("key" + i);
        cache.get("key" + i);
        cache.get("key" + i);
      }
    }

    // allow to move through spool
    Thread.sleep(220);
    assertEquals(10, store.getOnDiskSize());

    element = new Element("keyNew", "valueNew");
    store.put(element);

    // allow to get out of spool
    Thread.sleep(220);
    assertEquals(10, store.getOnDiskSize());
    // check new element not evicted
    assertNotNull(store.get(element.getObjectKey()));
    // check evicted honours LFU policy
    assertNull(store.get("key0"));

    for (int i = 0; i < 2000; i++) {
      store.put(new Element("" + i, new Date()));
    }
    // wait for spool to empty
    waitLonger();

    assertEquals(10, store.getOnDiskSize());
  }
Esempio n. 7
0
 /** Tests looking up an entry that does not exist. */
 @Test
 public void testGetUnknownThenKnown() throws Exception {
   final Store diskStore = createDiskStore();
   assertNull(diskStore.get("key1"));
   diskStore.put(new Element("key1", "value"));
   diskStore.put(new Element("key2", "value"));
   assertNotNull(diskStore.get("key1"));
   assertNotNull(diskStore.get("key2"));
 }
Esempio n. 8
0
 @Test
 public void testSupportsCopyOnRead() {
   Element element = new Element(KEY, "Some String", 1);
   xaStore.put(element);
   Element copy = xaStore.get(KEY);
   assertNotNull(copy);
   assertNotSame(copy, xaStore.get(KEY));
   assertEquals("Some String", copy.getValue());
   assertEquals(copy.getValue(), xaStore.get(KEY).getValue());
   assertNotSame(copy.getValue(), xaStore.get(KEY).getValue());
 }
Esempio n. 9
0
  /** Tests adding an entry. */
  @Test
  public void testPut() throws Exception {
    final String value = "value";
    final String key = "key";

    // Make sure the element is not found
    assertEquals(0, store.getSize());
    Element element = store.get(key);
    assertNull(element);

    // Add the element
    element = new Element(key, value);
    store.put(element);

    // Get the element
    assertEquals(1, store.getSize());
    element = store.get(key);
    assertNotNull(element);
    assertEquals(value, element.getObjectValue());
  }
Esempio n. 10
0
 @Test
 public void testThrowsExceptionOnNonSerializableValue() {
   try {
     xaStore.put(new Element(KEY, new Object()));
     fail("Should have thrown an Exception");
   } catch (Exception e) {
     assertTrue(
         "Expected " + CacheException.class.getName() + ", but was " + e.getClass().getName(),
         e instanceof CacheException);
   }
   assertNull(xaStore.get(KEY));
 }
Esempio n. 11
0
  /** Tests removing all the entries. */
  @Test
  public void testRemoveAll() 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
    element = store.get(key);
    assertNotNull(element);

    // Remove it
    store.removeAll();

    // Check the entry is not there
    assertEquals(0, store.getSize());
    element = store.get(key);
    assertNull(element);
  }
Esempio n. 12
0
  /** Test elements can be put in the store */
  protected void putTest() throws IOException {
    Element element;

    assertEquals(0, store.getSize());

    element = new Element("key1", "value1");
    store.put(element);
    assertEquals(1, store.getSize());
    assertEquals("value1", store.get("key1").getObjectValue());

    element = new Element("key2", "value2");
    store.put(element);
    assertEquals(2, store.getSize());
    assertEquals("value2", store.get("key2").getObjectValue());

    for (int i = 0; i < 1999; i++) {
      store.put(new Element("" + i, new Date()));
    }

    assertEquals(4, store.getSize());
    assertEquals(2001, cache.getSize());
    assertEquals(3998, cache.getDiskStoreSize());

    /** Non serializable test class */
    class NonSerializable {
      //
    }

    store.put(new Element(new NonSerializable(), new NonSerializable()));

    assertEquals(4, store.getSize());
    assertEquals(2002, cache.getSize());
    assertEquals(1999, cache.getDiskStoreSize());
    //        assertEquals(1998, cache.getDiskStoreSize());    ???

    // smoke test
    for (int i = 0; i < 2000; i++) {
      store.get("" + i);
    }
  }
Esempio n. 13
0
  @Test
  public void testSupportsCopyOnWrite() {

    AtomicLong atomicLong = new AtomicLong(0);

    Element element = new Element(KEY, atomicLong, 1);
    atomicLong.getAndIncrement();
    xaStore.put(element);

    atomicLong.getAndIncrement();
    element.setVersion(2);

    assertEquals(1, ((AtomicLong) xaStore.get(KEY).getValue()).get());
    assertEquals(1, xaStore.get(KEY).getVersion());

    xaStore.put(new Element(KEY, atomicLong, 1));
    assertEquals(2, ((AtomicLong) xaStore.get(KEY).getValue()).get());
    atomicLong.getAndIncrement();

    assertEquals(2, ((AtomicLong) xaStore.get(KEY).getValue()).get());
    assertEquals(1, xaStore.get(KEY).getVersion());
  }
Esempio n. 14
0
  /** Tests adding an entry. */
  @Test
  public void testPut() throws Exception {
    final Store diskStore = createDiskStore();

    // Make sure the element is not found
    assertEquals(0, diskStore.getSize());
    assertNull(diskStore.get("key"));

    // Add the element
    final String value = "value";
    diskStore.put(new Element("key1", value));
    diskStore.put(new Element("key2", value));

    // Get the element
    assertEquals(2, diskStore.getSize());
    assertEquals(1, diskStore.getOnDiskSize());

    Element element1 = diskStore.get("key1");
    Element element2 = diskStore.get("key2");
    assertNotNull(element1);
    assertNotNull(element2);
    assertEquals(value, element1.getObjectValue());
    assertEquals(value, element2.getObjectValue());
  }
Esempio n. 15
0
  /** Benchmark to test speed. */
  @Test
  public void testBenchmarkPutGet() 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);
    }
    for (int i = 0; i < 50000; i++) {
      store.get(key + i);
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for benchmarkPutGet: " + time);
    assertTrue("Too slow. Time was " + time, time < 300);
  }
Esempio n. 16
0
  /** Benchmark to test speed. Original implementation 12seconds This implementation 9 seconds */
  public void benchmarkPutGetSuryaTest(long allowedTime) throws Exception {
    Random random = new Random();
    byte[] value = new byte[500];
    StopWatch stopWatch = new StopWatch();

    // Add a bunch of entries
    for (int i = 0; i < 50000; i++) {
      String key = "key" + i;

      Element element = new Element(key, value);
      store.put(element);

      // Access each element random number of times, min:0 maximum:9
      int accesses = random.nextInt(5);
      for (int j = 0; j <= accesses; j++) {
        store.get(key);
      }
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for benchmarkPutGetSurya: " + time);
    assertTrue("Too slow. Time was " + time, time < allowedTime);
  }
Esempio n. 17
0
 /** Tests looking up an entry that does not exist. */
 @Test
 public void testGetUnknown() throws Exception {
   final Element element = store.get("key");
   assertNull(element);
 }
Esempio n. 18
0
 /** Check no NPE on get */
 @Test
 public void testNullGet() throws IOException {
   assertNull(store.get(null));
 }