Enable IndexActionTests and WatcherIndexingListenerTests Backport #38738

fix tests to use clock in milliseconds precision in watcher code
make sure the date comparison in string format is using same formatters
some of the code was modified in #38514 possibly because of merge conflicts

closes #38581
Backport #38738
This commit is contained in:
Przemyslaw Gomulka 2019-02-12 13:05:44 +01:00 committed by GitHub
parent 90fff54954
commit 7e178aa4a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 10 deletions

View File

@ -23,9 +23,12 @@ import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.DeprecationLogger;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -217,4 +220,22 @@ public class DateUtils {
millis += getTotalMillisByYearMonth(year, month); millis += getTotalMillisByYearMonth(year, month);
return millis; return millis;
} }
/**
* Returns the current UTC date-time with milliseconds precision.
* In Java 9+ (as opposed to Java 8) the {@code Clock} implementation uses system's best clock implementation (which could mean
* that the precision of the clock can be milliseconds, microseconds or nanoseconds), whereas in Java 8
* {@code System.currentTimeMillis()} is always used. To account for these differences, this method defines a new {@code Clock}
* which will offer a value for {@code ZonedDateTime.now()} set to always have milliseconds precision.
*
* @return {@link ZonedDateTime} instance for the current date-time with milliseconds precision in UTC
*/
public static ZonedDateTime nowWithMillisResolution() {
return nowWithMillisResolution(Clock.systemUTC());
}
public static ZonedDateTime nowWithMillisResolution(Clock clock) {
Clock millisResolutionClock = Clock.tick(clock, Duration.ofMillis(1));
return ZonedDateTime.now(millisResolutionClock);
}
} }

View File

@ -32,6 +32,7 @@ import org.elasticsearch.xpack.watcher.watch.WatchStoreUtils;
import java.io.IOException; import java.io.IOException;
import java.time.Clock; import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -101,7 +102,7 @@ final class WatcherIndexingListener implements IndexingOperationListener, Cluste
@Override @Override
public Engine.Index preIndex(ShardId shardId, Engine.Index operation) { public Engine.Index preIndex(ShardId shardId, Engine.Index operation) {
if (isWatchDocument(shardId.getIndexName(), operation.type())) { if (isWatchDocument(shardId.getIndexName(), operation.type())) {
ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); ZonedDateTime now = Instant.ofEpochMilli(clock.millis()).atZone(ZoneOffset.UTC);
try { try {
Watch watch = parser.parseWithSecrets(operation.id(), true, operation.source(), now, XContentType.JSON, Watch watch = parser.parseWithSecrets(operation.id(), true, operation.source(), now, XContentType.JSON,
operation.getIfSeqNo(), operation.getIfPrimaryTerm()); operation.getIfSeqNo(), operation.getIfPrimaryTerm());

View File

@ -27,6 +27,7 @@ import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardId;
@ -41,7 +42,6 @@ import org.elasticsearch.xpack.watcher.watch.WatchParser;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
@ -126,7 +126,6 @@ public class WatcherIndexingListenerTests extends ESTestCase {
verifyZeroInteractions(parser); verifyZeroInteractions(parser);
} }
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38581")
public void testPreIndex() throws Exception { public void testPreIndex() throws Exception {
when(operation.type()).thenReturn(Watch.DOC_TYPE); when(operation.type()).thenReturn(Watch.DOC_TYPE);
when(operation.id()).thenReturn(randomAlphaOfLength(10)); when(operation.id()).thenReturn(randomAlphaOfLength(10));
@ -140,8 +139,7 @@ public class WatcherIndexingListenerTests extends ESTestCase {
Engine.Index returnedOperation = listener.preIndex(shardId, operation); Engine.Index returnedOperation = listener.preIndex(shardId, operation);
assertThat(returnedOperation, is(operation)); assertThat(returnedOperation, is(operation));
ZonedDateTime now = DateUtils.nowWithMillisResolution(clock);
ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC);
verify(parser).parseWithSecrets(eq(operation.id()), eq(true), eq(BytesArray.EMPTY), eq(now), anyObject(), anyLong(), anyLong()); verify(parser).parseWithSecrets(eq(operation.id()), eq(true), eq(BytesArray.EMPTY), eq(now), anyObject(), anyLong(), anyLong());
if (isNewWatch) { if (isNewWatch) {

View File

@ -17,6 +17,7 @@ import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -28,6 +29,7 @@ import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status; import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.core.watcher.watch.Payload;
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils; import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
@ -36,7 +38,6 @@ import org.mockito.ArgumentCaptor;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -276,7 +277,6 @@ public class IndexActionTests extends ESTestCase {
fieldName + "] or [ctx.payload._doc." + fieldName + "]")); fieldName + "] or [ctx.payload._doc." + fieldName + "]"));
} }
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38581")
public void testIndexActionExecuteSingleDoc() throws Exception { public void testIndexActionExecuteSingleDoc() throws Exception {
boolean customId = randomBoolean(); boolean customId = randomBoolean();
boolean docIdAsParam = customId && randomBoolean(); boolean docIdAsParam = customId && randomBoolean();
@ -287,7 +287,7 @@ public class IndexActionTests extends ESTestCase {
refreshPolicy); refreshPolicy);
ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30), ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30),
TimeValue.timeValueSeconds(30)); TimeValue.timeValueSeconds(30));
ZonedDateTime executionTime = ZonedDateTime.now(ZoneOffset.UTC); ZonedDateTime executionTime = DateUtils.nowWithMillisResolution();
Payload payload; Payload payload;
if (customId && docIdAsParam == false) { if (customId && docIdAsParam == false) {
@ -326,9 +326,8 @@ public class IndexActionTests extends ESTestCase {
assertThat(indexRequest.getRefreshPolicy(), is(expectedRefreshPolicy)); assertThat(indexRequest.getRefreshPolicy(), is(expectedRefreshPolicy));
if (timestampField != null) { if (timestampField != null) {
final DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(2))); assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(2)));
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, formatter.format(executionTime))); assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, WatcherDateTimeUtils.formatDate(executionTime)));
} else { } else {
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(1))); assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(1)));
} }