Skip to content

Commit 9bb69a2

Browse files
authored
Fix delay timeout in MockTransportService (#97336) (#97338)
We can pass an invalid duration to TimeValue, which accepts either -1 or an non-negative duration.
1 parent 4ccdcce commit 9bb69a2

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,12 @@ public void addUnresponsiveRule(TransportService transportService, final TimeVal
404404
* @param duration the amount of time to delay sending and connecting.
405405
*/
406406
public void addUnresponsiveRule(TransportAddress transportAddress, final TimeValue duration) {
407-
final long startTime = System.currentTimeMillis();
407+
final long startTimeInMillis = threadPool.relativeTimeInMillis();
408408

409-
Supplier<TimeValue> delaySupplier = () -> new TimeValue(duration.millis() - (System.currentTimeMillis() - startTime));
409+
Supplier<TimeValue> delaySupplier = () -> {
410+
long elapsed = threadPool.relativeTimeInMillis() - startTimeInMillis;
411+
return new TimeValue(Math.max(duration.millis() - elapsed, 0));
412+
};
410413

411414
transport().addConnectBehavior(transportAddress, new StubbableTransport.OpenConnectionBehavior() {
412415
private CountDownLatch stopLatch = new CountDownLatch(1);

0 commit comments

Comments
 (0)