package com.codeborne.selenide;

import javax.annotation.CheckReturnValue;
import javax.annotation.ParametersAreNonnullByDefault;

import static java.lang.System.nanoTime;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

@ParametersAreNonnullByDefault
public class Stopwatch {
  private final long endTimeNano;

  public Stopwatch(long timeoutMs) {
    this.endTimeNano = nanoTime() + MILLISECONDS.toNanos(timeoutMs);
  }

  @CheckReturnValue
  public boolean isTimeoutReached() {
    return nanoTime() > endTimeNano;
  }

  public void sleep(long milliseconds) {
    if (isTimeoutReached()) return;

    try {
      Thread.sleep(milliseconds);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new RuntimeException(e);
    }
  }
}

@@ -301,23 +301,59 @@
 al long 
-endTime
+startTimeNano;%0A  private final long timeout
 Nano;%0A%0A 
@@ -396,16 +396,13 @@
     
-this.end
+start
 Time
@@ -422,10 +422,27 @@
 me()
- +
+;%0A    timeoutNano =
  MIL
@@ -560,17 +560,33 @@
 e() 
-%3E endTime
+- startTimeNano %3E timeout
 Nano

fix flaky StopwatchTest