Skip to content

Commit 7e178aa

Browse files
authored
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
1 parent 90fff54 commit 7e178aa

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

server/src/main/java/org/elasticsearch/common/time/DateUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import org.elasticsearch.common.logging.DeprecationLogger;
2424
import org.joda.time.DateTimeZone;
2525

26+
import java.time.Clock;
27+
import java.time.Duration;
2628
import java.time.Instant;
2729
import java.time.ZoneId;
2830
import java.time.ZoneOffset;
31+
import java.time.ZonedDateTime;
2932
import java.util.Collections;
3033
import java.util.HashMap;
3134
import java.util.Map;
@@ -217,4 +220,22 @@ private static long of(final int year, final int month) {
217220
millis += getTotalMillisByYearMonth(year, month);
218221
return millis;
219222
}
223+
224+
/**
225+
* Returns the current UTC date-time with milliseconds precision.
226+
* In Java 9+ (as opposed to Java 8) the {@code Clock} implementation uses system's best clock implementation (which could mean
227+
* that the precision of the clock can be milliseconds, microseconds or nanoseconds), whereas in Java 8
228+
* {@code System.currentTimeMillis()} is always used. To account for these differences, this method defines a new {@code Clock}
229+
* which will offer a value for {@code ZonedDateTime.now()} set to always have milliseconds precision.
230+
*
231+
* @return {@link ZonedDateTime} instance for the current date-time with milliseconds precision in UTC
232+
*/
233+
public static ZonedDateTime nowWithMillisResolution() {
234+
return nowWithMillisResolution(Clock.systemUTC());
235+
}
236+
237+
public static ZonedDateTime nowWithMillisResolution(Clock clock) {
238+
Clock millisResolutionClock = Clock.tick(clock, Duration.ofMillis(1));
239+
return ZonedDateTime.now(millisResolutionClock);
240+
}
220241
}

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherIndexingListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.time.Clock;
35+
import java.time.Instant;
3536
import java.time.ZoneOffset;
3637
import java.time.ZonedDateTime;
3738
import java.util.ArrayList;
@@ -101,7 +102,7 @@ void setConfiguration(Configuration configuration) {
101102
@Override
102103
public Engine.Index preIndex(ShardId shardId, Engine.Index operation) {
103104
if (isWatchDocument(shardId.getIndexName(), operation.type())) {
104-
ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC);
105+
ZonedDateTime now = Instant.ofEpochMilli(clock.millis()).atZone(ZoneOffset.UTC);
105106
try {
106107
Watch watch = parser.parseWithSecrets(operation.id(), true, operation.source(), now, XContentType.JSON,
107108
operation.getIfSeqNo(), operation.getIfPrimaryTerm());

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherIndexingListenerTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.cluster.routing.TestShardRouting;
2828
import org.elasticsearch.common.bytes.BytesArray;
2929
import org.elasticsearch.common.settings.Settings;
30+
import org.elasticsearch.common.time.DateUtils;
3031
import org.elasticsearch.index.Index;
3132
import org.elasticsearch.index.engine.Engine;
3233
import org.elasticsearch.index.shard.ShardId;
@@ -41,7 +42,6 @@
4142
import org.junit.Before;
4243

4344
import java.io.IOException;
44-
import java.time.ZoneOffset;
4545
import java.time.ZonedDateTime;
4646
import java.util.ArrayList;
4747
import java.util.BitSet;
@@ -126,7 +126,6 @@ public void testPreIndexCheckActive() throws Exception {
126126
verifyZeroInteractions(parser);
127127
}
128128

129-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38581")
130129
public void testPreIndex() throws Exception {
131130
when(operation.type()).thenReturn(Watch.DOC_TYPE);
132131
when(operation.id()).thenReturn(randomAlphaOfLength(10));
@@ -140,8 +139,7 @@ public void testPreIndex() throws Exception {
140139

141140
Engine.Index returnedOperation = listener.preIndex(shardId, operation);
142141
assertThat(returnedOperation, is(operation));
143-
144-
ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC);
142+
ZonedDateTime now = DateUtils.nowWithMillisResolution(clock);
145143
verify(parser).parseWithSecrets(eq(operation.id()), eq(true), eq(BytesArray.EMPTY), eq(now), anyObject(), anyLong(), anyLong());
146144

147145
if (isNewWatch) {

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.client.Client;
1818
import org.elasticsearch.common.collect.MapBuilder;
1919
import org.elasticsearch.common.settings.Settings;
20+
import org.elasticsearch.common.time.DateUtils;
2021
import org.elasticsearch.common.unit.TimeValue;
2122
import org.elasticsearch.common.util.concurrent.ThreadContext;
2223
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -28,6 +29,7 @@
2829
import org.elasticsearch.xpack.core.watcher.actions.Action;
2930
import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status;
3031
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
32+
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
3133
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
3234
import org.elasticsearch.xpack.core.watcher.watch.Payload;
3335
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
@@ -36,7 +38,6 @@
3638

3739
import java.time.ZoneOffset;
3840
import java.time.ZonedDateTime;
39-
import java.time.format.DateTimeFormatter;
4041
import java.util.ArrayList;
4142
import java.util.Arrays;
4243
import java.util.Collections;
@@ -276,7 +277,6 @@ public void testConfigureIndexInMapAndAction() {
276277
fieldName + "] or [ctx.payload._doc." + fieldName + "]"));
277278
}
278279

279-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38581")
280280
public void testIndexActionExecuteSingleDoc() throws Exception {
281281
boolean customId = randomBoolean();
282282
boolean docIdAsParam = customId && randomBoolean();
@@ -287,7 +287,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
287287
refreshPolicy);
288288
ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30),
289289
TimeValue.timeValueSeconds(30));
290-
ZonedDateTime executionTime = ZonedDateTime.now(ZoneOffset.UTC);
290+
ZonedDateTime executionTime = DateUtils.nowWithMillisResolution();
291291
Payload payload;
292292

293293
if (customId && docIdAsParam == false) {
@@ -326,9 +326,8 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
326326
assertThat(indexRequest.getRefreshPolicy(), is(expectedRefreshPolicy));
327327

328328
if (timestampField != null) {
329-
final DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
330329
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(2)));
331-
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, formatter.format(executionTime)));
330+
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, WatcherDateTimeUtils.formatDate(executionTime)));
332331
} else {
333332
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(1)));
334333
}

0 commit comments

Comments
 (0)