Skip to content

Commit

Permalink
Enable IndexActionTests and WatcherIndexingListenerTests (elastic#38738)
Browse files Browse the repository at this point in the history
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 elastic#38514 possibly because of merge conflicts

closes elastic#38581
  • Loading branch information
pgomulka committed Feb 12, 2019
1 parent c7cdd6a commit 4e786de
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
21 changes: 21 additions & 0 deletions server/src/main/java/org/elasticsearch/common/time/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import org.elasticsearch.common.logging.DeprecationLogger;
import org.joda.time.DateTimeZone;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -217,4 +220,22 @@ private static long of(final int year, final int month) {
millis += getTotalMillisByYearMonth(year, month);
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.IOException;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -101,7 +102,7 @@ void setConfiguration(Configuration configuration) {
@Override
public Engine.Index preIndex(ShardId shardId, Engine.Index operation) {
if (isWatchDocument(shardId.getIndexName(), operation.type())) {
ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC);
ZonedDateTime now = Instant.ofEpochMilli(clock.millis()).atZone(ZoneOffset.UTC);
try {
Watch watch = parser.parseWithSecrets(operation.id(), true, operation.source(), now, XContentType.JSON,
operation.getIfSeqNo(), operation.getIfPrimaryTerm());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.shard.ShardId;
Expand All @@ -41,7 +42,6 @@
import org.junit.Before;

import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.BitSet;
Expand Down Expand Up @@ -126,7 +126,6 @@ public void testPreIndexCheckActive() throws Exception {
verifyZeroInteractions(parser);
}

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

Engine.Index returnedOperation = listener.preIndex(shardId, operation);
assertThat(returnedOperation, is(operation));

ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC);
ZonedDateTime now = DateUtils.nowWithMillisResolution(clock);
verify(parser).parseWithSecrets(eq(operation.id()), eq(true), eq(BytesArray.EMPTY), eq(now), anyObject(), anyLong(), anyLong());

if (isNewWatch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -28,6 +29,7 @@
import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status;
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.watch.Payload;
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
Expand All @@ -36,7 +38,6 @@

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -276,7 +277,6 @@ public void testConfigureIndexInMapAndAction() {
fieldName + "] or [ctx.payload._doc." + fieldName + "]"));
}

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

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

if (timestampField != null) {
final DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(2)));
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, formatter.format(executionTime)));
assertThat(indexRequest.sourceAsMap(), hasEntry(timestampField, WatcherDateTimeUtils.formatDate(executionTime)));
} else {
assertThat(indexRequest.sourceAsMap().keySet(), is(hasSize(1)));
}
Expand Down

0 comments on commit 4e786de

Please sign in to comment.