View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.quotas;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertTrue;
17  
18  import java.util.concurrent.TimeUnit;
19  
20  import org.apache.hadoop.hbase.testclassification.SmallTests;
21  import org.junit.Test;
22  import org.junit.experimental.categories.Category;
23  
24  /**
25   * Verify the behaviour of the Rate Limiter.
26   */
27  @Category({ SmallTests.class })
28  public class TestRateLimiter {
29    @Test
30    public void testWaitIntervalTimeUnitSeconds() {
31      testWaitInterval(TimeUnit.SECONDS, 10, 100);
32    }
33  
34    @Test
35    public void testWaitIntervalTimeUnitMinutes() {
36      testWaitInterval(TimeUnit.MINUTES, 10, 6000);
37    }
38  
39    @Test
40    public void testWaitIntervalTimeUnitHours() {
41      testWaitInterval(TimeUnit.HOURS, 10, 360000);
42    }
43  
44    @Test
45    public void testWaitIntervalTimeUnitDays() {
46      testWaitInterval(TimeUnit.DAYS, 10, 8640000);
47    }
48  
49    private void testWaitInterval(final TimeUnit timeUnit, final long limit,
50        final long expectedWaitInterval) {
51      RateLimiter limiter = new RateLimiter();
52      limiter.set(limit, timeUnit);
53  
54      long nowTs = 0;
55      long lastTs = 0;
56  
57      // consume all the available resources, one request at the time.
58      // the wait interval should be 0
59      for (int i = 0; i < (limit - 1); ++i) {
60        assertTrue(limiter.canExecute(nowTs, lastTs));
61        limiter.consume();
62        long waitInterval = limiter.waitInterval();
63        assertEquals(0, waitInterval);
64      }
65  
66      for (int i = 0; i < (limit * 4); ++i) {
67        // There is one resource available, so we should be able to
68        // consume it without waiting.
69        assertTrue(limiter.canExecute(nowTs, lastTs));
70        assertEquals(0, limiter.waitInterval());
71        limiter.consume();
72        lastTs = nowTs;
73  
74        // No more resources are available, we should wait for at least an interval.
75        long waitInterval = limiter.waitInterval();
76        assertEquals(expectedWaitInterval, waitInterval);
77  
78        // set the nowTs to be the exact time when resources should be available again.
79        nowTs += waitInterval;
80  
81        // artificially go into the past to prove that when too early we should fail.
82        assertFalse(limiter.canExecute(nowTs - 500, lastTs));
83      }
84    }
85  
86    @Test
87    public void testOverconsumption() {
88      RateLimiter limiter = new RateLimiter();
89      limiter.set(10, TimeUnit.SECONDS);
90  
91      // 10 resources are available, but we need to consume 20 resources
92      // Verify that we have to wait at least 1.1sec to have 1 resource available
93      assertTrue(limiter.canExecute(0, 0));
94      limiter.consume(20);
95      assertEquals(1100, limiter.waitInterval());
96  
97      // Verify that after 1sec we need to wait for another 0.1sec to get a resource available
98      assertFalse(limiter.canExecute(1000, 0));
99      assertEquals(100, limiter.waitInterval());
100 
101     // Verify that after 1.1sec the resource is available
102     assertTrue(limiter.canExecute(1100, 0));
103     assertEquals(0, limiter.waitInterval());
104   }
105 }