diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java index 14a89f47628..8058a7aa188 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; +import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.SecurityIntegTestCase; @@ -63,9 +64,9 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import java.util.stream.IntStream; import java.util.stream.Stream; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; @@ -82,6 +83,7 @@ import static org.hamcrest.Matchers.nullValue; public class ApiKeyIntegTests extends SecurityIntegTestCase { private static final long DELETE_INTERVAL_MILLIS = 100L; + private static final int CRYPTO_THREAD_POOL_QUEUE_SIZE = 10; @Override public Settings nodeSettings(int nodeOrdinal) { @@ -90,6 +92,7 @@ public class ApiKeyIntegTests extends SecurityIntegTestCase { .put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true) .put(ApiKeyService.DELETE_INTERVAL.getKey(), TimeValue.timeValueMillis(DELETE_INTERVAL_MILLIS)) .put(ApiKeyService.DELETE_TIMEOUT.getKey(), TimeValue.timeValueSeconds(5L)) + .put("xpack.security.crypto.thread_pool.queue_size", CRYPTO_THREAD_POOL_QUEUE_SIZE) .build(); } @@ -855,7 +858,7 @@ public class ApiKeyIntegTests extends SecurityIntegTestCase { assertApiKeyNotCreated(client,"key-5"); } - public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOException, InterruptedException { + public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOException, InterruptedException, ExecutionException { final String nodeName = randomFrom(internalCluster().getNodeNames()); final Settings settings = internalCluster().getInstance(Settings.class, nodeName); final ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, nodeName); @@ -882,7 +885,6 @@ public class ApiKeyIntegTests extends SecurityIntegTestCase { final int numberOfThreads = (allocatedProcessors + 1) / 2; final CountDownLatch blockingLatch = new CountDownLatch(1); final CountDownLatch readyLatch = new CountDownLatch(numberOfThreads); - for (int i = 0; i < numberOfThreads; i++) { executorService.submit(() -> { readyLatch.countDown(); @@ -894,8 +896,13 @@ public class ApiKeyIntegTests extends SecurityIntegTestCase { }); } // Fill the whole queue for the crypto thread pool - final int queueSize = 1000; - IntStream.range(0, queueSize).forEach(i -> executorService.submit(() -> {})); + Future lastTaskFuture = null; + try { + for (int i = 0; i < CRYPTO_THREAD_POOL_QUEUE_SIZE; i++) { + lastTaskFuture = executorService.submit(() -> { }); + } + } catch (EsRejectedExecutionException e) { + } readyLatch.await(); try (RestClient restClient = createRestClient(nodeInfos, null, "http")) { @@ -910,6 +917,9 @@ public class ApiKeyIntegTests extends SecurityIntegTestCase { assertThat(responseException.getResponse().getStatusLine().getStatusCode(), is(429)); } finally { blockingLatch.countDown(); + if (lastTaskFuture != null) { + lastTaskFuture.get(); + } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java index a3842988185..101d95f98f2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java @@ -105,7 +105,7 @@ public class ApiKeyServiceTests extends ESTestCase { threadPool = Mockito.spy( new TestThreadPool("api key service tests", new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000, - "xpack.security.authc.api_key.thread_pool", false)) + "xpack.security.crypto.thread_pool", false)) ); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java index cd9ece350f8..7c95359f2cf 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java @@ -211,7 +211,7 @@ public class AuthenticationServiceTests extends ESTestCase { new FixedExecutorBuilder(settings, THREAD_POOL_NAME, 1, 1000, "xpack.security.authc.token.thread_pool", false), new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000, - "xpack.security.authc.api_key.thread_pool", false) + "xpack.security.crypto.thread_pool", false) ); threadContext = threadPool.getThreadContext(); when(client.threadPool()).thenReturn(threadPool);