From c43ae014b45ab5ddadbcebfe0e150e62c8c0fafd Mon Sep 17 00:00:00 2001 From: Hendrik Muhs Date: Wed, 10 May 2017 16:17:30 +0200 Subject: [PATCH] Replace timestamp initialization 'now' to an explicit timestamp. (elastic/x-pack-elasticsearch#1383) fixes build errors. Still ensures that the timestamp is set to 'now' if the parsed logfile misses it. Original commit: elastic/x-pack-elasticsearch@cf60e8d76bee4ae8e31896b7bce9c3a2b8060d7c --- .../ml/job/process/logging/CppLogMessage.java | 6 ++-- .../process/logging/CppLogMessageTests.java | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessage.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessage.java index aaf5373536b..27fff18d8cb 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessage.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessage.java @@ -37,7 +37,7 @@ public class CppLogMessage extends ToXContentToBytes implements Writeable { public static final ParseField LINE_FIELD = new ParseField("line"); public static final ObjectParser PARSER = new ObjectParser<>( - LOGGER_FIELD.getPreferredName(), CppLogMessage::new); + LOGGER_FIELD.getPreferredName(), () -> new CppLogMessage(Instant.now())); static { PARSER.declareString(CppLogMessage::setLogger, LOGGER_FIELD); @@ -68,8 +68,8 @@ public class CppLogMessage extends ToXContentToBytes implements Writeable { private String file = ""; private long line = 0; - public CppLogMessage() { - timestamp = Instant.now(); + public CppLogMessage(Instant timestamp) { + this.timestamp = timestamp; } public CppLogMessage(StreamInput in) throws IOException { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessageTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessageTests.java index a2ec8a76fad..516606e4cfe 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessageTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessageTests.java @@ -6,15 +6,22 @@ package org.elasticsearch.xpack.ml.job.process.logging; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContent; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.xpack.ml.support.AbstractSerializingTestCase; +import java.io.IOException; +import java.time.Instant; + public class CppLogMessageTests extends AbstractSerializingTestCase { public void testDefaultConstructor() { - CppLogMessage msg = new CppLogMessage(); + CppLogMessage msg = new CppLogMessage(Instant.ofEpochSecond(1494422876L)); assertEquals("", msg.getLogger()); - assertTrue(msg.getTimestamp().toString(), msg.getTimestamp().toEpochMilli() > 0); + assertEquals(Instant.ofEpochSecond(1494422876L), msg.getTimestamp()); assertEquals("", msg.getLevel()); assertEquals(0, msg.getPid()); assertEquals("", msg.getThread()); @@ -25,9 +32,24 @@ public class CppLogMessageTests extends AbstractSerializingTestCase