ARTEMIS-1971 make LDAP pooling test more robust

This commit is contained in:
Justin Bertram 2018-07-16 14:34:44 -05:00 committed by Clebert Suconic
parent 2748ef0253
commit fbdd6fe0ad
1 changed files with 45 additions and 69 deletions

View File

@ -115,7 +115,7 @@ public class LDAPLoginModuleTest extends AbstractLdapTestUnit {
} }
@Test @Test
public void testLogin() throws LoginException { public void testLogin() throws Exception {
logger.info("num session: " + ldapServer.getLdapSessionManager().getSessions().length); logger.info("num session: " + ldapServer.getLdapSessionManager().getSessions().length);
LoginContext context = new LoginContext("LDAPLogin", new CallbackHandler() { LoginContext context = new LoginContext("LDAPLogin", new CallbackHandler() {
@ -135,15 +135,12 @@ public class LDAPLoginModuleTest extends AbstractLdapTestUnit {
context.login(); context.login();
context.logout(); context.logout();
assertTrue("no sessions after logout", waitForSessions(0)); assertTrue("sessions still active after logout", waitFor(() -> ldapServer.getLdapSessionManager().getSessions().length == 0));
} }
@Test @Test
public void testLoginPooled() throws LoginException { public void testLoginPooled() throws Exception {
CallbackHandler callbackHandler = callbacks -> {
LoginContext context = new LoginContext("LDAPLoginPooled", new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) { for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) { if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName("first"); ((NameCallback) callbacks[i]).setName("first");
@ -153,82 +150,61 @@ public class LDAPLoginModuleTest extends AbstractLdapTestUnit {
throw new UnsupportedCallbackException(callbacks[i]); throw new UnsupportedCallbackException(callbacks[i]);
} }
} }
} };
});
LoginContext context = new LoginContext("LDAPLoginPooled", callbackHandler);
context.login(); context.login();
context.logout(); context.logout();
// again // again
context.login(); context.login();
context.logout(); context.logout();
// new context // new context
context = new LoginContext("LDAPLoginPooled", new CallbackHandler() { context = new LoginContext("LDAPLoginPooled", callbackHandler);
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName("first");
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword("secret".toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
});
context.login(); context.login();
context.logout(); context.logout();
Executor pool = Executors.newCachedThreadPool(); Executor pool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 20; i++) {
((ExecutorService) pool).execute(new Runnable() { pool.execute(() -> {
@Override
public void run() {
try { try {
LoginContext context = new LoginContext("LDAPLoginPooled", new CallbackHandler() { LoginContext context1 = new LoginContext("LDAPLoginPooled", callbackHandler);
@Override context1.login();
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { context1.logout();
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName("first");
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword("secret".toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
});
context.login();
context.logout();
} catch (Exception ignored) { } catch (Exception ignored) {
} }
}
}); });
} }
assertTrue("no sessions after logout", waitForSessions(10));
/*
* The number of sessions here is variable due to the pool used to create the LoginContext objects and the pooling
* for the LDAP connections (which are managed by the JVM implementation). We really just need to confirm that
* there are still connections to the LDAP server open even after all the LoginContext objects are closed as that
* will indicate the LDAP connection pooling is working.
*/
assertTrue("not enough active sessions after logout", waitFor(() -> ldapServer.getLdapSessionManager().getSessions().length >= 5));
((ExecutorService) pool).shutdown();
((ExecutorService) pool).awaitTermination(2, TimeUnit.SECONDS);
} }
private boolean waitForSessions(int expected) { public interface Condition {
boolean isSatisfied() throws Exception;
}
private boolean waitFor(final Condition condition) throws Exception {
final long expiry = System.currentTimeMillis() + 5000; final long expiry = System.currentTimeMillis() + 5000;
int numSession = ldapServer.getLdapSessionManager().getSessions().length; boolean conditionSatisified = condition.isSatisfied();
while (numSession != expected && System.currentTimeMillis() < expiry) { while (!conditionSatisified && System.currentTimeMillis() < expiry) {
try {
TimeUnit.MILLISECONDS.sleep(100); TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException ok) { conditionSatisified = condition.isSatisfied();
break;
} }
numSession = ldapServer.getLdapSessionManager().getSessions().length; return conditionSatisified;
logger.info("num session " + numSession);
}
return numSession == expected;
} }
@Test @Test
public void testUnauthenticated() throws LoginException { public void testUnauthenticated() throws Exception {
LoginContext context = new LoginContext("UnAuthenticatedLDAPLogin", new CallbackHandler() { LoginContext context = new LoginContext("UnAuthenticatedLDAPLogin", new CallbackHandler() {
@Override @Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
@ -250,7 +226,7 @@ public class LDAPLoginModuleTest extends AbstractLdapTestUnit {
return; return;
} }
fail("Should have failed authenticating"); fail("Should have failed authenticating");
assertTrue("no sessions after logout", waitForSessions(0)); assertTrue("sessions still active after logout", waitFor(() -> ldapServer.getLdapSessionManager().getSessions().length == 0));
} }
@Test @Test