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.SmallTests;
23 import org.junit.Test;
24 import org.junit.experimental.categories.Category;
25
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 import static org.junit.Assert.assertTrue;
30
31 @Category(SmallTests.class)
32 public class TestConnectionUtils {
33
34 @Test
35 public void testRetryTimeJitter() {
36 long[] retries = new long[200];
37 long baseTime = 1000000; //Larger number than reality to help test randomness.
38 long maxTimeExpected = (long) (baseTime * 1.01f);
39 for (int i = 0; i < retries.length; i++) {
40 retries[i] = ConnectionUtils.getPauseTime(baseTime, 0);
41 }
42
43 Set<Long> retyTimeSet = new TreeSet<Long>();
44 for (long l : retries) {
45 /*make sure that there is some jitter but only 1%*/
46 assertTrue(l >= baseTime);
47 assertTrue(l <= maxTimeExpected);
48 // Add the long to the set
49 retyTimeSet.add(l);
50 }
51
52 //Make sure that most are unique. some overlap will happen
53 assertTrue(retyTimeSet.size() > (retries.length * 0.80));
54 }
55
56 }