Skip to content

Commit 457af40

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 16d8254 commit 457af40

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
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/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 & 3 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;
@@ -275,7 +277,6 @@ public void testConfigureIndexInMapAndAction() {
275277
fieldName + "] or [ctx.payload._doc." + fieldName + "]"));
276278
}
277279

278-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38581")
279280
public void testIndexActionExecuteSingleDoc() throws Exception {
280281
boolean customId = randomBoolean();
281282
boolean docIdAsParam = customId && randomBoolean();
@@ -286,7 +287,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
286287
refreshPolicy);
287288
ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30),
288289
TimeValue.timeValueSeconds(30));
289-
ZonedDateTime executionTime = ZonedDateTime.now(ZoneOffset.UTC);
290+
ZonedDateTime executionTime = DateUtils.nowWithMillisResolution();
290291
Payload payload;
291292

292293
if (customId && docIdAsParam == false) {
@@ -326,7 +327,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
326327

327328
if (timestampField != null) {
328329
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(2)));
329-
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, executionTime.toString()));
330+
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, WatcherDateTimeUtils.formatDate(executionTime)));
330331
} else {
331332
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(1)));
332333
}

0 commit comments

Comments
 (0)