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