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