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.compactions;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.Date;
023import java.util.List;
024import java.util.TimeZone;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.RegionServerTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
034
035@Category({ RegionServerTests.class, SmallTests.class })
036public class TestCurrentHourProvider {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestCurrentHourProvider.class);
041
042  private static final List<String> ZONE_IDS = Lists.newArrayList("UTC", "US/Pacific", "Etc/GMT+8");
043
044  /**
045   * In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000 and the unix time
046   * of 2020-08-20 15:04:00 is 1597907081000, by calculating the delta time to get expected time in
047   * current timezone, then we can get special hour no matter which timezone it runs.
048   * <p/>
049   * In addition, we should consider the Daylight Saving Time. If in DaylightTime, we need reduce
050   * one hour.
051   */
052  @Test
053  public void testWithEnvironmentEdge() {
054    // test for all available zoneID
055    for (String zoneID : ZONE_IDS) {
056      TimeZone timezone = TimeZone.getTimeZone(zoneID);
057      TimeZone.setDefault(timezone);
058
059      // set a time represent hour 11
060      long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
061      long timeFor11 = 1597895561000L - deltaFor11;
062      EnvironmentEdgeManager.injectEdge(() -> timeFor11);
063      CurrentHourProvider.advanceTick();
064      int hour11 = CurrentHourProvider.getCurrentHour();
065      if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
066        hour11 = CurrentHourProvider.getCurrentHour() - 1;
067      }
068      assertEquals(11, hour11);
069
070      // set a time represent hour 15
071      long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
072      long timeFor15 = 1597907081000L - deltaFor15;
073      EnvironmentEdgeManager.injectEdge(() -> timeFor15);
074      CurrentHourProvider.advanceTick();
075      int hour15 = CurrentHourProvider.getCurrentHour();
076      if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
077        hour15 = CurrentHourProvider.getCurrentHour() - 1;
078      }
079      assertEquals(15, hour15);
080    }
081  }
082}