Fix flaky UserSessionPersisterProviderTest

Closes #32892

Signed-off-by: Michal Hajas <mhajas@redhat.com>
Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
Co-authored-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Michal Hajas 2024-09-20 13:24:34 +02:00 committed by GitHub
parent 0f53bf9079
commit d065be362a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 4 deletions

View file

@ -32,6 +32,7 @@ import org.keycloak.models.session.PersistentClientSessionModel;
import org.keycloak.models.session.PersistentUserSessionAdapter; import org.keycloak.models.session.PersistentUserSessionAdapter;
import org.keycloak.models.session.PersistentUserSessionModel; import org.keycloak.models.session.PersistentUserSessionModel;
import org.keycloak.models.session.UserSessionPersisterProvider; import org.keycloak.models.session.UserSessionPersisterProvider;
import org.keycloak.models.utils.SessionExpirationUtils;
import org.keycloak.models.utils.SessionTimeoutHelper; import org.keycloak.models.utils.SessionTimeoutHelper;
import org.keycloak.storage.StorageId; import org.keycloak.storage.StorageId;
@ -269,9 +270,9 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv
private int calculateOldestSessionTime(RealmModel realm, boolean offline) { private int calculateOldestSessionTime(RealmModel realm, boolean offline) {
if (offline) { if (offline) {
return Time.currentTime() - realm.getOfflineSessionIdleTimeout(); return Time.currentTime() - SessionExpirationUtils.getOfflineSessionIdleTimeout(realm);
} else { } else {
return Time.currentTime() - Math.max(realm.getSsoSessionIdleTimeout(), realm.getSsoSessionIdleTimeoutRememberMe()); return Time.currentTime() - Math.max(SessionExpirationUtils.getSsoSessionIdleTimeout(realm), realm.getSsoSessionIdleTimeoutRememberMe());
} }
} }

View file

@ -185,7 +185,7 @@ public class SessionExpirationUtils {
return lifespan; return lifespan;
} }
private static int getSsoSessionIdleTimeout(RealmModel realm) { public static int getSsoSessionIdleTimeout(RealmModel realm) {
int idle = realm.getSsoSessionIdleTimeout(); int idle = realm.getSsoSessionIdleTimeout();
if (idle <= 0) { if (idle <= 0) {
idle = Constants.DEFAULT_SESSION_IDLE_TIMEOUT; idle = Constants.DEFAULT_SESSION_IDLE_TIMEOUT;
@ -193,7 +193,7 @@ public class SessionExpirationUtils {
return idle; return idle;
} }
private static int getOfflineSessionIdleTimeout(RealmModel realm) { public static int getOfflineSessionIdleTimeout(RealmModel realm) {
int idle = realm.getOfflineSessionIdleTimeout(); int idle = realm.getOfflineSessionIdleTimeout();
if (idle <= 0) { if (idle <= 0) {
idle = Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT; idle = Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT;

View file

@ -30,9 +30,11 @@ import org.keycloak.connections.jpa.updater.liquibase.conn.LiquibaseConnectionPr
import org.keycloak.connections.jpa.updater.liquibase.conn.LiquibaseConnectionSpi; import org.keycloak.connections.jpa.updater.liquibase.conn.LiquibaseConnectionSpi;
import org.keycloak.connections.jpa.updater.liquibase.lock.LiquibaseDBLockProviderFactory; import org.keycloak.connections.jpa.updater.liquibase.lock.LiquibaseDBLockProviderFactory;
import org.keycloak.events.jpa.JpaEventStoreProviderFactory; import org.keycloak.events.jpa.JpaEventStoreProviderFactory;
import org.keycloak.infinispan.util.InfinispanUtils;
import org.keycloak.migration.MigrationProviderFactory; import org.keycloak.migration.MigrationProviderFactory;
import org.keycloak.migration.MigrationSpi; import org.keycloak.migration.MigrationSpi;
import org.keycloak.models.IdentityProviderStorageSpi; import org.keycloak.models.IdentityProviderStorageSpi;
import org.keycloak.models.UserSessionSpi;
import org.keycloak.models.dblock.DBLockSpi; import org.keycloak.models.dblock.DBLockSpi;
import org.keycloak.models.jpa.JpaClientProviderFactory; import org.keycloak.models.jpa.JpaClientProviderFactory;
import org.keycloak.models.jpa.JpaClientScopeProviderFactory; import org.keycloak.models.jpa.JpaClientScopeProviderFactory;
@ -138,5 +140,13 @@ public class Jpa extends KeycloakModelParameters {
.spi("deploymentState").defaultProvider("jpa") .spi("deploymentState").defaultProvider("jpa")
.spi("dblock").defaultProvider("jpa") .spi("dblock").defaultProvider("jpa")
; ;
// Use this for running model tests with Postgres database
// cf.spi("connectionsJpa")
// .provider("default")
// .config("url", "jdbc:postgresql://localhost:5432/keycloakDB")
// .config("user", "keycloak")
// .config("password", "pass")
// .config("driver", "org.postgresql.Driver");
//
} }
} }

View file

@ -55,6 +55,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import org.keycloak.models.Constants; import org.keycloak.models.Constants;
@ -83,6 +84,8 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
public void createEnvironment(KeycloakSession s) { public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test"); RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm); s.getContext().setRealm(realm);
realm.setSsoSessionMaxLifespan(Constants.DEFAULT_SESSION_MAX_LIFESPAN);
realm.setSsoSessionIdleTimeout(Constants.DEFAULT_SESSION_IDLE_TIMEOUT);
realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT); realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT);
realm.setOfflineSessionMaxLifespan(Constants.DEFAULT_OFFLINE_SESSION_MAX_LIFESPAN); realm.setOfflineSessionMaxLifespan(Constants.DEFAULT_OFFLINE_SESSION_MAX_LIFESPAN);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName())); realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
@ -281,6 +284,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
RealmModel fooRealm = session.realms().getRealmByName("foo"); RealmModel fooRealm = session.realms().getRealmByName("foo");
session.getContext().setRealm(fooRealm); session.getContext().setRealm(fooRealm);
UserSessionModel userSession = session.sessions().getUserSession(fooRealm, userSessionID.get()); UserSessionModel userSession = session.sessions().getUserSession(fooRealm, userSessionID.get());
assertNotNull(userSession);
persistUserSession(session, userSession, true); persistUserSession(session, userSession, true);
}); });