View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.testclassification.SmallTests;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  import java.util.Set;
28  import java.util.TreeSet;
29  
30  import static org.junit.Assert.assertTrue;
31  
32  @Category(SmallTests.class)
33  public class TestConnectionUtils {
34  
35    @Test
36    public void testRetryTimeJitter() {
37      long[] retries = new long[200];
38      long baseTime = 1000000;  //Larger number than reality to help test randomness.
39      long maxTimeExpected = (long) (baseTime * 1.01f);
40      for (int i = 0; i < retries.length; i++) {
41        retries[i] = ConnectionUtils.getPauseTime(baseTime, 0);
42      }
43  
44      Set<Long> retyTimeSet = new TreeSet<Long>();
45      for (long l : retries) {
46        /*make sure that there is some jitter but only 1%*/
47        assertTrue(l >= baseTime);
48        assertTrue(l <= maxTimeExpected);
49        // Add the long to the set
50        retyTimeSet.add(l);
51      }
52  
53      //Make sure that most are unique.  some overlap will happen
54      assertTrue(retyTimeSet.size() > (retries.length * 0.80));
55    }
56  
57    @Test
58    public void testGetPauseTime() {
59      long pauseTime;
60      long baseTime = 100;
61      pauseTime = ConnectionUtils.getPauseTime(baseTime, -1);
62      assertTrue(pauseTime >= (baseTime * HConstants.RETRY_BACKOFF[0]));
63      assertTrue(pauseTime <= (baseTime * HConstants.RETRY_BACKOFF[0] * 1.01f));
64  
65      for (int i = 0; i < HConstants.RETRY_BACKOFF.length; i++) {
66        pauseTime = ConnectionUtils.getPauseTime(baseTime, i);
67        assertTrue(pauseTime >= (baseTime * HConstants.RETRY_BACKOFF[i]));
68        assertTrue(pauseTime <= (baseTime * HConstants.RETRY_BACKOFF[i] * 1.01f));
69      }
70  
71      int length = HConstants.RETRY_BACKOFF.length;
72      pauseTime = ConnectionUtils.getPauseTime(baseTime, length);
73      assertTrue(pauseTime >= (baseTime * HConstants.RETRY_BACKOFF[length - 1]));
74      assertTrue(pauseTime <= (baseTime * HConstants.RETRY_BACKOFF[length - 1] * 1.01f));
75    }
76  }