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