test/com/sun/jndi/ldap/LdapTimeoutTest.java

Print this page

        

*** 62,76 **** env.put(Context.SECURITY_AUTHENTICATION,"simple"); env.put(Context.SECURITY_PRINCIPAL, "user"); env.put(Context.SECURITY_CREDENTIALS, "password"); - env.put("com.sun.jndi.ldap.connect.timeout", "10"); - env.put("com.sun.jndi.ldap.read.timeout", "3000"); - InitialContext ctx = null; try { new LdapTimeoutTest().ldapReadTimeoutTest(env, false); new LdapTimeoutTest().ldapReadTimeoutTest(env, true); new LdapTimeoutTest().simpleAuthConnectTest(env); } finally { s.interrupt(); --- 62,77 ---- env.put(Context.SECURITY_AUTHENTICATION,"simple"); env.put(Context.SECURITY_PRINCIPAL, "user"); env.put(Context.SECURITY_CREDENTIALS, "password"); InitialContext ctx = null; try { + new LdapTimeoutTest().deadServerNoTimeout(env); + + env.put("com.sun.jndi.ldap.connect.timeout", "10"); + env.put("com.sun.jndi.ldap.read.timeout", "3000"); new LdapTimeoutTest().ldapReadTimeoutTest(env, false); new LdapTimeoutTest().ldapReadTimeoutTest(env, true); new LdapTimeoutTest().simpleAuthConnectTest(env); } finally { s.interrupt();
*** 82,92 **** } void ldapReadTimeoutTest(Hashtable env, boolean ssl) { InitialContext ctx = null; if (ssl) env.put(Context.SECURITY_PROTOCOL, "ssl"); ! ScheduledFuture killer = killSwitch(); long start = System.nanoTime(); try { ctx = new InitialDirContext(env); SearchControls scl = new SearchControls(); scl.setSearchScope(SearchControls.SUBTREE_SCOPE); --- 83,93 ---- } void ldapReadTimeoutTest(Hashtable env, boolean ssl) { InitialContext ctx = null; if (ssl) env.put(Context.SECURITY_PROTOCOL, "ssl"); ! ScheduledFuture killer = killSwitch(5000); long start = System.nanoTime(); try { ctx = new InitialDirContext(env); SearchControls scl = new SearchControls(); scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
*** 110,120 **** } } void simpleAuthConnectTest(Hashtable env) { InitialContext ctx = null; ! ScheduledFuture killer = killSwitch(); long start = System.nanoTime(); try { ctx = new InitialDirContext(env); // shouldn't reach here System.err.println("Fail: InitialDirContext succeeded"); --- 111,121 ---- } } void simpleAuthConnectTest(Hashtable env) { InitialContext ctx = null; ! ScheduledFuture killer = killSwitch(5000); long start = System.nanoTime(); try { ctx = new InitialDirContext(env); // shouldn't reach here System.err.println("Fail: InitialDirContext succeeded");
*** 137,146 **** --- 138,173 ---- } finally { if (!shutItDown(killer, ctx)) fail(); } } + void deadServerNoTimeout(Hashtable env) { + InitialContext ctx = null; + ScheduledFuture killer = killSwitch(30000); + long start = System.nanoTime(); + try { + ctx = new InitialDirContext(env); + SearchControls scl = new SearchControls(); + scl.setSearchScope(SearchControls.SUBTREE_SCOPE); + NamingEnumeration<SearchResult> answer = ((InitialDirContext)ctx) + .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) < 15000) { + System.err.println("fail: timeout should be at least 15 seconds, actual time: " + + TimeUnit.NANOSECONDS.toMillis(end - start)); + fail(); + } else { + pass(); + } + } finally { + if (!shutItDown(killer, ctx)) fail(); + } + } + boolean shutItDown(ScheduledFuture killer, InitialContext ctx) { killer.cancel(true); try { if (ctx != null) ctx.close(); return true;
*** 147,165 **** } catch (NamingException ex) { return false; } } ! ScheduledFuture killSwitch() { final Thread current = Thread.currentThread(); return LdapTimeoutTest.pool.schedule(new Callable<Void>() { public Void call() throws Exception { System.err.println("Fail: killSwitch()"); ! current.interrupt(); return null; } ! }, 5000, TimeUnit.MILLISECONDS); } static class Server extends Thread { final ServerSocket serverSock; --- 174,192 ---- } catch (NamingException ex) { return false; } } ! ScheduledFuture killSwitch(int ms) { final Thread current = Thread.currentThread(); return LdapTimeoutTest.pool.schedule(new Callable<Void>() { public Void call() throws Exception { System.err.println("Fail: killSwitch()"); ! System.exit(0); return null; } ! }, ms, TimeUnit.MILLISECONDS); } static class Server extends Thread { final ServerSocket serverSock;