Skip to content

Commit 6a97abf

Browse files
authored
Fix HttpClientStatsTrackerTests#testToXContent (#134617) (#134633)
It turns out that `Instant#toString()` disagrees with the `XContent` timestamp formatter when the timestamp has a zero milliseconds component. This commit adjusts the tests to use the correct formatter. Closes #134581
1 parent 783bda6 commit 6a97abf

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.ElasticsearchParseException;
1313
import org.elasticsearch.common.util.LocaleUtils;
14+
import org.elasticsearch.common.xcontent.XContentElasticsearchExtension;
1415
import org.elasticsearch.index.mapper.DateFieldMapper;
1516
import org.elasticsearch.test.ESTestCase;
1617
import org.hamcrest.Matcher;
@@ -1450,4 +1451,19 @@ public void testNoClassCastException() {
14501451
assertThat(e.getCause(), instanceOf(ClassCastException.class));
14511452
assertThat(e.getMessage(), containsString(input));
14521453
}
1454+
1455+
public void testXContentElasticsearchExtensionDefaultFormatter() {
1456+
final var formatter = DateFormatter.forPattern("strict_date_optional_time_nanos");
1457+
assertSame(XContentElasticsearchExtension.DEFAULT_FORMATTER, formatter);
1458+
1459+
assertEquals("2025-09-12T08:12:12.123Z", formatter.format(Instant.ofEpochMilli(1757664732123L)));
1460+
assertEquals("2025-09-12T08:12:12.000Z", formatter.format(Instant.ofEpochMilli(1757664732000L)));
1461+
assertEquals("2025-09-12T08:12:00.000Z", formatter.format(Instant.ofEpochMilli(1757664720000L)));
1462+
assertEquals("2025-09-12T08:00:00.000Z", formatter.format(Instant.ofEpochMilli(1757664000000L)));
1463+
assertEquals("2025-09-12T00:00:00.000Z", formatter.format(Instant.ofEpochMilli(1757635200000L)));
1464+
1465+
// NB differs from Instant.toString():
1466+
assertEquals("2025-09-12T08:12:12.123Z", Instant.ofEpochMilli(1757664732123L).toString());
1467+
assertEquals("2025-09-12T08:12:00Z", Instant.ofEpochMilli(1757664720000L).toString());
1468+
}
14531469
}

server/src/test/java/org/elasticsearch/http/HttpClientStatsTrackerTests.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.settings.ClusterSettings;
1717
import org.elasticsearch.common.settings.Settings;
1818
import org.elasticsearch.common.util.Maps;
19+
import org.elasticsearch.common.xcontent.XContentElasticsearchExtension;
1920
import org.elasticsearch.common.xcontent.XContentHelper;
2021
import org.elasticsearch.core.TimeValue;
2122
import org.elasticsearch.node.Node;
@@ -41,6 +42,7 @@
4142
import java.util.concurrent.atomic.AtomicBoolean;
4243
import java.util.concurrent.locks.ReadWriteLock;
4344
import java.util.concurrent.locks.ReentrantReadWriteLock;
45+
import java.util.function.BiConsumer;
4446
import java.util.function.Consumer;
4547

4648
import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_CLIENT_STATS_ENABLED;
@@ -419,18 +421,18 @@ public void testToXContent() throws IOException {
419421
assertEquals(description, clientStats.forwardedFor(), xcontentMap.get("x_forwarded_for"));
420422
assertEquals(description, clientStats.opaqueId(), xcontentMap.get("x_opaque_id"));
421423

422-
assertEquals(description, clientStats.openedTimeMillis(), xcontentMap.get("opened_time_millis"));
423-
assertEquals(description, Instant.ofEpochMilli(clientStats.openedTimeMillis()).toString(), xcontentMap.get("opened_time"));
424-
425-
assertEquals(description, clientStats.closedTimeMillis(), xcontentMap.get("closed_time_millis"));
426-
assertEquals(description, Instant.ofEpochMilli(clientStats.closedTimeMillis()).toString(), xcontentMap.get("closed_time"));
424+
final BiConsumer<Long, String> timestampFieldAsserter = (timestampMillis, fieldName) -> {
425+
assertEquals(description, timestampMillis, xcontentMap.get(fieldName + "_millis"));
426+
assertEquals(
427+
description,
428+
XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(timestampMillis)),
429+
xcontentMap.get(fieldName)
430+
);
431+
};
427432

428-
assertEquals(description, clientStats.lastRequestTimeMillis(), xcontentMap.get("last_request_time_millis"));
429-
assertEquals(
430-
description,
431-
Instant.ofEpochMilli(clientStats.lastRequestTimeMillis()).toString(),
432-
xcontentMap.get("last_request_time")
433-
);
433+
timestampFieldAsserter.accept(clientStats.openedTimeMillis(), "opened_time");
434+
timestampFieldAsserter.accept(clientStats.closedTimeMillis(), "closed_time");
435+
timestampFieldAsserter.accept(clientStats.lastRequestTimeMillis(), "last_request_time");
434436

435437
assertEquals(description, clientStats.requestCount(), (long) xcontentMap.get("request_count"));
436438
assertEquals(description, clientStats.requestSizeBytes(), (long) xcontentMap.get("request_size_bytes"));

0 commit comments

Comments
 (0)