Improve random DateTimeZone creation in tests
We often require a random joda DateTimeZone in our tests. Currently there are a few options for generating such a random DateTimeZone from the set of available ids. Currently most random picks are not really reproducable across different jvms because they rely on order in the ids set implementation. The helper in DateProcessorFactoryTests thus performs a sort on the set of ids before random picking from the result, so I moved this to ESTestCase to make it publicly available and changed all other tests to use that method.
This commit is contained in:
parent
4d6887075f
commit
d2515727d0
|
@ -25,7 +25,6 @@ import org.joda.time.DateTime;
|
|||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
@ -234,19 +233,18 @@ public class TimeZoneRoundingTests extends ESTestCase {
|
|||
* amount of milliseconds.
|
||||
*/
|
||||
public void testSubHourNextRoundingEquallySpaced() {
|
||||
String timeZone = randomFrom(new ArrayList<>(DateTimeZone.getAvailableIDs()));
|
||||
DateTimeUnit unit = randomFrom(new DateTimeUnit[] { DateTimeUnit.HOUR_OF_DAY, DateTimeUnit.MINUTES_OF_HOUR,
|
||||
DateTimeUnit.SECOND_OF_MINUTE });
|
||||
DateTimeZone tz = DateTimeZone.forID(timeZone);
|
||||
TimeZoneRounding rounding = new TimeZoneRounding.TimeUnitRounding(unit, tz);
|
||||
DateTimeZone timezone = randomDateTimeZone();
|
||||
TimeZoneRounding rounding = new TimeZoneRounding.TimeUnitRounding(unit, timezone);
|
||||
// move the random date to transition for timezones that have offset change due to dst transition
|
||||
long nextTransition = tz.nextTransition(Math.abs(randomLong() % ((long) 10e11)));
|
||||
long nextTransition = timezone.nextTransition(Math.abs(randomLong() % ((long) 10e11)));
|
||||
final long millisPerUnit = unit.field().getDurationField().getUnitMillis();
|
||||
// start ten units before transition
|
||||
long roundedDate = rounding.round(nextTransition - (10 * millisPerUnit));
|
||||
while (roundedDate < nextTransition + 10 * millisPerUnit) {
|
||||
long delta = rounding.nextRoundingValue(roundedDate) - roundedDate;
|
||||
assertEquals("Difference between rounded values not equally spaced for [" + unit.name() + "], [" + timeZone + "] at "
|
||||
assertEquals("Difference between rounded values not equally spaced for [" + unit.name() + "], [" + timezone + "] at "
|
||||
+ new DateTime(roundedDate), millisPerUnit, delta);
|
||||
roundedDate = rounding.nextRoundingValue(roundedDate);
|
||||
}
|
||||
|
|
|
@ -903,12 +903,6 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
|
|||
return randomFrom("1", "-1", "75%", "-25%", "2<75%", "2<-25%");
|
||||
}
|
||||
|
||||
protected static String randomTimeZone() {
|
||||
return randomFrom(TIMEZONE_IDS);
|
||||
}
|
||||
|
||||
private static final List<String> TIMEZONE_IDS = new ArrayList<>(DateTimeZone.getAvailableIDs());
|
||||
|
||||
private static class ClientInvocationHandler implements InvocationHandler {
|
||||
AbstractQueryTestCase<?> delegate;
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import org.apache.lucene.document.IntPoint;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queryparser.classic.MapperQueryParser;
|
||||
import org.apache.lucene.queryparser.classic.QueryParserSettings;
|
||||
|
@ -27,7 +26,6 @@ import org.apache.lucene.search.BooleanClause;
|
|||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.BoostQuery;
|
||||
import org.apache.lucene.search.DisjunctionMaxQuery;
|
||||
import org.apache.lucene.search.LegacyNumericRangeQuery;
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
import org.apache.lucene.search.MatchNoDocsQuery;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
|
@ -148,7 +146,7 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStr
|
|||
queryStringQueryBuilder.locale(randomLocale(random()));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
queryStringQueryBuilder.timeZone(randomTimeZone());
|
||||
queryStringQueryBuilder.timeZone(randomDateTimeZone().getID());
|
||||
}
|
||||
return queryStringQueryBuilder;
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
|
|||
// otherwise we could trigger exception.
|
||||
if (createShardContext().getMapperService().fullName(DATE_FIELD_NAME) != null) {
|
||||
if (randomBoolean()) {
|
||||
query.timeZone(randomTimeZone());
|
||||
query.timeZone(randomDateTimeZone().getID());
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
query.format("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
|
||||
|
|
|
@ -120,7 +120,7 @@ public class DateProcessorFactoryTests extends ESTestCase {
|
|||
config.put("field", sourceField);
|
||||
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
|
||||
|
||||
DateTimeZone timezone = randomTimezone();
|
||||
DateTimeZone timezone = randomDateTimeZone();
|
||||
config.put("timezone", timezone.getID());
|
||||
DateProcessor processor = factory.create(config);
|
||||
assertThat(processor.getTimezone(), equalTo(timezone));
|
||||
|
@ -141,14 +141,6 @@ public class DateProcessorFactoryTests extends ESTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
//we generate a timezone out of the available ones in joda, some available in the jdk are not available in joda by default
|
||||
private static DateTimeZone randomTimezone() {
|
||||
List<String> ids = new ArrayList<>(DateTimeZone.getAvailableIDs());
|
||||
Collections.sort(ids);
|
||||
return DateTimeZone.forID(randomFrom(ids));
|
||||
}
|
||||
|
||||
|
||||
public void testParseMatchFormats() throws Exception {
|
||||
DateProcessor.Factory factory = new DateProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
|
|
|
@ -295,8 +295,7 @@ public class DateRangeIT extends ESIntegTestCase {
|
|||
}
|
||||
|
||||
public void testSingleValueFieldWithDateMath() throws Exception {
|
||||
String[] ids = DateTimeZone.getAvailableIDs().toArray(new String[DateTimeZone.getAvailableIDs().size()]);
|
||||
DateTimeZone timezone = DateTimeZone.forID(randomFrom(ids));
|
||||
DateTimeZone timezone = randomDateTimeZone();
|
||||
int timeZoneOffset = timezone.getOffset(date(2, 15));
|
||||
// if time zone is UTC (or equivalent), time zone suffix is "Z", else something like "+03:00", which we get with the "ZZ" format
|
||||
String feb15Suffix = timeZoneOffset == 0 ? "Z" : date(2,15, timezone).toString("ZZ");
|
||||
|
|
|
@ -22,12 +22,9 @@ package org.elasticsearch.search.aggregations.bucket;
|
|||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregator.Range;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
public class DateRangeTests extends BaseAggregationTestCase<DateRangeAggregationBuilder> {
|
||||
|
||||
private final static String[] timeZoneIds = DateTimeZone.getAvailableIDs().toArray(new String[DateTimeZone.getAvailableIDs().size()]);
|
||||
|
||||
@Override
|
||||
protected DateRangeAggregationBuilder createTestAggregatorBuilder() {
|
||||
int numRanges = randomIntBetween(1, 10);
|
||||
|
@ -60,7 +57,7 @@ public class DateRangeTests extends BaseAggregationTestCase<DateRangeAggregation
|
|||
factory.missing(randomIntBetween(0, 10));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
factory.timeZone(DateTimeZone.forID(randomFrom(timeZoneIds)));
|
||||
factory.timeZone(randomDateTimeZone());
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
package org.elasticsearch.test;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.RandomizedContext;
|
||||
import com.carrotsearch.randomizedtesting.RandomizedTest;
|
||||
import com.carrotsearch.randomizedtesting.annotations.TestGroup;
|
||||
import com.carrotsearch.randomizedtesting.generators.RandomInts;
|
||||
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
||||
|
@ -124,7 +123,6 @@ import org.elasticsearch.test.store.MockFSIndexStore;
|
|||
import org.elasticsearch.test.transport.AssertingLocalTransport;
|
||||
import org.elasticsearch.test.transport.MockTransportService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
|
@ -1826,23 +1824,6 @@ public abstract class ESIntegTestCase extends ESTestCase {
|
|||
return perTestRatio;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random JODA Time Zone based on Java Time Zones
|
||||
*/
|
||||
public static DateTimeZone randomDateTimeZone() {
|
||||
DateTimeZone timeZone;
|
||||
|
||||
// It sounds like some Java Time Zones are unknown by JODA. For example: Asia/Riyadh88
|
||||
// We need to fallback in that case to a known time zone
|
||||
try {
|
||||
timeZone = DateTimeZone.forTimeZone(RandomizedTest.randomTimeZone());
|
||||
} catch (IllegalArgumentException e) {
|
||||
timeZone = DateTimeZone.forOffsetHours(randomIntBetween(-12, 12));
|
||||
}
|
||||
|
||||
return timeZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns path to a random directory that can be used to create a temporary file system repo
|
||||
*/
|
||||
|
|
|
@ -61,6 +61,7 @@ import org.elasticsearch.search.MockSearchService;
|
|||
import org.elasticsearch.test.junit.listeners.LoggingListener;
|
||||
import org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
|
@ -398,6 +399,15 @@ public abstract class ESTestCase extends LuceneTestCase {
|
|||
return randomTimeValue(1, 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* generate a random DateTimeZone from the ones available in joda library
|
||||
*/
|
||||
public static DateTimeZone randomDateTimeZone() {
|
||||
List<String> ids = new ArrayList<>(DateTimeZone.getAvailableIDs());
|
||||
Collections.sort(ids);
|
||||
return DateTimeZone.forID(randomFrom(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* helper to randomly perform on <code>consumer</code> with <code>value</code>
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue