From db780ed6c78ed810085dcfba92bb4c8a09a4ec17 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 dd9fa5af33..fc4b7d41e1 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.FluentWait; 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 final class WaitUtils { 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.getQueue().isEmpty() || 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 8fb8a9b484..63f7078c69 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 abstract class AbstractAdvancedBrokerTest extends AbstractBrokerTest { 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 ff18fdce05..1cac029af1 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 class BruteForceTest extends AbstractTestRealmKeycloakTest { 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 class BruteForceTest extends AbstractTestRealmKeycloakTest { 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();