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