summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Shai Barack <shayba@google.com> 2025-02-11 14:39:09 -0800
committer Android (Google) Code Review <android-gerrit@google.com> 2025-02-11 14:39:09 -0800
commite50accd4ddeb8353f6ffdf327e5188778aa1ac35 (patch)
treefbe52d9fb27c391a9f94f3f91838fac0c926ec75
parent19019846f82f568a10d122d11c45e832fb781ecf (diff)
parent5f19df700cc32f8e81889f94346d26291bb8ce99 (diff)
Merge "Reland "Remove sleeps from RateLimitingCacheTest"" into main
-rw-r--r--core/tests/coretests/src/com/android/internal/util/RateLimitingCacheTest.java63
1 files changed, 34 insertions, 29 deletions
diff --git a/core/tests/coretests/src/com/android/internal/util/RateLimitingCacheTest.java b/core/tests/coretests/src/com/android/internal/util/RateLimitingCacheTest.java
index 2edab6268bec..19f7747fc8d1 100644
--- a/core/tests/coretests/src/com/android/internal/util/RateLimitingCacheTest.java
+++ b/core/tests/coretests/src/com/android/internal/util/RateLimitingCacheTest.java
@@ -22,8 +22,6 @@ import static org.junit.Assert.fail;
import android.os.SystemClock;
-import android.os.SystemClock;
-
import androidx.test.runner.AndroidJUnit4;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -56,13 +54,13 @@ public class RateLimitingCacheTest {
*/
@Test
public void testTtl_Zero() {
- RateLimitingCache<Integer> s = new RateLimitingCache<>(0);
+ TestRateLimitingCache<Integer> s = new TestRateLimitingCache<>(0);
int first = s.get(mFetcher);
assertEquals(first, 0);
int second = s.get(mFetcher);
assertEquals(second, 1);
- SystemClock.sleep(20);
+ s.advanceTime(20);
int third = s.get(mFetcher);
assertEquals(third, 2);
}
@@ -73,14 +71,14 @@ public class RateLimitingCacheTest {
*/
@Test
public void testTtl_100() {
- RateLimitingCache<Integer> s = new RateLimitingCache<>(100);
+ TestRateLimitingCache<Integer> s = new TestRateLimitingCache<>(100);
int first = s.get(mFetcher);
assertEquals(first, 0);
int second = s.get(mFetcher);
// Too early to change
assertEquals(second, 0);
- SystemClock.sleep(150);
+ s.advanceTime(150);
int third = s.get(mFetcher);
// Changed by now
assertEquals(third, 1);
@@ -95,11 +93,11 @@ public class RateLimitingCacheTest {
*/
@Test
public void testTtl_Negative() {
- RateLimitingCache<Integer> s = new RateLimitingCache<>(-1);
+ TestRateLimitingCache<Integer> s = new TestRateLimitingCache<>(-1);
int first = s.get(mFetcher);
assertEquals(first, 0);
- SystemClock.sleep(200);
+ s.advanceTime(200);
// Should return the original value every time
int second = s.get(mFetcher);
assertEquals(second, 0);
@@ -111,7 +109,7 @@ public class RateLimitingCacheTest {
*/
@Test
public void testTtl_Spam() {
- RateLimitingCache<Integer> s = new RateLimitingCache<>(100);
+ TestRateLimitingCache<Integer> s = new TestRateLimitingCache<>(100);
assertCount(s, 1000, 7, 15);
}
@@ -121,28 +119,13 @@ public class RateLimitingCacheTest {
*/
@Test
public void testRate_10hz() {
- RateLimitingCache<Integer> s = new RateLimitingCache<>(1000, 10);
+ TestRateLimitingCache<Integer> s = new TestRateLimitingCache<>(1000, 10);
// At 10 per second, 2 seconds should not exceed about 30, assuming overlap into left and
// right windows that allow 10 each
assertCount(s, 2000, 20, 33);
}
/**
- * Test that using a different timebase works correctly.
- */
- @Test
- public void testTimebase() {
- RateLimitingCache<Integer> s = new RateLimitingCache<>(1000, 10) {
- @Override
- protected long getTime() {
- return SystemClock.elapsedRealtime() / 2;
- }
- };
- // Timebase is moving at half the speed, so only allows for 1 second worth in 2 seconds.
- assertCount(s, 2000, 10, 22);
- }
-
- /**
* Exercises concurrent access to the cache.
*/
@Test
@@ -287,15 +270,37 @@ public class RateLimitingCacheTest {
* @param minCount the lower end of the expected number of fetches, with a margin for error
* @param maxCount the higher end of the expected number of fetches, with a margin for error
*/
- private void assertCount(RateLimitingCache<Integer> cache, long period,
+ private void assertCount(TestRateLimitingCache<Integer> cache, long period,
int minCount, int maxCount) {
- long startTime = SystemClock.elapsedRealtime();
- while (SystemClock.elapsedRealtime() < startTime + period) {
+ long startTime = cache.getTime();
+ while (cache.getTime() < startTime + period) {
int value = cache.get(mFetcher);
- SystemClock.sleep(5);
+ cache.advanceTime(5);
}
int latest = cache.get(mFetcher);
assertTrue("Latest should be between " + minCount + " and " + maxCount
+ " but is " + latest, latest <= maxCount && latest >= minCount);
}
+
+ private static class TestRateLimitingCache<Value> extends RateLimitingCache<Value> {
+ // Start at a non-zero time to avoid confusion with uninitialized state.
+ private long mTime = 1;
+
+ public TestRateLimitingCache(long periodMillis) {
+ super(periodMillis);
+ }
+
+ public TestRateLimitingCache(long periodMillis, int count) {
+ super(periodMillis, count);
+ }
+
+ public void advanceTime(long time) {
+ mTime += time;
+ }
+
+ @Override
+ public long getTime() {
+ return mTime;
+ }
+ }
}