fhan688 commented on code in PR #20189:
URL: https://github.com/apache/hudi/pull/20189#discussion_r4218862634


##########
hudi-common/src/test/java/org/apache/hudi/common/util/TestRatelimiter.java:
##########
@@ -33,25 +41,75 @@ public class TestRatelimiter {
   @Test
   public void testRateLimiterWithNoThrottling() throws InterruptedException {
     RateLimiter limiter =  RateLimiter.create(1000, TimeUnit.SECONDS);
-    long start = System.currentTimeMillis();
-    assertEquals(true, limiter.tryAcquire(1000));
-    // Sleep to represent some operation
-    Thread.sleep(500);
-    long end = System.currentTimeMillis();
-    // With a large permit limit, there shouldn't be any throttling of 
operations
-    assertTrue((end - start) < TimeUnit.SECONDS.toMillis(2));
+    try {
+      long start = System.currentTimeMillis();
+      assertEquals(true, limiter.tryAcquire(1000));
+      // Sleep to represent some operation
+      Thread.sleep(500);
+      long end = System.currentTimeMillis();
+      // With a large permit limit, there shouldn't be any throttling of 
operations
+      assertTrue((end - start) < TimeUnit.SECONDS.toMillis(2));
+    } finally {
+      limiter.stop();
+    }
   }
 
   @Test
   public void testRateLimiterWithThrottling() throws InterruptedException {
     RateLimiter limiter =  RateLimiter.create(100, TimeUnit.SECONDS);
-    long start = System.currentTimeMillis();
-    assertEquals(true, limiter.tryAcquire(400));
-    // Sleep to represent some operation
-    Thread.sleep(500);
-    long end = System.currentTimeMillis();
-    // As size of operations is more than the maximum permits per second,
-    // whole execution should be greater than 1 second
-    assertTrue((end - start) >= TimeUnit.SECONDS.toMillis(2));
+    try {
+      long start = System.currentTimeMillis();
+      assertEquals(true, limiter.tryAcquire(400));
+      // Sleep to represent some operation
+      Thread.sleep(500);
+      long end = System.currentTimeMillis();
+      // As size of operations is more than the maximum permits per second,
+      // whole execution should be greater than 1 second
+      assertTrue((end - start) >= TimeUnit.SECONDS.toMillis(2));
+    } finally {
+      limiter.stop();
+    }
+  }
+
+  @Test
+  public void testCustomReleasePeriod() {
+    RateLimiter limiter = RateLimiter.create(1, 100, TimeUnit.MILLISECONDS);
+    try {
+      assertTrue(limiter.acquire(1));
+      long start = System.nanoTime();
+      assertTimeoutPreemptively(Duration.ofSeconds(1), () -> 
assertTrue(limiter.acquire(1)));
+      assertTrue(System.nanoTime() - start >= 
TimeUnit.MILLISECONDS.toNanos(50));

Review Comment:
   Good catch. The refill schedule starts when the limiter is created, so the 
elapsed time measured after the first acquisition depends on the scheduler 
phase and can make the test flaky.
   
   I will replace the wall-clock assertion with a controllable scheduler, 
verify the scheduled period directly, and trigger the captured refill task 
manually.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to