@@ -174,6 +174,11 @@ public final class DohResolver implements Resolver {
174174 USE_HTTP_CLIENT = initSuccess ;
175175 }
176176
177+ // package-visible for testing
178+ long getNanoTime () {
179+ return System .nanoTime ();
180+ }
181+
177182 /**
178183 * Creates a new DoH resolver that performs lookups with HTTP GET and the default timeout (5s).
179184 *
@@ -315,7 +320,7 @@ public CompletionStage<Message> sendAsync(Message query, Executor executor) {
315320 private CompletionStage <Message > sendAsync8 (final Message query , Executor executor ) {
316321 byte [] queryBytes = prepareQuery (query ).toWire ();
317322 String url = getUrl (queryBytes );
318- long startTime = System . nanoTime ();
323+ long startTime = getNanoTime ();
319324 return maxConcurrentRequests
320325 .acquire (timeout )
321326 .handleAsync (
@@ -363,7 +368,7 @@ private SendAndGetMessageBytesResponse sendAndGetMessageBytes(
363368 ((HttpsURLConnection ) conn ).setSSLSocketFactory (sslSocketFactory );
364369 }
365370
366- Duration remainingTimeout = timeout .minus (System . nanoTime () - startTime , ChronoUnit .NANOS );
371+ Duration remainingTimeout = timeout .minus (getNanoTime () - startTime , ChronoUnit .NANOS );
367372 conn .setConnectTimeout ((int ) remainingTimeout .toMillis ());
368373 conn .setReadTimeout ((int ) remainingTimeout .toMillis ());
369374 conn .setRequestMethod (usePost ? "POST" : "GET" );
@@ -389,7 +394,7 @@ private SendAndGetMessageBytesResponse sendAndGetMessageBytes(
389394 int offset = 0 ;
390395 while ((r = is .read (responseBytes , offset , responseBytes .length - offset )) > 0 ) {
391396 offset += r ;
392- remainingTimeout = timeout .minus (System . nanoTime () - startTime , ChronoUnit .NANOS );
397+ remainingTimeout = timeout .minus (getNanoTime () - startTime , ChronoUnit .NANOS );
393398 if (remainingTimeout .isNegative ()) {
394399 throw new SocketTimeoutException ();
395400 }
@@ -403,7 +408,7 @@ private SendAndGetMessageBytesResponse sendAndGetMessageBytes(
403408 byte [] buffer = new byte [4096 ];
404409 int r ;
405410 while ((r = is .read (buffer , 0 , buffer .length )) > 0 ) {
406- remainingTimeout = timeout .minus (System . nanoTime () - startTime , ChronoUnit .NANOS );
411+ remainingTimeout = timeout .minus (getNanoTime () - startTime , ChronoUnit .NANOS );
407412 if (remainingTimeout .isNegative ()) {
408413 throw new SocketTimeoutException ();
409414 }
@@ -432,7 +437,7 @@ private void discardStream(InputStream es) throws IOException {
432437 }
433438
434439 private CompletionStage <Message > sendAsync11 (final Message query , Executor executor ) {
435- long startTime = System . nanoTime ();
440+ long startTime = getNanoTime ();
436441 byte [] queryBytes = prepareQuery (query ).toWire ();
437442 String url = getUrl (queryBytes );
438443
@@ -454,7 +459,7 @@ private CompletionStage<Message> sendAsync11(final Message query, Executor execu
454459 // check if this request needs to be done synchronously because of HttpClient's stupidity to
455460 // not use the connection pool for HTTP/2 until one connection is successfully established,
456461 // which could lead to hundreds of connections (and threads with the default executor)
457- Duration remainingTimeout = timeout .minus (System . nanoTime () - startTime , ChronoUnit .NANOS );
462+ Duration remainingTimeout = timeout .minus (getNanoTime () - startTime , ChronoUnit .NANOS );
458463 return initialRequestLock
459464 .acquire (remainingTimeout )
460465 .handle (
@@ -476,14 +481,13 @@ private CompletionStage<Message> sendAsync11WithInitialRequestPermit(
476481 Object requestBuilder ,
477482 Permit initialRequestPermit ) {
478483 long lastRequestTime = lastRequest .get ();
479- boolean isInitialRequest =
480- (lastRequestTime < System .nanoTime () - idleConnectionTimeout .toNanos ());
484+ boolean isInitialRequest = idleConnectionTimeout .toNanos () > getNanoTime () - lastRequestTime ;
481485 if (!isInitialRequest ) {
482486 initialRequestPermit .release ();
483487 }
484488
485489 // check if we already exceeded the query timeout while checking the initial connection
486- Duration remainingTimeout = timeout .minus (System . nanoTime () - startTime , ChronoUnit .NANOS );
490+ Duration remainingTimeout = timeout .minus (getNanoTime () - startTime , ChronoUnit .NANOS );
487491 if (remainingTimeout .isNegative ()) {
488492 if (isInitialRequest ) {
489493 initialRequestPermit .release ();
@@ -525,7 +529,7 @@ private CompletionStage<Message> sendAsync11WithConcurrentRequestPermit(
525529 boolean isInitialRequest ,
526530 Permit maxConcurrentRequestPermit ) {
527531 // check if the stream lock acquisition took too long
528- Duration remainingTimeout = timeout .minus (System . nanoTime () - startTime , ChronoUnit .NANOS );
532+ Duration remainingTimeout = timeout .minus (getNanoTime () - startTime , ChronoUnit .NANOS );
529533 if (remainingTimeout .isNegative ()) {
530534 if (isInitialRequest ) {
531535 initialRequestPermit .release ();
0 commit comments