Skip to content

Commit

Permalink
Fix flaky UserSessionPersisterProviderTest
Browse files Browse the repository at this point in the history
Closes keycloak#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>
  • Loading branch information
mhajas and ahus1 authored Sep 20, 2024
1 parent 0f53bf9 commit d065be3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.keycloak.models.session.PersistentUserSessionAdapter;
import org.keycloak.models.session.PersistentUserSessionModel;
import org.keycloak.models.session.UserSessionPersisterProvider;
import org.keycloak.models.utils.SessionExpirationUtils;
import org.keycloak.models.utils.SessionTimeoutHelper;
import org.keycloak.storage.StorageId;

Expand Down Expand Up @@ -269,9 +270,9 @@ public void removeExpired(RealmModel realm) {

private int calculateOldestSessionTime(RealmModel realm, boolean offline) {
if (offline) {
return Time.currentTime() - realm.getOfflineSessionIdleTimeout();
return Time.currentTime() - SessionExpirationUtils.getOfflineSessionIdleTimeout(realm);
} else {
return Time.currentTime() - Math.max(realm.getSsoSessionIdleTimeout(), realm.getSsoSessionIdleTimeoutRememberMe());
return Time.currentTime() - Math.max(SessionExpirationUtils.getSsoSessionIdleTimeout(realm), realm.getSsoSessionIdleTimeoutRememberMe());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ private static int getOfflineSessionMaxLifespan(RealmModel realm) {
return lifespan;
}

private static int getSsoSessionIdleTimeout(RealmModel realm) {
public static int getSsoSessionIdleTimeout(RealmModel realm) {
int idle = realm.getSsoSessionIdleTimeout();
if (idle <= 0) {
idle = Constants.DEFAULT_SESSION_IDLE_TIMEOUT;
}
return idle;
}

private static int getOfflineSessionIdleTimeout(RealmModel realm) {
public static int getOfflineSessionIdleTimeout(RealmModel realm) {
int idle = realm.getOfflineSessionIdleTimeout();
if (idle <= 0) {
idle = Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import org.keycloak.connections.jpa.updater.liquibase.conn.LiquibaseConnectionSpi;
import org.keycloak.connections.jpa.updater.liquibase.lock.LiquibaseDBLockProviderFactory;
import org.keycloak.events.jpa.JpaEventStoreProviderFactory;
import org.keycloak.infinispan.util.InfinispanUtils;
import org.keycloak.migration.MigrationProviderFactory;
import org.keycloak.migration.MigrationSpi;
import org.keycloak.models.IdentityProviderStorageSpi;
import org.keycloak.models.UserSessionSpi;
import org.keycloak.models.dblock.DBLockSpi;
import org.keycloak.models.jpa.JpaClientProviderFactory;
import org.keycloak.models.jpa.JpaClientScopeProviderFactory;
Expand Down Expand Up @@ -138,5 +140,13 @@ public static void updateConfigForJpa(Config cf) {
.spi("deploymentState").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");
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import org.keycloak.models.Constants;
Expand Down Expand Up @@ -83,6 +84,8 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
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.setOfflineSessionMaxLifespan(Constants.DEFAULT_OFFLINE_SESSION_MAX_LIFESPAN);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
Expand Down Expand Up @@ -281,6 +284,7 @@ public void testOnRealmRemoved() {
RealmModel fooRealm = session.realms().getRealmByName("foo");
session.getContext().setRealm(fooRealm);
UserSessionModel userSession = session.sessions().getUserSession(fooRealm, userSessionID.get());
assertNotNull(userSession);
persistUserSession(session, userSession, true);
});

Expand Down

0 comments on commit d065be3

Please sign in to comment.