Skip to content

Commit 9242a3d

Browse files
Apache9petersomogyi
authored andcommitted
HBASE-25629 Reimplement TestCurrentHourProvider to not depend on unstable TZs (#3013)
Signed-off-by: XinSun <[email protected]>
1 parent 36a0f01 commit 9242a3d

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CurrentHourProvider.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
*/
1818
package org.apache.hadoop.hbase.regionserver.compactions;
1919

20+
import com.google.errorprone.annotations.RestrictedApi;
2021
import java.util.Calendar;
2122
import java.util.GregorianCalendar;
22-
2323
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
2424
import org.apache.yetus.audience.InterfaceAudience;
25-
import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
2625

2726
@InterfaceAudience.Private
2827
public class CurrentHourProvider {
29-
private CurrentHourProvider() { throw new AssertionError(); }
28+
29+
private CurrentHourProvider() {
30+
throw new AssertionError();
31+
}
3032

3133
private static final class Tick {
3234
final int currentHour;
@@ -38,8 +40,7 @@ private static final class Tick {
3840
}
3941
}
4042

41-
@VisibleForTesting
42-
static Tick nextTick() {
43+
private static Tick nextTick() {
4344
Calendar calendar = new GregorianCalendar();
4445
calendar.setTimeInMillis(EnvironmentEdgeManager.currentTime());
4546
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
@@ -54,16 +55,21 @@ private static void moveToNextHour(Calendar calendar) {
5455
calendar.set(Calendar.MILLISECOND, 0);
5556
}
5657

57-
@VisibleForTesting
58-
static volatile Tick tick = nextTick();
58+
private static volatile Tick tick = nextTick();
5959

6060
public static int getCurrentHour() {
6161
Tick tick = CurrentHourProvider.tick;
6262
if (EnvironmentEdgeManager.currentTime() < tick.expirationTimeInMillis) {
6363
return tick.currentHour;
6464
}
65-
66-
CurrentHourProvider.tick = tick = nextTick();
65+
tick = nextTick();
66+
CurrentHourProvider.tick = tick;
6767
return tick.currentHour;
6868
}
69+
70+
@RestrictedApi(explanation = "Should only be called in tests", link = "",
71+
allowedOnPath = ".*/src/test/.*")
72+
static void advanceTick() {
73+
tick = nextTick();
74+
}
6975
}

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestCurrentHourProvider.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertEquals;
2121

2222
import java.util.Date;
23+
import java.util.List;
2324
import java.util.TimeZone;
2425
import org.apache.hadoop.hbase.HBaseClassTestRule;
2526
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
@@ -29,52 +30,51 @@
2930
import org.junit.Test;
3031
import org.junit.experimental.categories.Category;
3132

32-
@Category({RegionServerTests.class, SmallTests.class})
33+
import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
34+
35+
@Category({ RegionServerTests.class, SmallTests.class })
3336
public class TestCurrentHourProvider {
37+
3438
@ClassRule
3539
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");
3743

3844
/**
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.
4751
*/
4852
@Test
4953
public void testWithEnvironmentEdge() {
5054
// test for all available zoneID
51-
for (String zoneID : TimeZone.getAvailableIDs()) {
55+
for (String zoneID : ZONE_IDS) {
5256
TimeZone timezone = TimeZone.getTimeZone(zoneID);
5357
TimeZone.setDefault(timezone);
5458

5559
// set a time represent hour 11
5660
long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
5761
long timeFor11 = 1597895561000L - deltaFor11;
5862
EnvironmentEdgeManager.injectEdge(() -> timeFor11);
59-
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
63+
CurrentHourProvider.advanceTick();
6064
int hour11 = CurrentHourProvider.getCurrentHour();
6165
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;
6567
}
6668
assertEquals(11, hour11);
6769

6870
// set a time represent hour 15
6971
long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
7072
long timeFor15 = 1597907081000L - deltaFor15;
7173
EnvironmentEdgeManager.injectEdge(() -> timeFor15);
72-
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
74+
CurrentHourProvider.advanceTick();
7375
int hour15 = CurrentHourProvider.getCurrentHour();
7476
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;
7878
}
7979
assertEquals(15, hour15);
8080
}

0 commit comments

Comments
 (0)