From a12b384906b5e61d95d2c927e783adaf71079159 Mon Sep 17 00:00:00 2001 From: Tim Vernum Date: Tue, 6 Jun 2017 17:47:28 +1000 Subject: [PATCH] [TEST] Force LDAP connection to close at end of test (elastic/x-pack-elasticsearch#1620) This test would sometime leak threads. The "Timer thread for LDAPConnection" is created by the unboundid SDK - closing the connection should force the thread to terminate Original commit: elastic/x-pack-elasticsearch@bd58a17a59ebebe31bbc11af168dbcea6211806e --- .../SearchGroupsResolverInMemoryTests.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java index 01f73f08564..3e884132d61 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java @@ -13,37 +13,39 @@ import com.unboundid.ldap.sdk.LDAPConnectionOptions; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.LDAPURL; import com.unboundid.ldap.sdk.ResultCode; -import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.xpack.security.authc.ldap.support.LdapSearchScope; import org.elasticsearch.xpack.security.authc.ldap.support.LdapTestCase; import org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils; +import org.junit.After; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; -@AwaitsFix(bugUrl = "https://github.com/elastic/x-pack-elasticsearch/issues/971") public class SearchGroupsResolverInMemoryTests extends LdapTestCase { + private LDAPConnection connection; + + @After + public void closeConnection() { + if (connection != null) { + connection.close(); + } + } + /** * Tests that a client-side timeout in the asynchronous LDAP SDK is treated as a failure, rather * than simply returning no results. */ public void testSearchTimeoutIsFailure() throws Exception { - ldapServers[0].setProcessingDelayMillis(100); final LDAPConnectionOptions options = new LDAPConnectionOptions(); options.setConnectTimeoutMillis(500); options.setResponseTimeoutMillis(5); - - final LDAPURL ldapurl = new LDAPURL(ldapUrls()[0]); - final LDAPConnection connection = LdapUtils.privilegedConnect( - () -> new LDAPConnection(options, - ldapurl.getHost(), ldapurl.getPort()) - ); + connect(options); final Settings settings = Settings.builder() .put("group_search.base_dn", "ou=groups,o=sevenSeas") @@ -63,4 +65,13 @@ public class SearchGroupsResolverInMemoryTests extends LdapTestCase { assertThat(((LDAPException) cause).getResultCode(), is(ResultCode.TIMEOUT)); } + private void connect(LDAPConnectionOptions options) throws LDAPException { + if (connection != null) { + throw new IllegalStateException("Already connected (" + connection.getConnectionName() + ' ' + + connection.getConnectedAddress() + ')'); + } + final LDAPURL ldapurl = new LDAPURL(ldapUrls()[0]); + this.connection = LdapUtils.privilegedConnect(() -> new LDAPConnection(options, ldapurl.getHost(), ldapurl.getPort())); + } + } \ No newline at end of file