001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.DataOutputStream;
025import java.io.IOException;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.io.ByteArrayOutputStream;
028import org.apache.hadoop.hbase.io.TimeRange;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({RegionServerTests.class, SmallTests.class})
036public class TestSimpleTimeRangeTracker {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestSimpleTimeRangeTracker.class);
041
042  protected TimeRangeTracker getTimeRangeTracker() {
043    return TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC);
044  }
045
046  protected TimeRangeTracker getTimeRangeTracker(long min, long max) {
047    return TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC, min, max);
048  }
049
050  @Test
051  public void testExtreme() {
052    TimeRange tr = TimeRange.allTime();
053    assertTrue(tr.includesTimeRange(TimeRange.allTime()));
054    TimeRangeTracker trt = getTimeRangeTracker();
055    assertFalse(trt.includesTimeRange(TimeRange.allTime()));
056    trt.includeTimestamp(1);
057    trt.includeTimestamp(10);
058    assertTrue(trt.includesTimeRange(TimeRange.allTime()));
059  }
060
061  @Test
062  public void testTimeRangeInitialized() {
063    TimeRangeTracker src = getTimeRangeTracker();
064    TimeRange tr = new TimeRange(System.currentTimeMillis());
065    assertFalse(src.includesTimeRange(tr));
066  }
067
068  @Test
069  public void testTimeRangeTrackerNullIsSameAsTimeRangeNull() throws IOException {
070    TimeRangeTracker src = getTimeRangeTracker(1, 2);
071    byte[] bytes = TimeRangeTracker.toByteArray(src);
072    TimeRange tgt = TimeRangeTracker.parseFrom(bytes).toTimeRange();
073    assertEquals(src.getMin(), tgt.getMin());
074    assertEquals(src.getMax(), tgt.getMax());
075  }
076
077  @Test
078  public void testSerialization() throws IOException {
079    TimeRangeTracker src = getTimeRangeTracker(1, 2);
080    TimeRangeTracker tgt = TimeRangeTracker.parseFrom(TimeRangeTracker.toByteArray(src));
081    assertEquals(src.getMin(), tgt.getMin());
082    assertEquals(src.getMax(), tgt.getMax());
083  }
084
085  @Test
086  public void testLegacySerialization() throws IOException {
087    ByteArrayOutputStream data = new ByteArrayOutputStream();
088    DataOutputStream output = new DataOutputStream(data);
089    output.writeLong(100);
090    output.writeLong(200);
091    TimeRangeTracker tgt = TimeRangeTracker.parseFrom(data.toByteArray());
092    assertEquals(100, tgt.getMin());
093    assertEquals(200, tgt.getMax());
094  }
095
096  @Test
097  public void testAlwaysDecrementingSetsMaximum() {
098    TimeRangeTracker trr = getTimeRangeTracker();
099    trr.includeTimestamp(3);
100    trr.includeTimestamp(2);
101    trr.includeTimestamp(1);
102    assertTrue(trr.getMin() != TimeRangeTracker.INITIAL_MIN_TIMESTAMP);
103    assertTrue(trr.getMax() != -1 /*The initial max value*/);
104  }
105
106  @Test
107  public void testSimpleInRange() {
108    TimeRangeTracker trr = getTimeRangeTracker();
109    trr.includeTimestamp(0);
110    trr.includeTimestamp(2);
111    assertTrue(trr.includesTimeRange(new TimeRange(1)));
112  }
113
114  @Test
115  public void testRangeConstruction() throws IOException {
116    TimeRange defaultRange = TimeRange.allTime();
117    assertEquals(0L, defaultRange.getMin());
118    assertEquals(Long.MAX_VALUE, defaultRange.getMax());
119    assertTrue(defaultRange.isAllTime());
120
121    TimeRange oneArgRange = new TimeRange(0L);
122    assertEquals(0L, oneArgRange.getMin());
123    assertEquals(Long.MAX_VALUE, oneArgRange.getMax());
124    assertTrue(oneArgRange.isAllTime());
125
126    TimeRange oneArgRange2 = new TimeRange(1);
127    assertEquals(1, oneArgRange2.getMin());
128    assertEquals(Long.MAX_VALUE, oneArgRange2.getMax());
129    assertFalse(oneArgRange2.isAllTime());
130
131    TimeRange twoArgRange = new TimeRange(0L, Long.MAX_VALUE);
132    assertEquals(0L, twoArgRange.getMin());
133    assertEquals(Long.MAX_VALUE, twoArgRange.getMax());
134    assertTrue(twoArgRange.isAllTime());
135
136    TimeRange twoArgRange2 = new TimeRange(0L, Long.MAX_VALUE - 1);
137    assertEquals(0L, twoArgRange2.getMin());
138    assertEquals(Long.MAX_VALUE - 1, twoArgRange2.getMax());
139    assertFalse(twoArgRange2.isAllTime());
140
141    TimeRange twoArgRange3 = new TimeRange(1, Long.MAX_VALUE);
142    assertEquals(1, twoArgRange3.getMin());
143    assertEquals(Long.MAX_VALUE, twoArgRange3.getMax());
144    assertFalse(twoArgRange3.isAllTime());
145  }
146
147}