Skip to content

Commit

Permalink
Test LocalCache when async refresh takes longer than expire-after-w…
Browse files Browse the repository at this point in the history
…rite duration.

(followup to cl/605069776 / #6851)

Also, restore the use of `ConcurrentMapTestSuiteBuilder` in the mainline. It was added in cl/94773095 but then lost during cl/132882204 in the mainline only.

Fixes #7038

RELNOTES=n/a
PiperOrigin-RevId: 610388763
  • Loading branch information
paggynie authored and Google Java Core Libraries committed Mar 14, 2024
1 parent 41d0d9a commit 361fe5d
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 24 deletions.
Expand Up @@ -25,9 +25,12 @@
import static com.google.common.cache.TestingRemovalListeners.countingRemovalListener;
import static com.google.common.cache.TestingRemovalListeners.queuingRemovalListener;
import static com.google.common.cache.TestingWeighers.constantWeigher;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.immutableEntry;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator;
import static java.lang.Thread.State.WAITING;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
Expand All @@ -47,6 +50,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder;
Expand All @@ -58,10 +62,14 @@
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import com.google.common.testing.TestLogHandler;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.SettableFuture;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -247,11 +255,24 @@ private void checkLogged(Throwable t) {
assertSame(t, popLoggedThrowable());
}

/*
* TODO(cpovirk): Can we replace makeLocalCache with a call to builder.build()? Some tests may
* need access to LocalCache APIs, but maybe we can at least make makeLocalCache use
* builder.build() and then cast?
*/

private static <K, V> LocalCache<K, V> makeLocalCache(
CacheBuilder<? super K, ? super V> builder) {
return new LocalCache<>(builder, null);
}

private static <K, V> LocalCache<K, V> makeLocalCache(
CacheBuilder<? super K, ? super V> builder, CacheLoader<? super K, V> loader) {
return new LocalCache<>(builder, loader);
}

// TODO(cpovirk): Inline createCacheBuilder()?

private static CacheBuilder<Object, Object> createCacheBuilder() {
return CacheBuilder.newBuilder();
}
Expand Down Expand Up @@ -516,6 +537,57 @@ public void testSetRefresh() {
assertEquals(unit.toNanos(duration), map.refreshNanos);
}

public void testLongAsyncRefresh() throws Exception {
FakeTicker ticker = new FakeTicker();
CountDownLatch reloadStarted = new CountDownLatch(1);
SettableFuture<Thread> threadAboutToBlockForRefresh = SettableFuture.create();

ListeningExecutorService refreshExecutor = listeningDecorator(newSingleThreadExecutor());
try {
CacheBuilder<Object, Object> builder =
createCacheBuilder()
.expireAfterWrite(100, MILLISECONDS)
.refreshAfterWrite(5, MILLISECONDS)
.ticker(ticker);

CacheLoader<String, String> loader =
new CacheLoader<String, String>() {
@Override
public String load(String key) {
return key + "Load";
}

@Override
public ListenableFuture<String> reload(String key, String oldValue) {
return refreshExecutor.submit(
() -> {
reloadStarted.countDown();

Thread blockingForRefresh = threadAboutToBlockForRefresh.get();
while (blockingForRefresh.isAlive()
&& blockingForRefresh.getState() != WAITING) {
Thread.yield();
}

return key + "Reload";
});
}
};
LocalCache<String, String> cache = makeLocalCache(builder, loader);

assertThat(cache.getOrLoad("test")).isEqualTo("testLoad");

ticker.advance(10, MILLISECONDS); // so that the next call will trigger refresh
assertThat(cache.getOrLoad("test")).isEqualTo("testLoad");
reloadStarted.await();
ticker.advance(500, MILLISECONDS); // so that the entry expires during the reload
threadAboutToBlockForRefresh.set(Thread.currentThread());
assertThat(cache.getOrLoad("test")).isEqualTo("testReload");
} finally {
refreshExecutor.shutdown();
}
}

public void testSetRemovalListener() {
RemovalListener<Object, Object> testListener = TestingRemovalListeners.nullRemovalListener();
LocalCache<Object, Object> map =
Expand Down Expand Up @@ -584,7 +656,7 @@ public void testRecordReadOnCompute() throws ExecutionException {

// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
List<ReferenceEntry<Object, Object>> reads = new ArrayList<>();
Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
Expand Down Expand Up @@ -2097,7 +2169,7 @@ public void testRecordRead() {

// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
List<ReferenceEntry<Object, Object>> reads = new ArrayList<>();
Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
Expand Down Expand Up @@ -2138,7 +2210,7 @@ public void testRecordReadOnGet() {

// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
List<ReferenceEntry<Object, Object>> reads = new ArrayList<>();
Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
Expand Down Expand Up @@ -2179,7 +2251,7 @@ public void testRecordWrite() {

// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> writes = Lists.newArrayList();
List<ReferenceEntry<Object, Object>> writes = new ArrayList<>();
Iterator<ReferenceEntry<Object, Object>> i = writeOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
Expand Down Expand Up @@ -2725,7 +2797,8 @@ private void testLoadThrows(
* weakKeys and weak/softValues.
*/
private static Iterable<CacheBuilder<Object, Object>> allEntryTypeMakers() {
List<CacheBuilder<Object, Object>> result = newArrayList(allKeyValueStrengthMakers());
List<CacheBuilder<Object, Object>> result = new ArrayList<>();
Iterables.addAll(result, allKeyValueStrengthMakers());
for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
result.add(builder.maximumSize(SMALL_MAX_SIZE));
}
Expand Down

0 comments on commit 361fe5d

Please sign in to comment.