From 8f7d93939c4c067c32db6e0deadad1ce20e9ce7b Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 20 Jun 2024 18:01:04 +0200 Subject: [PATCH] Fix odic token retrieval under concurrency test flakiness improve test reliability et the expense of test time fix https://github.com/quarkusio/quarkus/issues/41298 --- .../io/quarkus/it/keycloak/OidcClientTest.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/integration-tests/oidc-client-wiremock/src/test/java/io/quarkus/it/keycloak/OidcClientTest.java b/integration-tests/oidc-client-wiremock/src/test/java/io/quarkus/it/keycloak/OidcClientTest.java index 15b12cd919e9b..b21fa0c95f83e 100644 --- a/integration-tests/oidc-client-wiremock/src/test/java/io/quarkus/it/keycloak/OidcClientTest.java +++ b/integration-tests/oidc-client-wiremock/src/test/java/io/quarkus/it/keycloak/OidcClientTest.java @@ -63,7 +63,7 @@ public void testEchoAndRefreshTokens() { .body(equalTo("access_token_1")); // Wait until the access_token_1 has expired - waitUntillAccessTokenHasExpired(); + waitUntillAccessTokenHasExpired(5000); // access_token_1 has expired, refresh_token_1 is assumed to be valid and used to acquire access_token_2 and refresh_token_2. // access_token_2 expires in 4 seconds, but refresh_token_2 - in 1 sec - it will expire by the time access_token_2 has expired @@ -74,7 +74,7 @@ public void testEchoAndRefreshTokens() { .body(equalTo("access_token_2")); // Wait until the access_token_2 has expired - waitUntillAccessTokenHasExpired(); + waitUntillAccessTokenHasExpired(5000); // Both access_token_2 and refresh_token_2 have now expired therefore a password grant request is repeated, // as opposed to using a refresh token grant. @@ -88,8 +88,8 @@ public void testEchoAndRefreshTokens() { checkLog(); } - private static void waitUntillAccessTokenHasExpired() { - long expiredTokenTime = System.currentTimeMillis() + 5000; + private static void waitUntillAccessTokenHasExpired(int expirationMs) { + long expiredTokenTime = System.currentTimeMillis() + expirationMs; await().atMost(10, TimeUnit.SECONDS) .pollInterval(Duration.ofSeconds(3)) .until(new Callable() { @@ -120,14 +120,16 @@ public void testEchoAndRefreshTokensWithConcurrency() { server.resetRequests(); // reset request counters - // Given : after 1s, the token expires + // Given : the token access_token_1 expires + waitUntillAccessTokenHasExpired(2000); + // When : 2 concurrents requests until the refresh was made (access_token_2 comes from the refresh) - await().atMost(2000, TimeUnit.MILLISECONDS).untilAsserted(() -> IntStream.range(0, 2).parallel().forEach(i -> { + IntStream.range(0, 2).parallel().forEach(i -> { RestAssured.when().get("/frontend/crashTest") .then() .statusCode(200) .body(equalTo("access_token_2")); - })); + }); // Then: only one token retrieval should be made server.verify(1, WireMock.postRequestedFor(urlEqualTo("/tokens-with-delay"))); }