8047062: Improve diagnostic output in com/sun/jndi/ldap/LdapTimeoutTest.java
authorprappo
Tue, 17 Jun 2014 13:57:01 +0100
changeset 25406 4814eec6a323
parent 25405 aa7f539c3569
child 25407 63b91a5b8d1f
8047062: Improve diagnostic output in com/sun/jndi/ldap/LdapTimeoutTest.java Reviewed-by: duke
jdk/src/share/classes/com/sun/jndi/ldap/Connection.java
jdk/test/com/sun/jndi/ldap/LdapTimeoutTest.java
--- a/jdk/src/share/classes/com/sun/jndi/ldap/Connection.java	Wed Jul 09 21:26:11 2014 -0700
+++ b/jdk/src/share/classes/com/sun/jndi/ldap/Connection.java	Tue Jun 17 13:57:01 2014 +0100
@@ -111,6 +111,7 @@
 
     private static final boolean debug = false;
     private static final int dump = 0; // > 0 r, > 1 rw
+    public static final long DEFAULT_READ_TIMEOUT_MILLIS = 15 * 1000; // 15 second timeout;
 
 
     final private Thread worker;    // Initialized in constructor
@@ -460,7 +461,7 @@
                             // available
                             ldr.wait(readTimeout);
                         } else {
-                            ldr.wait(15 * 1000); // 15 second timeout
+                            ldr.wait(DEFAULT_READ_TIMEOUT_MILLIS);
                         }
                         waited = true;
                     } else {
--- a/jdk/test/com/sun/jndi/ldap/LdapTimeoutTest.java	Wed Jul 09 21:26:11 2014 -0700
+++ b/jdk/test/com/sun/jndi/ldap/LdapTimeoutTest.java	Tue Jun 17 13:57:01 2014 +0100
@@ -27,6 +27,8 @@
  * @summary Timeout tests for ldap
  */
 
+import com.sun.jndi.ldap.Connection;
+
 import java.net.Socket;
 import java.net.ServerSocket;
 import java.net.SocketTimeoutException;
@@ -38,7 +40,9 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 public class LdapTimeoutTest {
     private static final ScheduledExecutorService pool =
@@ -85,7 +89,7 @@
     void ldapReadTimeoutTest(Hashtable env, boolean ssl) {
         InitialContext ctx = null;
         if (ssl) env.put(Context.SECURITY_PROTOCOL, "ssl");
-        ScheduledFuture killer = killSwitch(5000);
+        ScheduledFuture killer = killSwitch(5_000);
         long start = System.nanoTime();
         try {
             ctx = new InitialDirContext(env);
@@ -113,7 +117,7 @@
 
     void simpleAuthConnectTest(Hashtable env) {
         InitialContext ctx = null;
-        ScheduledFuture killer = killSwitch(5000);
+        ScheduledFuture killer = killSwitch(5_000);
         long start = System.nanoTime();
         try {
             ctx = new InitialDirContext(env);
@@ -123,7 +127,7 @@
         } catch (NamingException e) {
             long end = System.nanoTime();
             if (e.getCause() instanceof SocketTimeoutException) {
-                if (TimeUnit.NANOSECONDS.toMillis(end - start) < 2900) {
+                if (NANOSECONDS.toMillis(end - start) < 2_900) {
                     pass();
                 } else {
                     System.err.println("Fail: Waited too long");
@@ -142,7 +146,7 @@
 
     void deadServerNoTimeout(Hashtable env) {
         InitialContext ctx = null;
-        ScheduledFuture killer = killSwitch(30000);
+        ScheduledFuture killer = killSwitch(30_000);
         long start = System.nanoTime();
         try {
             ctx = new InitialDirContext(env);
@@ -154,9 +158,13 @@
             fail();
         } catch (NamingException e) {
             long end = System.nanoTime();
-            if (TimeUnit.NANOSECONDS.toMillis(end - start) < 14000) {
-                System.err.println("fail: timeout should be at least 15 seconds, actual time: "
-                                   + TimeUnit.NANOSECONDS.toMillis(end - start));
+            long elapsed = NANOSECONDS.toMillis(end - start);
+            if (elapsed < Connection.DEFAULT_READ_TIMEOUT_MILLIS) {
+                System.err.printf(
+                        "fail: timeout should be at least %s ms, actual time is %s ms",
+                        Connection.DEFAULT_READ_TIMEOUT_MILLIS,
+                        elapsed);
+                e.printStackTrace();
                 fail();
             } else {
                 pass();
@@ -184,7 +192,7 @@
                 System.exit(0);
                 return null;
             }
-        }, ms, TimeUnit.MILLISECONDS);
+        }, ms, MILLISECONDS);
     }
 
     static class Server extends Thread {