Skip to content

Commit

Permalink
Fix delay timeout in MockTransportService (#97336) (#97338)
Browse files Browse the repository at this point in the history
We can pass an invalid duration to TimeValue, which accepts either -1 or
an non-negative duration.
  • Loading branch information
dnhatn committed Jul 3, 2023
1 parent 4ccdcce commit 9bb69a2
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,12 @@ public void addUnresponsiveRule(TransportService transportService, final TimeVal
* @param duration the amount of time to delay sending and connecting.
*/
public void addUnresponsiveRule(TransportAddress transportAddress, final TimeValue duration) {
final long startTime = System.currentTimeMillis();
final long startTimeInMillis = threadPool.relativeTimeInMillis();

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

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

0 comments on commit 9bb69a2

Please sign in to comment.