< prev index next >

test/jdk/com/sun/jndi/dns/ConfigTests/TcpTimeout.java

Print this page

        

@@ -28,37 +28,58 @@
 import java.io.IOException;
 import java.net.BindException;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static jdk.test.lib.Utils.adjustTimeout;
+
 /*
  * @test
  * @bug 8228580
  * @summary Tests that we get a DNS response when the UDP DNS server returns a
  *          truncated response and the TCP DNS server does not respond at all
  *          after connect.
  * @library ../lib/
  * @library /test/lib
  * @modules java.base/sun.security.util
  * @run main TcpTimeout
+ * @run main TcpTimeout -Dcom.sun.jndi.dns.timeout.initial=5000
  */
 
 public class TcpTimeout extends DNSTestBase {
     private TcpDnsServer tcpDnsServer;
 
+    /* The acceptable variation in timeout measurement. */
+    private static final long TOLERANCE = adjustTimeout(5_000);
+    
     public static void main(String[] args) throws Exception {
         new TcpTimeout().run(args);
     }
     
     @Override
     public void runTest() throws Exception {
-        /* Using default test context because we rely on default DNS client
-        timeout setting (1 second). */
+        /* Default timeout value is 1 second, as stated in jdk.naming.dns
+           module docs. */
+        long timeout = 1_000;
+        var envTimeout = env().get("com.sun.jndi.dns.timeout.initial");
+        if (envTimeout != null)
+            timeout = Long.parseLong(String.valueOf(envTimeout));
+
         setContext(new InitialDirContext(env()));
         
+        long startNanos = System.nanoTime();
+
         /* perform query */
         var attrs = context().getAttributes("host1");
+
+        long elapsed = NANOSECONDS.toMillis(System.nanoTime() - startNanos);
+        if (elapsed < timeout || elapsed > timeout + TOLERANCE) {
+            throw new RuntimeException("Query took " + elapsed + " ms. "
+                + ". Timeout value is " + timeout);
+        }
+
         DNSTestUtils.debug(attrs);
 
         /* Note that the returned attributes are truncated and the response
         is not valid. */
         var txtAttr = attrs.get("TXT");
< prev index next >