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