Improve the cleanup after a failed test to ensure retries work

Closes #30018

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2024-05-31 11:21:03 +02:00 committed by Alexander Schwartz
parent ad32f8bdbc
commit f6f3b385c5
2 changed files with 36 additions and 4 deletions

View file

@ -26,6 +26,7 @@ federation/sssd/ ci ci-sssd
quarkus/ ci-quarkus guides
model/ ci-store
testsuite/model/ ci-store
operator/ operator
docs/guides/ guides

View file

@ -61,6 +61,8 @@ public class SessionTimeoutsTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
super.createEnvironment(s);
RealmModel realm = createRealm(s, "test");
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
@ -78,8 +80,29 @@ public class SessionTimeoutsTest extends KeycloakModelTest {
UserModel user1 = s.users().getUserByUsername(realm, "user1");
s.sessions().removeUserSessions(realm);
s.sessions().getOfflineUserSessionsStream(realm, user1).forEach(us -> s.sessions().removeOfflineUserSession(realm, us));
s.realms().removeRealm(realmId);
// explicitly clear session caches, as removeUserSessions() contains asynchronous processing or might be incomplete due to a previous failure
clearSessionCaches(s);
super.cleanEnvironment(s);
}
private void clearSessionCaches(KeycloakSession s) {
InfinispanConnectionProvider provider = s.getProvider(InfinispanConnectionProvider.class);
if (provider != null) {
for (String cache : InfinispanConnectionProvider.DISTRIBUTED_REPLICATED_CACHE_NAMES) {
provider.getCache(cache).clear();
}
}
HotRodServerRule hotRodServer = getParameters(HotRodServerRule.class).findFirst().orElse(null);
if (hotRodServer != null) {
for (String cache : InfinispanConnectionProvider.DISTRIBUTED_REPLICATED_CACHE_NAMES) {
hotRodServer.getHotRodCacheManager().getCache(cache).clear();
hotRodServer.getHotRodCacheManager2().getCache(cache).clear();
}
}
}
protected static UserSessionModel createUserSession(KeycloakSession session, RealmModel realm, UserModel user, boolean offline) {
@ -345,12 +368,12 @@ public class SessionTimeoutsTest extends KeycloakModelTest {
testUserClientMaxLifespanSmallerThanSession(true, true);
}
@Test
@Test(timeout = 10 * 1000)
public void testOfflineUserClientIdleTimeoutSmallerThanSessionNoRefresh() {
testUserClientIdleTimeoutSmallerThanSession(0, true, false);
}
@Test
@Test(timeout = 10 * 1000)
public void testOfflineUserClientIdleTimeoutSmallerThanSessionOneRefresh() {
testUserClientIdleTimeoutSmallerThanSession(1, true, false);
}
@ -397,7 +420,15 @@ public class SessionTimeoutsTest extends KeycloakModelTest {
while (hotRodServer.getHotRodCacheManager().getCache(cacheName).size() != hotRodServer.getHotRodCacheManager2().getCache(cacheName).size()) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.errorf("Interrupted while waiting. Cache: %s, Cache sizes: %d vs %d",
cacheName,
hotRodServer.getHotRodCacheManager().getCache(cacheName).size(),
hotRodServer.getHotRodCacheManager2().getCache(cacheName).size()
);
throw new RuntimeException(e);
}
}
}
}