test/com/sun/jndi/ldap/LdapTimeoutTest.java
Print this page
rev 10092 : 8046768: com/sun/jndi/ldap/LdapTimeoutTest.java fails intermittently
Summary: Added diagnostic printStackTrace() for NamingException
Reviewed-by: duke
@@ -25,10 +25,12 @@
* @test
* @bug 7094377 8000487 6176036 7056489
* @summary Timeout tests for ldap
*/
+import com.sun.jndi.ldap.Connection;
+
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.io.*;
import javax.naming.*;
@@ -36,11 +38,13 @@
import java.util.Hashtable;
import java.util.concurrent.Callable;
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 =
Executors.newScheduledThreadPool(1);
static volatile int passed = 0, failed = 0;
@@ -83,11 +87,11 @@
}
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);
SearchControls scl = new SearchControls();
scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
@@ -111,21 +115,21 @@
}
}
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);
// shouldn't reach here
System.err.println("Fail: InitialDirContext succeeded");
fail();
} 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");
fail();
}
@@ -140,11 +144,11 @@
}
}
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);
SearchControls scl = new SearchControls();
scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
@@ -152,13 +156,17 @@
.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
// shouldn't reach here
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();
}
} finally {
@@ -182,11 +190,11 @@
public Void call() throws Exception {
System.err.println("Fail: killSwitch()");
System.exit(0);
return null;
}
- }, ms, TimeUnit.MILLISECONDS);
+ }, ms, MILLISECONDS);
}
static class Server extends Thread {
final ServerSocket serverSock;