Skip to content

Commit 3fa1e0a

Browse files
Fixing the reference time so that age does not change during a test (#116900)
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(). Co-authored-by: Keith Massey <[email protected]>
1 parent aa83dac commit 3fa1e0a

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.util.Objects;
26+
import java.util.function.Supplier;
2627
import java.util.stream.Stream;
2728

2829
public class IndexLifecycleExplainResponse implements ToXContentObject, Writeable {
@@ -124,6 +125,8 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl
124125
private final String snapshotName;
125126
private final String shrinkIndexName;
126127

128+
Supplier<Long> nowSupplier = System::currentTimeMillis; // Can be changed for testing
129+
127130
public static IndexLifecycleExplainResponse newManagedIndexResponse(
128131
String index,
129132
String policyName,
@@ -412,11 +415,11 @@ public Integer getFailedStepRetryCount() {
412415
return failedStepRetryCount;
413416
}
414417

415-
public TimeValue getAge() {
418+
public TimeValue getAge(Supplier<Long> now) {
416419
if (lifecycleDate == null) {
417420
return TimeValue.MINUS_ONE;
418421
} else {
419-
return TimeValue.timeValueMillis(Math.max(0L, System.currentTimeMillis() - lifecycleDate));
422+
return TimeValue.timeValueMillis(Math.max(0L, now.get() - lifecycleDate));
420423
}
421424
}
422425

@@ -441,7 +444,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
441444
builder.field(POLICY_NAME_FIELD.getPreferredName(), policyName);
442445
if (lifecycleDate != null) {
443446
builder.timeField(LIFECYCLE_DATE_MILLIS_FIELD.getPreferredName(), LIFECYCLE_DATE_FIELD.getPreferredName(), lifecycleDate);
444-
builder.field(AGE_FIELD.getPreferredName(), getAge().toHumanReadableString(2));
447+
builder.field(AGE_FIELD.getPreferredName(), getAge(nowSupplier).toHumanReadableString(2));
445448
}
446449
if (phase != null) {
447450
builder.field(PHASE_FIELD.getPreferredName(), phase);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@
3434
public class IndexLifecycleExplainResponseTests extends AbstractSerializingTestCase<IndexLifecycleExplainResponse> {
3535

3636
static IndexLifecycleExplainResponse randomIndexExplainResponse() {
37+
final IndexLifecycleExplainResponse indexLifecycleExplainResponse;
3738
if (frequently()) {
38-
return randomManagedIndexExplainResponse();
39+
indexLifecycleExplainResponse = randomManagedIndexExplainResponse();
3940
} else {
40-
return randomUnmanagedIndexExplainResponse();
41+
indexLifecycleExplainResponse = randomUnmanagedIndexExplainResponse();
4142
}
43+
long now = System.currentTimeMillis();
44+
// So that now is the same for the duration of the test. See #84352
45+
indexLifecycleExplainResponse.nowSupplier = () -> now;
46+
return indexLifecycleExplainResponse;
4247
}
4348

4449
private static IndexLifecycleExplainResponse randomUnmanagedIndexExplainResponse() {
@@ -116,7 +121,7 @@ public void testNegativeAge() {
116121
null,
117122
null
118123
);
119-
assertThat(response.getAge(), equalTo(TimeValue.ZERO));
124+
assertThat(response.getAge(System::currentTimeMillis), equalTo(TimeValue.ZERO));
120125
}
121126

122127
@Override

0 commit comments

Comments
 (0)