Skip to content

Commit 63eeed8

Browse files
authored
Fix random timezone id selection to be consistent with DateUtilsTests.testTimezoneIds (#105989)
1 parent 8e05395 commit 63eeed8

File tree

4 files changed

+42
-57
lines changed

4 files changed

+42
-57
lines changed

server/src/test/java/org/elasticsearch/common/time/DateUtilsTests.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
import java.time.ZoneOffset;
2222
import java.time.ZonedDateTime;
2323
import java.time.temporal.ChronoField;
24-
import java.util.Arrays;
25-
import java.util.HashSet;
26-
import java.util.Set;
2724

2825
import static org.elasticsearch.common.time.DateUtils.clampToNanosRange;
2926
import static org.elasticsearch.common.time.DateUtils.toInstant;
@@ -35,42 +32,15 @@
3532
import static org.hamcrest.Matchers.is;
3633

3734
public class DateUtilsTests extends ESTestCase {
38-
// list of ignored timezones.
39-
// These should be cleaned up when all tested jdks (oracle, adoptopenjdk, openjdk etc) have the timezone db included
40-
// see when a timezone was included in jdk version here https://www.oracle.com/java/technologies/tzdata-versions.html
41-
private static final Set<String> IGNORE = new HashSet<>(
42-
Arrays.asList(
43-
"Eire",
44-
"Europe/Dublin", // dublin timezone in joda does not account for DST
45-
"Asia/Qostanay", // part of tzdata2018h
46-
"America/Godthab", // part of tzdata2020a (maps to America/Nuuk)
47-
"America/Nuuk", // part of tzdata2020a
48-
"America/Ciudad_Juarez", // part of tzdata2022g
49-
"America/Pangnirtung", // part of tzdata2022g
50-
"Europe/Kyiv", // part of tzdata2022c,
51-
"Pacific/Kanton" // part of tzdata2021b
52-
)
53-
);
54-
55-
// A temporary list of zones and JDKs, where Joda and the JDK timezone data are out of sync, until either Joda or the JDK
56-
// are updated, see https://github.com/elastic/elasticsearch/issues/82356
57-
private static final Set<String> IGNORE_IDS = new HashSet<>(Arrays.asList("Pacific/Niue", "America/Pangnirtung", "Antarctica/Vostok"));
58-
59-
private static boolean maybeIgnore(String jodaId) {
60-
if (IGNORE_IDS.contains(jodaId)) {
61-
return true;
62-
}
63-
return false;
64-
}
6535

6636
public void testTimezoneIds() {
6737
assertNull(DateUtils.dateTimeZoneToZoneId(null));
6838
assertNull(DateUtils.zoneIdToDateTimeZone(null));
6939
for (String jodaId : DateTimeZone.getAvailableIDs()) {
70-
if (IGNORE.contains(jodaId) || maybeIgnore(jodaId)) continue;
40+
if (IGNORED_TIMEZONE_IDS.contains(jodaId)) continue;
7141
DateTimeZone jodaTz = DateTimeZone.forID(jodaId);
7242
// some timezones get mapped back to problematic timezones
73-
if (IGNORE.contains(jodaTz.toString())) continue;
43+
if (IGNORED_TIMEZONE_IDS.contains(jodaTz.toString())) continue;
7444
ZoneId zoneId = DateUtils.dateTimeZoneToZoneId(jodaTz); // does not throw
7545
long now = 0;
7646
assertThat(

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,30 @@
192192
@LuceneTestCase.SuppressReproduceLine
193193
public abstract class ESTestCase extends LuceneTestCase {
194194

195+
// A temporary list of ignored timezones.
196+
//
197+
// These should be cleaned up when all tested jdks (oracle, adoptopenjdk, openjdk etc) have the timezone db included
198+
// see when a timezone was included in jdk version here https://www.oracle.com/java/technologies/tzdata-versions.html
199+
// In some cases Joda and the JDK timezone data might also be out of sync,
200+
// see https://github.com/elastic/elasticsearch/issues/82356
201+
protected static final Set<String> IGNORED_TIMEZONE_IDS = Collections.unmodifiableSet(
202+
new HashSet<>(
203+
Arrays.asList(
204+
"Eire",
205+
"Europe/Dublin", // dublin timezone in joda does not account for DST
206+
"Asia/Qostanay", // part of tzdata2018h
207+
"America/Godthab", // part of tzdata2020a (maps to America/Nuuk)
208+
"America/Nuuk", // part of tzdata2020a
209+
"America/Ciudad_Juarez", // part of tzdata2022g
210+
"America/Pangnirtung", // part of tzdata2022g
211+
"Europe/Kyiv", // part of tzdata2022c,
212+
"Pacific/Kanton", // part of tzdata2021b
213+
"Pacific/Niue",
214+
"Antarctica/Vostok"
215+
)
216+
)
217+
);
218+
195219
protected static final List<String> JODA_TIMEZONE_IDS;
196220
protected static final List<String> JAVA_TIMEZONE_IDS;
197221
protected static final List<String> JAVA_ZONE_IDS;
@@ -1058,8 +1082,17 @@ public static ZoneId randomZone() {
10581082
* still need to do internally e.g. in bwc serialization and in the extract() method
10591083
* //TODO remove once joda is not supported
10601084
*/
1061-
private static String randomJodaAndJavaSupportedTimezone(List<String> zoneIds) {
1062-
return randomValueOtherThanMany(id -> JODA_TIMEZONE_IDS.contains(id) == false, () -> randomFrom(zoneIds));
1085+
private static boolean rejectTimezone(String zoneId) {
1086+
// reject if unknown to joda
1087+
return JODA_TIMEZONE_IDS.contains(zoneId) == false
1088+
// reject ignored ids
1089+
|| IGNORED_TIMEZONE_IDS.contains(zoneId)
1090+
// some timezones get mapped back to problematic timezones
1091+
|| IGNORED_TIMEZONE_IDS.contains(DateTimeZone.forID(zoneId).toString());
1092+
}
1093+
1094+
protected static String randomJodaAndJavaSupportedTimezone(List<String> javaZoneIds) {
1095+
return randomValueOtherThanMany(ESTestCase::rejectTimezone, () -> randomFrom(javaZoneIds));
10631096
}
10641097

10651098
/**
@@ -1099,13 +1132,13 @@ public static <T> T randomValueOtherThan(T input, Supplier<T> randomSupplier) {
10991132
}
11001133

11011134
/**
1102-
* helper to get a random value in a certain range that's different from the input
1135+
* Helper to repeatedly get a random value until it doesn't match the reject predicate.
11031136
*/
1104-
public static <T> T randomValueOtherThanMany(Predicate<T> input, Supplier<T> randomSupplier) {
1137+
public static <T> T randomValueOtherThanMany(Predicate<T> reject, Supplier<T> randomSupplier) {
11051138
T randomValue = null;
11061139
do {
11071140
randomValue = randomSupplier.get();
1108-
} while (input.test(randomValue));
1141+
} while (reject.test(randomValue));
11091142
return randomValue;
11101143
}
11111144

x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcIntegrationTestCase.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
import java.sql.Connection;
2323
import java.sql.DriverManager;
2424
import java.sql.SQLException;
25-
import java.util.ArrayList;
26-
import java.util.Collections;
27-
import java.util.HashSet;
28-
import java.util.List;
2925
import java.util.Map;
3026
import java.util.Properties;
31-
import java.util.Set;
3227

3328
public abstract class JdbcIntegrationTestCase extends ESRestTestCase {
3429

@@ -142,11 +137,7 @@ public static String randomKnownTimeZone() {
142137
// from all available JDK timezones. While Joda and JDK are generally in sync, some timezones might not be known
143138
// to the current version of Joda and in this case the test might fail. To avoid that, we specify a timezone
144139
// known for both Joda and JDK
145-
Set<String> timeZones = new HashSet<>(JODA_TIMEZONE_IDS);
146-
timeZones.retainAll(JAVA_TIMEZONE_IDS);
147-
List<String> ids = new ArrayList<>(timeZones);
148-
Collections.sort(ids);
149-
return randomFrom(ids);
140+
return randomJodaAndJavaSupportedTimezone(JAVA_TIMEZONE_IDS);
150141
}
151142

152143
private static Map<String, Object> searchStats() throws IOException {

x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcIntegrationTestCase.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@
2121
import java.sql.Connection;
2222
import java.sql.DriverManager;
2323
import java.sql.SQLException;
24-
import java.util.ArrayList;
25-
import java.util.Collections;
26-
import java.util.HashSet;
27-
import java.util.List;
2824
import java.util.Properties;
29-
import java.util.Set;
3025

3126
import static org.elasticsearch.common.Strings.hasText;
3227
import static org.elasticsearch.xpack.ql.TestUtils.assertNoSearchContexts;
@@ -158,10 +153,6 @@ public static String randomKnownTimeZone() {
158153
// from all available JDK timezones. While Joda and JDK are generally in sync, some timezones might not be known
159154
// to the current version of Joda and in this case the test might fail. To avoid that, we specify a timezone
160155
// known for both Joda and JDK
161-
Set<String> timeZones = new HashSet<>(JODA_TIMEZONE_IDS);
162-
timeZones.retainAll(JAVA_TIMEZONE_IDS);
163-
List<String> ids = new ArrayList<>(timeZones);
164-
Collections.sort(ids);
165-
return randomFrom(ids);
156+
return randomJodaAndJavaSupportedTimezone(JAVA_TIMEZONE_IDS);
166157
}
167158
}

0 commit comments

Comments
 (0)