Skip to content

Commit

Permalink
fixed a bug in cache loader asynchronous load all
Browse files Browse the repository at this point in the history
  • Loading branch information
ocafebabe committed Feb 24, 2016
1 parent 8789227 commit e2c6dcd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/main/java/ca/exprofesso/guava/jcache/GuavaCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,14 @@ public void run()

for (K key : keys)
{
if (!view.containsKey(key) || replaceExistingValues)
if (!view.containsKey(key))
{
loadingCache.get(key);
}
else if (replaceExistingValues)
{
loadingCache.refresh(key);
}
}
}
}
Expand Down
48 changes: 31 additions & 17 deletions src/test/java/ca/exprofesso/guava/jcache/GuavaCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.cache.Cache;
import javax.cache.CacheManager;
Expand Down Expand Up @@ -52,6 +52,8 @@
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Sets;

public class GuavaCacheTest
{
private CachingProvider cachingProvider;
Expand Down Expand Up @@ -238,10 +240,7 @@ public void testRemoveAllWithKeys()
cache.put("2", 2);
cache.put("3", 3);

Set<String> keys = new HashSet<>();

keys.add("1");
keys.add("3");
Set<String> keys = Sets.newHashSet("1", "3");

cache.removeAll(keys);

Expand Down Expand Up @@ -336,11 +335,7 @@ public CacheLoader<String, Integer> create()
assertEquals(Integer.valueOf(2), loadingCache.get("2"));
assertEquals(Integer.valueOf(3), loadingCache.get("3"));

Set<String> keys = new HashSet<>();

keys.add("4");
keys.add("5");
keys.add("6");
Set<String> keys = Sets.newHashSet("4", "5", "6");

Map<String, Integer> map = loadingCache.getAll(keys);

Expand All @@ -354,12 +349,16 @@ public CacheLoader<String, Integer> create()
public void testCacheLoaderAsyncLoadAll()
throws InterruptedException
{
final AtomicInteger loads = new AtomicInteger();

final CacheLoader<String, Integer> cacheLoader = new CacheLoader<String, Integer>()
{
@Override
public Integer load(String key)
throws CacheLoaderException
{
loads.incrementAndGet();

return Integer.valueOf(key);
}

Expand Down Expand Up @@ -405,11 +404,22 @@ public CacheLoader<String, Integer> create()

Cache<String, Integer> loadingCache = cacheManager.createCache("loadingCache", custom);

Set<String> keys = new HashSet<>();
loadingCache.put("1", 1);
loadingCache.put("2", 2);
loadingCache.put("3", 3);

Set<String> keys = Sets.newHashSet("1", "2", "3", "4", "5", "6");

loadingCache.loadAll(keys, false, completionListener);

while (!completed.get())
{
Thread.sleep(250);
}

assertEquals(3, loads.getAndSet(0));

keys.add("1");
keys.add("2");
keys.add("3");
completed.set(false);

loadingCache.loadAll(keys, true, completionListener);

Expand All @@ -418,9 +428,13 @@ public CacheLoader<String, Integer> create()
Thread.sleep(250);
}

assertTrue(loadingCache.remove("1"));
assertTrue(loadingCache.remove("2"));
assertTrue(loadingCache.remove("3"));
assertEquals(6, loads.get());
assertEquals(Integer.valueOf(1), loadingCache.getAndRemove("1"));
assertEquals(Integer.valueOf(2), loadingCache.getAndRemove("2"));
assertEquals(Integer.valueOf(3), loadingCache.getAndRemove("3"));
assertEquals(Integer.valueOf(4), loadingCache.getAndRemove("4"));
assertEquals(Integer.valueOf(5), loadingCache.getAndRemove("5"));
assertEquals(Integer.valueOf(6), loadingCache.getAndRemove("6"));
}

@Test
Expand Down

0 comments on commit e2c6dcd

Please sign in to comment.