Skip to content

Commit 30bcc75

Browse files
authored
Fixing the reference time so that age does not change during a test (#84528) (#84572)
This change makes it so that the reference time from which the "age" field of the IndexLifecycleExplainResponse object is derived does not change for the duration of testConcurrentToXContent().
1 parent 4a9a7fc commit 30bcc75

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl
133133
private final String snapshotName;
134134
private final String shrinkIndexName;
135135

136+
Supplier<Long> nowSupplier = System::currentTimeMillis; // Can be changed for testing
137+
136138
public static IndexLifecycleExplainResponse newManagedIndexResponse(
137139
String index,
138140
Long indexCreationDate,
@@ -463,12 +465,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
463465
);
464466
builder.field(
465467
TIME_SINCE_INDEX_CREATION_FIELD.getPreferredName(),
466-
getTimeSinceIndexCreation(System::currentTimeMillis).toHumanReadableString(2)
468+
getTimeSinceIndexCreation(nowSupplier).toHumanReadableString(2)
467469
);
468470
}
469471
if (lifecycleDate != null) {
470472
builder.timeField(LIFECYCLE_DATE_MILLIS_FIELD.getPreferredName(), LIFECYCLE_DATE_FIELD.getPreferredName(), lifecycleDate);
471-
builder.field(AGE_FIELD.getPreferredName(), getAge(System::currentTimeMillis).toHumanReadableString(2));
473+
builder.field(AGE_FIELD.getPreferredName(), getAge(nowSupplier).toHumanReadableString(2));
472474
}
473475
if (phase != null) {
474476
builder.field(PHASE_FIELD.getPreferredName(), phase);

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponseTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@
3737
public class IndexLifecycleExplainResponseTests extends AbstractSerializingTestCase<IndexLifecycleExplainResponse> {
3838

3939
static IndexLifecycleExplainResponse randomIndexExplainResponse() {
40+
final IndexLifecycleExplainResponse indexLifecycleExplainResponse;
4041
if (frequently()) {
41-
return randomManagedIndexExplainResponse();
42+
indexLifecycleExplainResponse = randomManagedIndexExplainResponse();
4243
} else {
43-
return randomUnmanagedIndexExplainResponse();
44+
indexLifecycleExplainResponse = randomUnmanagedIndexExplainResponse();
4445
}
46+
long now = System.currentTimeMillis();
47+
// So that now is the same for the duration of the test. See #84352
48+
indexLifecycleExplainResponse.nowSupplier = () -> now;
49+
return indexLifecycleExplainResponse;
4550
}
4651

4752
private static IndexLifecycleExplainResponse randomUnmanagedIndexExplainResponse() {

0 commit comments

Comments
 (0)