• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.telemetry;
6 
7 import static org.junit.Assert.assertFalse;
8 import static org.junit.Assert.assertThrows;
9 import static org.junit.Assert.assertTrue;
10 
11 import androidx.test.ext.junit.runners.AndroidJUnit4;
12 
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 
16 import org.chromium.base.test.util.Batch;
17 
18 @RunWith(AndroidJUnit4.class)
19 @Batch(Batch.UNIT_TESTS)
20 public final class RateLimiterTest {
21     @Test
testImmediateRateLimit()22     public void testImmediateRateLimit() {
23         RateLimiter rateLimiter = new RateLimiter(1);
24         assertTrue("First request was rate limited", rateLimiter.tryAcquire());
25         assertFalse("Second request was not rate limited", rateLimiter.tryAcquire());
26     }
27 
28     // TODO(b/309098875): the internal repo these tests were originally written in had the following
29     // tests:
30     // - testOneSamplePerSecond
31     // - testNoSampleSentPerSecond
32     // - testMultipleSamplesPerSecond
33     // Problem is these tests used the Roboelectric ShadowSystemClock, which we can't easily use
34     // here. We should find a way to work around that. In the mean time, these tests are omitted.
35 
36     @Test
testInvalidSamplePerSecond()37     public void testInvalidSamplePerSecond() {
38         int samplesPerSecond = -1;
39         assertThrows(
40                 "samples per second was negative",
41                 IllegalArgumentException.class,
42                 () -> new RateLimiter(samplesPerSecond));
43     }
44 }
45