KEYCLOAK-9988 Fix unstable UserSessionProviderOfflineTest.testExpired

This commit is contained in:
mposolda 2019-04-12 15:07:02 +02:00 committed by Marek Posolda
parent 4a227ae564
commit a8af51c7bb
2 changed files with 63 additions and 52 deletions

View file

@ -27,6 +27,9 @@ import org.keycloak.models.sessions.infinispan.entities.UserSessionEntity;
*/
public class PersisterLastSessionRefreshStoreFactory extends AbstractLastSessionRefreshStoreFactory {
// Name of periodic task to update DB with lastSessionRefreshes
public static final String DB_LSR_PERIODIC_TASK_NAME = "db-last-session-refresh";
public PersisterLastSessionRefreshStore createAndInit(KeycloakSession kcSession, boolean offline) {
return createAndInit(kcSession, DEFAULT_TIMER_INTERVAL_MS, DEFAULT_MAX_INTERVAL_BETWEEN_MESSAGES_SECONDS, DEFAULT_MAX_COUNT, offline);
}
@ -37,7 +40,7 @@ public class PersisterLastSessionRefreshStoreFactory extends AbstractLastSession
PersisterLastSessionRefreshStore store = createStoreInstance(maxIntervalBetweenMessagesSeconds, maxCount, offline);
// Setup periodic timer check
setupPeriodicTimer(kcSession, store, timerIntervalMs, "db-last-session-refresh");
setupPeriodicTimer(kcSession, store, timerIntervalMs, DB_LSR_PERIODIC_TASK_NAME);
return store;
}

View file

@ -28,6 +28,7 @@ import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.common.util.Time;
import org.keycloak.models.*;
import org.keycloak.models.session.UserSessionPersisterProvider;
import org.keycloak.models.sessions.infinispan.changes.sessions.PersisterLastSessionRefreshStoreFactory;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.representations.idm.RealmRepresentation;
@ -37,6 +38,7 @@ import org.keycloak.services.managers.UserSessionManager;
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
import org.keycloak.testsuite.arquillian.annotation.ModelTest;
import org.keycloak.testsuite.runonserver.RunOnServerDeployment;
import org.keycloak.timer.TimerProvider;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
@ -433,6 +435,11 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
@Test
@ModelTest
public void testExpired(KeycloakSession session) {
// Suspend periodic tasks to avoid race-conditions, which may cause missing updates of lastSessionRefresh times to UserSessionPersisterProvider
TimerProvider timer = session.getProvider(TimerProvider.class);
TimerProvider.TimerTaskContext timerTaskCtx = timer.cancelTask(PersisterLastSessionRefreshStoreFactory.DB_LSR_PERIODIC_TASK_NAME);
try {
AtomicReference<UserSessionModel[]> origSessionsAt = new AtomicReference<>();
// Key is userSessionId, value is set of client UUIDS
@ -485,7 +492,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
session0.setLastSessionRefresh(Time.currentTime());
});
try {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionExpired4) -> {
currentSession = sessionExpired4;
realm = currentSession.realms().getRealm("test");
@ -545,6 +552,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
} finally {
Time.setOffset(0);
timer.schedule(timerTaskCtx.getRunnable(), timerTaskCtx.getIntervalMillis(), PersisterLastSessionRefreshStoreFactory.DB_LSR_PERIODIC_TASK_NAME);
}
}