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 d8ab9b7dcc0d3..bf5fe3e7f2ca4 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.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; @@ -66,9 +67,9 @@ 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.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; @@ -84,6 +85,7 @@ 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) { @@ -92,6 +94,7 @@ public Settings nodeSettings(int nodeOrdinal) { .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(); } @@ -833,7 +836,7 @@ public void testDerivedKeys() throws ExecutionException, InterruptedException { 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); @@ -859,7 +862,6 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc 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(); @@ -871,8 +873,13 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc }); } // 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")) { @@ -887,6 +894,9 @@ public void testAuthenticationReturns429WhenThreadPoolIsSaturated() throws IOExc 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 d405e1b3aa389..d1f2b1d6535f7 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 @@ -106,7 +106,7 @@ public void createThreadPool() { 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 fbe9413283f31..db58c8ea9a8b0 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 void init() throws Exception { 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);