Search in sources :

Example 11 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project zm-mailbox by Zimbra.

the class TestLdapConnection method testConnPoolNumAvailConns.

@Test
public // @Ignore  // TODO: must be the first test to run
void testConnPoolNumAvailConns() throws Exception {
    int INIT_POOL_SIZE = LC.ldap_connect_pool_initsize.intValue();
    int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
    LDAPConnectionPool connPool = LdapConnectionPool.getConnPoolByName(LdapConnectionPool.CP_ZIMBRA_REPLICA);
    assertEquals(INIT_POOL_SIZE, connPool.getCurrentAvailableConnections());
    assertEquals(MAX_POOL_SIZE, connPool.getMaximumAvailableConnections());
    UBIDLdapContext zlc = getContext();
    String poolName = connPool.getConnectionPoolName();
    closeContext(zlc);
    assertEquals(LdapConnectionPool.CP_ZIMBRA_REPLICA, poolName);
    // should not change.
    for (int i = 0; i < 10; i++) {
        UBIDLdapContext conn = getContext();
        closeContext(conn);
        assertEquals(INIT_POOL_SIZE, connPool.getCurrentAvailableConnections());
    }
    int numOpen = 20;
    // make sure numOpen is a good number to test
    assertTrue(numOpen > INIT_POOL_SIZE);
    assertTrue(numOpen < MAX_POOL_SIZE);
    // get connections, not closing them, num available connections in the pool
    // should keep decreasing until there is no more.
    UBIDLdapContext[] conns = new UBIDLdapContext[numOpen];
    for (int i = 0; i < numOpen; i++) {
        conns[i] = getContext();
        int expected = Math.max(0, INIT_POOL_SIZE - (i + 1));
        assertEquals(expected, connPool.getCurrentAvailableConnections());
    }
    // should keep increasing.
    for (int i = 0; i < numOpen; i++) {
        closeContext(conns[i]);
        int expected = i + 1;
        assertEquals(expected, connPool.getCurrentAvailableConnections());
    }
// dumpConnPool(connPool);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Test(org.junit.Test)

Example 12 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project zm-mailbox by Zimbra.

the class TestLdapConnection method afterExceptionHealthCheck.

@Test
// after-exception health check is not supported.
@Ignore
public void afterExceptionHealthCheck() throws Exception {
    Map<KnownKey, String> lcKeysToModify = new HashMap<KnownKey, String>();
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_after_exception_enabled, "true");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_enabled, "false");
    Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
    final int NUM_CONNS = 10;
    ExternalLdapConfig ldapConfig = new ExternalLdapConfig(LDAP_URL_AFTER_EXCEPTION, START_TLS_ENABLED, null, BIND_DN, BIND_PASSWORD, null, null);
    LDAPConnectionPool connPool = populateConnPool(ldapConfig, NUM_CONNS);
    // stop ldap server here
    System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
    stopLdap();
    // try to get a connection from the pool
    // unlike on checkout health check, this will NOT trigger a health check
    // it will just return one connection from the pool
    UBIDLdapContext zlcTest = getContext(ldapConfig);
    // use the connection - now we should get an exception, and a health check
    // should be triggered.
    boolean caughtException = false;
    try {
        zlcTest.getAttributes(LdapConstants.DN_ROOT_DSE, null);
    } catch (ServiceException e) {
        caughtException = true;
    // e.printStackTrace();
    } finally {
    // if this is called, it somehow increments the CurrentAvailableConnections count
    // in the connection pool - it should not, because the connection is already defunced
    // (LDAPConnectionPool.releaseConnectionAfterException() was called).
    // 
    // The CurrentAvailableConnections count drop back to NUM_CONNS - 1 after one minute.
    // closeContext(zlcTest);
    }
    assertTrue(caughtException);
    System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
    int secs = 0;
    while (true) {
        Thread.sleep(1000);
        secs++;
        int junk = connPool.getCurrentAvailableConnections();
        System.out.println("After health check, availConns = " + junk + " " + secs);
        if (junk < NUM_CONNS) {
            break;
        }
    }
    // unlink on-checkout and beckground modes, the after-exception mode removes only
    // the bad connection.   To support this, we need to call
    // LDAPConnectionPool.releaseConnectionAfterException(LDAPConnection connection, LDAPException exception)
    // after an exception is caught.
    // For some reason this only work in Eclipse because of timing issue.
    assertEquals(NUM_CONNS - 1, connPool.getCurrentAvailableConnections());
    // put the config key back
    setLocalConfig(origLCKeyValues);
    startLdap();
    // get a connection now, should be successful
    UBIDLdapContext zlc = getContext(ldapConfig);
    closeContext(zlc);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) KnownKey(com.zimbra.common.localconfig.KnownKey) ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)12 LDAPException (com.unboundid.ldap.sdk.LDAPException)5 UBIDLdapContext (com.zimbra.cs.ldap.unboundid.UBIDLdapContext)5 Test (org.junit.Test)4 KnownKey (com.zimbra.common.localconfig.KnownKey)3 ExternalLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig)3 HashMap (java.util.HashMap)3 FailoverServerSet (com.unboundid.ldap.sdk.FailoverServerSet)2 ServiceException (com.zimbra.common.service.ServiceException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 BindRequest (com.unboundid.ldap.sdk.BindRequest)1 GetEntryLDAPConnectionPoolHealthCheck (com.unboundid.ldap.sdk.GetEntryLDAPConnectionPoolHealthCheck)1 PostConnectProcessor (com.unboundid.ldap.sdk.PostConnectProcessor)1 ServerSet (com.unboundid.ldap.sdk.ServerSet)1 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)1 StartTLSPostConnectProcessor (com.unboundid.ldap.sdk.StartTLSPostConnectProcessor)1 SSLContext (javax.net.ssl.SSLContext)1 Ignore (org.junit.Ignore)1