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.compactions;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import com.google.common.collect.Lists;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.TimeZone;
26  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.EnvironmentEdge;
29  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category({RegionServerTests.class, SmallTests.class})
34  public class TestCurrentHourProvider {
35  
36    private static final List<String> ZONE_IDS = Lists.newArrayList("UTC", "US/Pacific", "Etc/GMT+8");
37  
38    /**
39     * In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000 and the unix time
40     * of 2020-08-20 15:04:00 is 1597907081000, by calculating the delta time to get expected time in
41     * current timezone, then we can get special hour no matter which timezone it runs.
42     * <p/>
43     * In addition, we should consider the Daylight Saving Time. If in DaylightTime, we need reduce
44     * one hour.
45     */
46    @Test
47    public void testWithEnvironmentEdge() {
48      // test for all available zoneID
49      for (String zoneID : ZONE_IDS) {
50        TimeZone timezone = TimeZone.getTimeZone(zoneID);
51        TimeZone.setDefault(timezone);
52  
53        // set a time represent hour 11
54        long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
55        final long timeFor11 = 1597895561000L - deltaFor11;
56        EnvironmentEdgeManager.injectEdge(new EnvironmentEdge() {
57          @Override
58          public long currentTime() {
59            return timeFor11;
60          }
61        });
62        CurrentHourProvider.advanceTick();
63        int hour11 = CurrentHourProvider.getCurrentHour();
64        if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
65          hour11 = CurrentHourProvider.getCurrentHour() - 1;
66        }
67        assertEquals(11, hour11);
68  
69        // set a time represent hour 15
70        long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
71        final long timeFor15 = 1597907081000L - deltaFor15;
72        EnvironmentEdgeManager.injectEdge(new EnvironmentEdge() {
73          @Override
74          public long currentTime() {
75            return timeFor15;
76          }
77        });
78        CurrentHourProvider.advanceTick();
79        int hour15 = CurrentHourProvider.getCurrentHour();
80        if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
81          hour15 = CurrentHourProvider.getCurrentHour() - 1;
82        }
83        assertEquals(15, hour15);
84      }
85    }
86  }