From 67b20dfd9b7c63f96bbd81a6ce8562f663fde56c Mon Sep 17 00:00:00 2001 From: Martin Kanis Date: Fri, 14 Jul 2023 14:38:52 +0200 Subject: [PATCH] Introduce delay in SessionTimeoutsTest to allow xsite replication to finish Fixes #20983 --- .../model/session/SessionTimeoutsTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/SessionTimeoutsTest.java b/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/SessionTimeoutsTest.java index 12c97d5d23..60fac5d669 100644 --- a/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/SessionTimeoutsTest.java +++ b/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/SessionTimeoutsTest.java @@ -22,6 +22,7 @@ import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import org.keycloak.common.util.Time; +import org.keycloak.connections.infinispan.InfinispanConnectionProvider; import org.keycloak.models.AuthenticatedClientSessionModel; import org.keycloak.models.ClientModel; import org.keycloak.models.Constants; @@ -34,6 +35,7 @@ import org.keycloak.models.UserProvider; import org.keycloak.models.UserSessionProvider; import org.keycloak.protocol.oidc.OIDCConfigAttributes; import org.keycloak.protocol.oidc.OIDCLoginProtocol; +import org.keycloak.testsuite.model.HotRodServerRule; import org.keycloak.testsuite.model.KeycloakModelTest; import org.keycloak.testsuite.model.RequireProvider; import org.keycloak.testsuite.model.infinispan.InfinispanTestUtil; @@ -255,6 +257,8 @@ public class SessionTimeoutsTest extends KeycloakModelTest { return new String[]{userSession.getId(), clientSession.getId()}; }); + allowXSiteReplication(offline); + int offset = 0; for (int i = 0; i < refreshTimes; i++) { offset += 1500; @@ -357,4 +361,21 @@ public class SessionTimeoutsTest extends KeycloakModelTest { public void testOnlineUserClientIdleTimeoutSmallerThanSessionOneRefresh() { testUserClientIdleTimeoutSmallerThanSession(1, false, false); } + + /** + * This method introduces a delay to allow replication of clientSession cache on site 1 and site 2. + * Without the delay these test fails from time to time. This has no effect when tests run without cross-dc + * @param offline boolean Indicates where we work with offline sessions + */ + private void allowXSiteReplication(boolean offline) { + HotRodServerRule hotRodServer = getParameters(HotRodServerRule.class).findFirst().orElse(null); + if (hotRodServer != null) { + String cacheName = offline ? InfinispanConnectionProvider.OFFLINE_CLIENT_SESSION_CACHE_NAME : InfinispanConnectionProvider.CLIENT_SESSION_CACHE_NAME; + while (hotRodServer.getHotRodCacheManager().getCache(cacheName).size() != hotRodServer.getHotRodCacheManager2().getCache(cacheName).size()) { + try { + Thread.sleep(5); + } catch (InterruptedException e) {} + } + } + } }