Skip to content

Commit 64322c3

Browse files
authored
11243: RLS cleanups (#12085)
1 parent af7efeb commit 64322c3

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import io.grpc.internal.FakeClock;
2626
import io.grpc.rls.LruCache.EvictionListener;
2727
import io.grpc.rls.LruCache.EvictionType;
28+
import java.util.Arrays;
2829
import java.util.Objects;
2930
import java.util.concurrent.TimeUnit;
31+
import javax.annotation.Nullable;
3032
import org.junit.Before;
3133
import org.junit.Rule;
3234
import org.junit.Test;
@@ -266,4 +268,91 @@ public int hashCode() {
266268
return Objects.hash(value, expireTime);
267269
}
268270
}
271+
272+
@Test
273+
public void testFitToLimitWithReSize() {
274+
275+
Entry entry1 = new Entry("Entry1", ticker.read() + 10, 4);
276+
Entry entry2 = new Entry("Entry2", ticker.read() + 20, 1);
277+
Entry entry3 = new Entry("Entry3", ticker.read() + 30, 2);
278+
279+
cache.cache(1, entry1);
280+
cache.cache(2, entry2);
281+
cache.cache(3, entry3);
282+
283+
assertThat(cache.estimatedSize()).isEqualTo(2);
284+
assertThat(cache.estimatedSizeBytes()).isEqualTo(3);
285+
assertThat(cache.estimatedMaxSizeBytes()).isEqualTo(5);
286+
287+
cache.resize(2);
288+
assertThat(cache.estimatedSize()).isEqualTo(1);
289+
assertThat(cache.estimatedSizeBytes()).isEqualTo(2);
290+
assertThat(cache.estimatedMaxSizeBytes()).isEqualTo(2);
291+
292+
assertThat(cache.fitToLimit()).isEqualTo(false);
293+
}
294+
295+
@Test
296+
public void testFitToLimit() {
297+
298+
TestFitToLimitEviction localCache = new TestFitToLimitEviction(
299+
MAX_SIZE,
300+
evictionListener,
301+
fakeClock.getTicker()
302+
);
303+
304+
Entry entry1 = new Entry("Entry1", ticker.read() + 10, 4);
305+
Entry entry2 = new Entry("Entry2", ticker.read() + 20, 2);
306+
Entry entry3 = new Entry("Entry3", ticker.read() + 30, 1);
307+
308+
localCache.cache(1, entry1);
309+
localCache.cache(2, entry2);
310+
localCache.cache(3, entry3);
311+
312+
assertThat(localCache.estimatedSize()).isEqualTo(3);
313+
assertThat(localCache.estimatedSizeBytes()).isEqualTo(7);
314+
assertThat(localCache.estimatedMaxSizeBytes()).isEqualTo(5);
315+
316+
localCache.enableEviction();
317+
318+
assertThat(localCache.fitToLimit()).isEqualTo(true);
319+
320+
assertThat(localCache.values().contains(entry1)).isFalse();
321+
assertThat(localCache.values().containsAll(Arrays.asList(entry2, entry3))).isTrue();
322+
323+
assertThat(localCache.estimatedSize()).isEqualTo(2);
324+
assertThat(localCache.estimatedSizeBytes()).isEqualTo(3);
325+
assertThat(localCache.estimatedMaxSizeBytes()).isEqualTo(5);
326+
}
327+
328+
private static class TestFitToLimitEviction extends LinkedHashLruCache<Integer, Entry> {
329+
330+
private boolean allowEviction = false;
331+
332+
TestFitToLimitEviction(
333+
long estimatedMaxSizeBytes,
334+
@Nullable EvictionListener<Integer, Entry> evictionListener,
335+
Ticker ticker) {
336+
super(estimatedMaxSizeBytes, evictionListener, ticker);
337+
}
338+
339+
@Override
340+
protected boolean isExpired(Integer key, Entry value, long nowNanos) {
341+
return value.expireTime - nowNanos <= 0;
342+
}
343+
344+
@Override
345+
protected int estimateSizeOf(Integer key, Entry value) {
346+
return value.size;
347+
}
348+
349+
@Override
350+
protected boolean shouldInvalidateEldestEntry(Integer eldestKey, Entry eldestValue, long now) {
351+
return allowEviction && super.shouldInvalidateEldestEntry(eldestKey, eldestValue, now);
352+
}
353+
354+
public void enableEviction() {
355+
allowEviction = true;
356+
}
357+
}
269358
}

0 commit comments

Comments
 (0)