Search in sources :

Example 1 with UBIDLdapContext

use of com.zimbra.cs.ldap.unboundid.UBIDLdapContext in project zm-mailbox by Zimbra.

the class TestLdapConnection method backgroundHealthCheck.

@Test
public void backgroundHealthCheck() throws Exception {
    SKIP_FOR_INMEM_LDAP_SERVER(SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
    // 5 secs
    final long BACKGROUND_HEALTH_CHECK_INTERVAL = 5000;
    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, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_enabled, "true");
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_interval_millis, Long.valueOf(BACKGROUND_HEALTH_CHECK_INTERVAL).toString());
    Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
    final int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
    final int NUM_CONNS = 10;
    ExternalLdapConfig ldapConfig = new ExternalLdapConfig(LDAP_URL_BACKGROUND, 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();
    // wait for the health check interval to trigger health check
    long waitFor = BACKGROUND_HEALTH_CHECK_INTERVAL + 1000;
    System.out.println("Waiting for " + waitFor + " msecs");
    Thread.sleep(waitFor);
    System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
    assertEquals(0, 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) HashMap(java.util.HashMap) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Test(org.junit.Test)

Example 2 with UBIDLdapContext

use of com.zimbra.cs.ldap.unboundid.UBIDLdapContext in project zm-mailbox by Zimbra.

the class TestLdapConnection method populateConnPool.

private LDAPConnectionPool populateConnPool(ExternalLdapConfig ldapConfig, int numConns) throws Exception {
    final int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
    assertTrue(numConns < MAX_POOL_SIZE);
    LDAPConnectionPool connPool = null;
    List<UBIDLdapContext> zlcs = Lists.newArrayList();
    for (int i = 0; i < numConns; i++) {
        UBIDLdapContext zlc = getContext(ldapConfig);
        zlcs.add(zlc);
        if (connPool == null) {
            connPool = zlc.getConnectionPool();
        // System.out.println("backgroundHealthCheck pool is " + connPool.getConnectionPoolName());
        } else {
            // verify all zlcs use the same conn pool
            assertTrue(connPool == zlc.getConnectionPool());
        }
    }
    assertEquals(MAX_POOL_SIZE, connPool.getMaximumAvailableConnections());
    // number of connections that are currently available for use in this connection pool
    assertEquals(0, connPool.getCurrentAvailableConnections());
    for (int i = 0; i < numConns; i++) {
        UBIDLdapContext zlc = zlcs.get(i);
        closeContext(zlc);
    }
    // There should be NUM_CONNS conns in the pool
    assertEquals(numConns, connPool.getCurrentAvailableConnections());
    return connPool;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext)

Example 3 with UBIDLdapContext

use of com.zimbra.cs.ldap.unboundid.UBIDLdapContext in project zm-mailbox by Zimbra.

the class TestLdapConnectivity method testConnectivity.

/*
     * Important: Each invocation of testConnectivity must be run in its own VM
     */
public void testConnectivity(ConnectionConfig connConfig) throws Exception {
    connConfig.setLocalConfig();
    try {
        LdapClient.initialize();
        int expectedPort;
        if (connConfig == ConnectionConfig.LDAPI) {
            // LdapServerPool.DUMMY_LDAPI_PORT
            expectedPort = 1;
        } else if (connConfig == ConnectionConfig.LDAP || connConfig == ConnectionConfig.STARTTLS_T_UNTRUSTED_T_MISMATCHED || connConfig == ConnectionConfig.STARTTLS_T_UNTRUSTED_F_MISMATCHED || connConfig == ConnectionConfig.STARTTLS_F_UNTRUSTED_T_MISMATCHED || connConfig == ConnectionConfig.STARTTLS_F_UNTRUSTED_F_MISMATCHED) {
            expectedPort = 389;
        } else {
            expectedPort = 636;
        }
        UBIDLdapContext zlc1 = getContext();
        assertEquals(expectedPort, zlc1.getNative().getConnectedPort());
        ZAttributes attrs = zlc1.getAttributes("cn=zimbra", null);
        assertEquals("Zimbra Systems Application Data", attrs.getAttrString("description"));
        UBIDLdapContext zlc2 = getContext();
        assertEquals(expectedPort, zlc2.getNative().getConnectedPort());
        closeContext(zlc1);
        closeContext(zlc2);
    } finally {
        // so next test can re-initialized UBIDLdapContext and run
        LdapClient.shutdown();
    }
}
Also used : ZAttributes(com.zimbra.cs.ldap.ZAttributes) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext)

Example 4 with UBIDLdapContext

use of com.zimbra.cs.ldap.unboundid.UBIDLdapContext in project zm-mailbox by Zimbra.

the class TestLdapConnection method onCheckoutHealthCheck.

@Test
public void onCheckoutHealthCheck() throws Exception {
    SKIP_FOR_INMEM_LDAP_SERVER(SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
    Map<KnownKey, String> lcKeysToModify = new HashMap<KnownKey, String>();
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "true");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_after_exception_enabled, "false");
    // 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_ON_CHECKOUT, 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 to trigger health check
    boolean caughtException = false;
    try {
        UBIDLdapContext zlc = getContext(ldapConfig);
    } catch (ServiceException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
    System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
    assertEquals(0, 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) Test(org.junit.Test)

Example 5 with UBIDLdapContext

use of com.zimbra.cs.ldap.unboundid.UBIDLdapContext 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)

Aggregations

UBIDLdapContext (com.zimbra.cs.ldap.unboundid.UBIDLdapContext)6 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)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 ServiceException (com.zimbra.common.service.ServiceException)2 ZAttributes (com.zimbra.cs.ldap.ZAttributes)1 Ignore (org.junit.Ignore)1