private void put(Cache<Long, String> cache, String value, boolean addToCacheRecords) { cache.put(KEY, value); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, cache.get(KEY))); } }
private void replace(Cache<Long, String> cache, String value, boolean addToCacheRecords) { cache.replace(KEY, value); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, cache.get(KEY))); } }
@Test public void getMisses() throws Exception { cache.get(1L); assertThat(onHeap.getMisses()).isEqualTo(1L); assertStat("OnHeap:MissCount").isEqualTo(1L); }
@Test public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "one"), Matchers.<CharSequence>equalTo("un")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one")); }
@Test public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "un", "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one")); }
@Test public void getHits() throws Exception { cache.put(1L, "a"); cache.get(1L); assertThat(onHeap.getHits()).isEqualTo(1L); assertStat("OnHeap:HitCount").isEqualTo(1L); }
@Test public void getHits() { cache.put(1L, "a"); cache.get(1L); assertThat(onHeap.getHits()).isEqualTo(0L); assertNoStat("OnHeap:HitCount"); }
@Test public void getExpirations() throws Exception { cache.put(1L, "a"); timeSource.advanceTime(TIME_TO_EXPIRATION); cache.get(1L); assertThat(onHeap.getExpirations()).isEqualTo(1L); assertStat("OnHeap:ExpirationCount").isEqualTo(1L); }
@Test public void testSimplePutWithExpiry_replace3Args() throws Exception { insert(testCache, getEntries()); assertThat(cacheSize(testCache), is(2)); manualTimeSource.setTimeMillis(1001); assertThat(testCache.replace(1, "one", "one#2"), is(false)); assertThat(testCache.get(1), is(nullValue())); assertThat(testCache.replace(2, "two", "two#2"), is(false)); assertThat(testCache.get(2), is(nullValue())); }
@Test public void putIfAbsent_absent() { createCacheManager(); cache = createCache(); assertThat(cache.putIfAbsent(1, "a")).isNull(); assertThat(cache.get(1)).isEqualTo("a"); changesOf(1, 1, 1, 0); }
@Test public void testDestroyLoop() throws Exception { for (int i = 0; i < 10; i++) { try (CacheManagerContainer cmc = new CacheManagerContainer(10, this::createCacheManager)) { // just put in one and get from another cmc.cacheManagerList.get(0).getCache(CACHE_NAME, Long.class, String.class).put(1L, "value"); assertThat(cmc.cacheManagerList.get(5).getCache(CACHE_NAME, Long.class, String.class).get(1L), is("value")); } destroyCacheManager(); } }
@Test public void testClusteredWriteBehindLoading() throws Exception { cache.put(KEY, "Some value"); checkValueFromLoaderWriter(cache, "Some value"); cache.clear(); assertThat(cache.get(KEY), notNullValue()); doThreadDump = false; } }
@Test public void testSimpleReplace() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))); testCache.put(1, "one"); assertThat(testCache.replace(1, "one_"), Matchers.<CharSequence>equalTo("one")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one_")); assertThat(testCache.replace(2, "two_"), is(nullValue())); }
@Test public void testCacheConfigUsage() { final CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("foo", CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class, heap(10)) .add(new DefaultCacheLoaderWriterConfiguration(MyLoader.class)) .build()).build(true); final Object foo = manager.getCache("foo", Object.class, Object.class).get(new Object()); assertThat(foo, is(MyLoader.object)); }
public static void testByteSizedOnHeapInOsgi() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().heap(10, MemoryUnit.KB)) .build()) .build(true); Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class); cache.put(42L, "I am out of heap!!"); cache.get(42L); } }
public static void testProgrammaticClusteredCache(OsgiTestUtils.Cluster cluster) throws Throwable { try (PersistentCacheManager cacheManager = newCacheManagerBuilder() .with(cluster(cluster.getConnectionUri()).autoCreate()) .withCache("clustered-cache", newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().with(clusteredDedicated("main", 2, MemoryUnit.MB)))) .build(true)) { final Cache<Long, String> cache = cacheManager.getCache("clustered-cache", Long.class, String.class); cache.put(1L, "value"); assertThat(cache.get(1L), is("value")); } }
public static void testEhcache3WithSerializationAndClientClass() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", newCacheConfigurationBuilder(Long.class, Person.class, heap(10)) .add(new DefaultCopierConfiguration<>(SerializingCopier.<Person>asCopierClass(), DefaultCopierConfiguration.Type.VALUE)) .withClassLoader(TestMethods.class.getClassLoader()) .build()) .build(true); Cache<Long, Person> myCache = cacheManager.getCache("myCache", Long.class, Person.class); myCache.put(42L, new Person("Arthur")); assertTrue(myCache.get(42L) instanceof Person); }
public static void testOffHeapInOsgi() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).offheap(10, MemoryUnit.MB)) .build()) .build(true); Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class); cache.put(42L, "I am out of heap!!"); cache.get(42L); }