From 1be186ea18101fa4f61277e64a93ef2f983f8622 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Wed, 30 Oct 2024 09:51:31 -0300 Subject: [PATCH] Trying to make sure there is no active tasks and introduce a timeout Closes #34432 Signed-off-by: Pedro Igor --- .../keycloak/testsuite/util/WaitUtils.java | 39 ++++++++++--------- .../broker/AbstractAdvancedBrokerTest.java | 3 +- .../testsuite/forms/BruteForceTest.java | 6 +-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/WaitUtils.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/WaitUtils.java index dd9fa5af33d3..fb18e4dbf66b 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/WaitUtils.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/WaitUtils.java @@ -29,13 +29,16 @@ import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import static org.jboss.arquillian.graphene.Graphene.waitGui; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import static org.keycloak.testsuite.util.DroneUtils.getCurrentDriver; import static org.openqa.selenium.support.ui.ExpectedConditions.javaScriptThrowsNoExceptions; import static org.openqa.selenium.support.ui.ExpectedConditions.not; @@ -162,28 +165,28 @@ public static void waitForModalFadeOut() { waitUntilElementIsNotPresent(By.className("modal-backdrop")); } - public static long getNumExecutors(KeycloakTestingClient testingClient) { - String numExecutors = testingClient.server().fetchString(session -> { - ExecutorsProvider provider = session.getProvider(ExecutorsProvider.class); - ExecutorService executor = provider.getExecutor("bruteforce"); - ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; - return threadPoolExecutor.getCompletedTaskCount(); - }); - return Long.valueOf(numExecutors); - } - - public static void waitForExecutors(KeycloakTestingClient testingClient, long numExecutors) { + public static void waitForBruteForceExecutors(KeycloakTestingClient testingClient) { testingClient.server().run(session -> { ExecutorsProvider provider = session.getProvider(ExecutorsProvider.class); ExecutorService executor = provider.getExecutor("bruteforce"); ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; - do { - try { - Thread.sleep(1000); - } catch (Exception e) { - } - } while (!threadPoolExecutor.getQueue().isEmpty()); - assertEquals(numExecutors, threadPoolExecutor.getCompletedTaskCount()); + try { + CompletableFuture.runAsync(() -> { + do { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } while (threadPoolExecutor.getActiveCount() > 0); + }).get(30, TimeUnit.SECONDS); + } catch (java.util.concurrent.TimeoutException te) { + fail("Timeout while waiting for brute force executors!"); + } catch (Exception e) { + e.printStackTrace(); + fail("Unexpected error while waiting for brute force executors!"); + } + assertEquals(0, threadPoolExecutor.getActiveCount()); }); } } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/AbstractAdvancedBrokerTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/AbstractAdvancedBrokerTest.java index 8fb8a9b4843a..63f7078c6913 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/AbstractAdvancedBrokerTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/AbstractAdvancedBrokerTest.java @@ -567,14 +567,13 @@ public void testPostBrokerLoginFlowWithOTP_bruteForceEnabled() { Map bruteForceStatus = realm.attackDetection().bruteForceUserStatus(user.getId()); assertFalse("User should not be disabled by brute force.", (boolean) bruteForceStatus.get("disabled")); - long numExecutors = WaitUtils.getNumExecutors(testingClient); // Login for 2 times with incorrect TOTP. This should temporarily disable the user loginTotpPage.login("bad-totp"); Assert.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError()); loginTotpPage.login("bad-totp"); Assert.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError()); - WaitUtils.waitForExecutors(testingClient, numExecutors+2); + WaitUtils.waitForBruteForceExecutors(testingClient); bruteForceStatus = realm.attackDetection().bruteForceUserStatus(user.getId()); assertTrue("User should be disabled by brute force.", (boolean) bruteForceStatus.get("disabled")); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/BruteForceTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/BruteForceTest.java index ff18fdce05ef..1cac029af1f1 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/BruteForceTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/BruteForceTest.java @@ -416,11 +416,10 @@ public void testFailureResetForTemporaryLockout() throws Exception { try { realm.setMaxDeltaTimeSeconds(5); testRealm().update(realm); - long numExecutors = WaitUtils.getNumExecutors(testingClient); loginInvalidPassword(); //Wait for brute force executor to process the login and then wait for delta time - WaitUtils.waitForExecutors(testingClient, numExecutors + 1); + WaitUtils.waitForBruteForceExecutors(testingClient); testingClient.testing().setTimeOffset(Collections.singletonMap("offset", String.valueOf(5))); loginInvalidPassword(); @@ -438,11 +437,10 @@ public void testNoFailureResetForPermanentLockout() throws Exception { realm.setMaxDeltaTimeSeconds(5); realm.setPermanentLockout(true); testRealm().update(realm); - long numExecutors = WaitUtils.getNumExecutors(testingClient); loginInvalidPassword(); //Wait for brute force executor to process the login and then wait for delta time - WaitUtils.waitForExecutors(testingClient, numExecutors + 1); + WaitUtils.waitForBruteForceExecutors(testingClient); testingClient.testing().setTimeOffset(Collections.singletonMap("offset", String.valueOf(5))); loginInvalidPassword();