use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CacheManagerTest method testCacheInstance.
@Test
public void testCacheInstance() {
// Don't mock
teardown();
CacheManager manager = CacheManager.instance();
Cache<Id, Object> c1 = manager.cache("c1");
Cache<Id, Object> c12 = manager.cache("c1");
Cache<Id, Object> c13 = manager.cache("c1", 123);
Assert.assertEquals(c1, c12);
Assert.assertEquals(c1, c13);
Assert.assertEquals(c1.capacity(), c13.capacity());
Cache<Id, Object> c2 = manager.offheapCache(null, "c2", 1, 11);
Cache<Id, Object> c22 = manager.offheapCache(null, "c2", 2, 22);
Cache<Id, Object> c23 = manager.offheapCache(null, "c2", 3, 33);
Assert.assertEquals(c2, c22);
Assert.assertEquals(c2, c23);
Assert.assertEquals(c2.capacity(), c23.capacity());
Cache<Id, Object> c3 = manager.levelCache(null, "c3", 1, 1, 11);
Cache<Id, Object> c32 = manager.levelCache(null, "c3", 2, 2, 22);
Cache<Id, Object> c33 = manager.levelCache(null, "c3", 3, 3, 33);
Assert.assertEquals(c3, c32);
Assert.assertEquals(c3, c33);
Assert.assertEquals(c3.capacity(), c33.capacity());
Assert.assertThrows(IllegalArgumentException.class, () -> {
manager.cache("c2");
}, e -> {
Assert.assertContains("Invalid cache implement:", e.getMessage());
Assert.assertContains("OffheapCache", e.getMessage());
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
manager.cache("c3");
}, e -> {
Assert.assertContains("Invalid cache implement:", e.getMessage());
Assert.assertContains("LevelCache", e.getMessage());
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
manager.offheapCache(null, "c1", 1, 11);
}, e -> {
Assert.assertContains("Invalid cache implement:", e.getMessage());
Assert.assertContains("RamCache", e.getMessage());
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
manager.offheapCache(null, "c3", 1, 11);
}, e -> {
Assert.assertContains("Invalid cache implement:", e.getMessage());
Assert.assertContains("LevelCache", e.getMessage());
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
manager.levelCache(null, "c1", 1, 1, 11);
}, e -> {
Assert.assertContains("Invalid cache implement:", e.getMessage());
Assert.assertContains("RamCache", e.getMessage());
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
manager.levelCache(null, "c2", 1, 1, 11);
}, e -> {
Assert.assertContains("Invalid cache implement:", e.getMessage());
Assert.assertContains("OffheapCache", e.getMessage());
});
this.originCaches.remove("c1");
this.originCaches.remove("c2");
this.originCaches.remove("c3");
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CacheManagerTest method testCacheEnableMetrics.
@Test
public void testCacheEnableMetrics() {
// Don't mock
teardown();
CacheManager manager = CacheManager.instance();
Cache<Id, Object> c1 = manager.cache("m1");
Cache<Id, Object> c2 = manager.cache("m2");
Cache<Id, Object> c3 = manager.offheapCache(null, "m3", 1, 11);
Cache<Id, Object> c4 = manager.levelCache(null, "m4", 1, 1, 11);
Assert.assertEquals(false, c1.enableMetrics(false));
Assert.assertEquals(false, c2.enableMetrics(false));
Assert.assertEquals(false, c3.enableMetrics(false));
Assert.assertEquals(false, c4.enableMetrics(false));
Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m1", true));
Assert.assertEquals(true, c1.enableMetrics(true));
Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m2", true));
Assert.assertEquals(true, c2.enableMetrics(true));
Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m3", true));
Assert.assertEquals(true, c3.enableMetrics(true));
Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m4", true));
Assert.assertEquals(true, c4.enableMetrics(true));
Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m1", false));
Assert.assertEquals(false, c1.enableMetrics(true));
Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m2", false));
Assert.assertEquals(false, c2.enableMetrics(true));
Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m3", false));
Assert.assertEquals(false, c3.enableMetrics(true));
Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m4", false));
Assert.assertEquals(false, c4.enableMetrics(true));
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CacheTest method testUpdateIfAbsentWithExistKey.
@Test
public void testUpdateIfAbsentWithExistKey() {
Cache<Id, Object> cache = newCache();
Id id = IdGenerator.of("1");
cache.update(id, "value-1");
cache.updateIfAbsent(id, "value-2");
Assert.assertEquals("value-1", cache.get(id));
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CacheTest method testKeyExpired.
@Test
public void testKeyExpired() {
Cache<Id, Object> cache = newCache();
cache.expire(2000L);
Id key = IdGenerator.of("key");
cache.update(key, "value", -1000L);
waitTillNext(1);
cache.tick();
Assert.assertFalse(cache.containsKey(key));
cache.update(key, "value", -2000L);
cache.tick();
Assert.assertFalse(cache.containsKey(key));
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CacheTest method testGetOrFetch.
@Test
public void testGetOrFetch() {
Cache<Id, Object> cache = newCache();
Id id = IdGenerator.of("1");
Assert.assertNull(cache.get(id));
Assert.assertEquals("value-1", cache.getOrFetch(id, key -> {
return "value-1";
}));
cache.update(id, "value-2");
Assert.assertEquals("value-2", cache.getOrFetch(id, key -> {
return "value-1";
}));
}
Aggregations