View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.hbase.io.TimeRange;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category({SmallTests.class})
32  public class TestTimeRangeTracker {
33  
34    @Test
35    public void testRangeConstruction() throws IOException {
36      TimeRange defaultRange = new TimeRange();
37      assertEquals(0L, defaultRange.getMin());
38      assertEquals(Long.MAX_VALUE, defaultRange.getMax());
39      assertTrue(defaultRange.isAllTime());
40  
41      TimeRange oneArgRange = new TimeRange(0L);
42      assertEquals(0L, oneArgRange.getMin());
43      assertEquals(Long.MAX_VALUE, oneArgRange.getMax());
44      assertTrue(oneArgRange.isAllTime());
45  
46      TimeRange oneArgRange2 = new TimeRange(1);
47      assertEquals(1, oneArgRange2.getMin());
48      assertEquals(Long.MAX_VALUE, oneArgRange2.getMax());
49      assertFalse(oneArgRange2.isAllTime());
50  
51      TimeRange twoArgRange = new TimeRange(0L, Long.MAX_VALUE);
52      assertEquals(0L, twoArgRange.getMin());
53      assertEquals(Long.MAX_VALUE, twoArgRange.getMax());
54      assertTrue(twoArgRange.isAllTime());
55  
56      TimeRange twoArgRange2 = new TimeRange(0L, Long.MAX_VALUE - 1);
57      assertEquals(0L, twoArgRange2.getMin());
58      assertEquals(Long.MAX_VALUE - 1, twoArgRange2.getMax());
59      assertFalse(twoArgRange2.isAllTime());
60  
61      TimeRange twoArgRange3 = new TimeRange(1, Long.MAX_VALUE);
62      assertEquals(1, twoArgRange3.getMin());
63      assertEquals(Long.MAX_VALUE, twoArgRange3.getMax());
64      assertFalse(twoArgRange3.isAllTime());
65    }
66  
67    /**
68     * Bit of code to test concurrent access on this class.
69     * @param args
70     * @throws InterruptedException
71     */
72    public static void main(String[] args) throws InterruptedException {
73      long start = System.currentTimeMillis();
74      final TimeRangeTracker trr = new TimeRangeTracker();
75      final int threadCount = 5;
76      final int calls = 1024 * 1024 * 128;
77      Thread [] threads = new Thread[threadCount];
78      for (int i = 0; i < threads.length; i++) {
79        Thread t = new Thread("" + i) {
80          @Override
81          public void run() {
82            for (int i = 0; i < calls; i++) trr.includeTimestamp(i);
83          }
84        };
85        t.start();
86        threads[i] = t;
87      }
88      for (int i = 0; i < threads.length; i++) {
89        threads[i].join();
90      }
91      System.out.println(trr.getMin() + " " + trr.getMax() + " " +
92        (System.currentTimeMillis() - start));
93    }
94  }