KEYCLOAK-12065 UserSessionInitializerTest is failing

This commit is contained in:
mabartos 2020-02-11 13:01:23 +01:00 committed by Marek Posolda
parent c3f0b342bf
commit 1bdf77f409
3 changed files with 17 additions and 11 deletions

View file

@ -21,6 +21,7 @@ import org.junit.After;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.common.util.reflections.Reflections;
import org.keycloak.events.Details;
import org.keycloak.models.KeycloakSession;
import org.keycloak.representations.IDToken;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.EventRepresentation;
@ -109,4 +110,9 @@ public abstract class AbstractTestRealmKeycloakTest extends AbstractKeycloakTest
return idToken;
}
/** KEYCLOAK-12065 Inherit Client Connection from parent session **/
public static KeycloakSession inheritClientConnection(KeycloakSession parentSession, KeycloakSession currentSession) {
currentSession.getContext().setConnection(parentSession.getContext().getConnection());
return currentSession;
}
}

View file

@ -88,8 +88,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
AtomicReference<UserSessionModel[]> origSessionsAtomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession SessionInit1) -> {
KeycloakSession currentSession = SessionInit1;
UserSessionManager sessionManager = new UserSessionManager(currentSession);
KeycloakSession currentSession = inheritClientConnection(session, SessionInit1);
int started = Time.currentTime();
startedAtomic.set(started);
@ -103,7 +102,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession SessionInit2) -> {
KeycloakSession currentSession = SessionInit2;
KeycloakSession currentSession = inheritClientConnection(session, SessionInit2);
RealmModel realm = currentSession.realms().getRealmByName(realmName);
int started = startedAtomic.get();
@ -133,8 +132,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
AtomicReference<UserSessionModel[]> origSessionsAtomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession SessionInitWithDeleting1) -> {
KeycloakSession currentSession = SessionInitWithDeleting1;
UserSessionManager sessionManager = new UserSessionManager(currentSession);
KeycloakSession currentSession = inheritClientConnection(session, SessionInitWithDeleting1);
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -149,7 +147,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession SessionInitWithDeleting2) -> {
KeycloakSession currentSession = SessionInitWithDeleting2;
KeycloakSession currentSession = inheritClientConnection(session, SessionInitWithDeleting2);
// Load sessions from persister into infinispan/memory
UserSessionProviderFactory userSessionFactory = (UserSessionProviderFactory) currentSession.getKeycloakSessionFactory().getProviderFactory(UserSessionProvider.class);
@ -157,7 +155,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession SessionInitWithDeleting3) -> {
KeycloakSession currentSession = SessionInitWithDeleting3;
KeycloakSession currentSession = inheritClientConnection(session, SessionInitWithDeleting3);
RealmModel realm = currentSession.realms().getRealmByName(realmName);
int started = startedAtomic.get();
@ -184,14 +182,14 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
AtomicReference<UserSessionModel[]> origSessionsAtomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession createSessionPersister1) -> {
KeycloakSession currentSession = createSessionPersister1;
KeycloakSession currentSession = inheritClientConnection(session, createSessionPersister1);
UserSessionModel[] origSessions = createSessions(currentSession);
origSessionsAtomic.set(origSessions);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession createSessionPersister2) -> {
KeycloakSession currentSession = createSessionPersister2;
KeycloakSession currentSession = inheritClientConnection(session, createSessionPersister2);
RealmModel realm = currentSession.realms().getRealmByName(realmName);
UserSessionManager sessionManager = new UserSessionManager(currentSession);
@ -206,7 +204,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession createSessionPersister3) -> {
KeycloakSession currentSession = createSessionPersister3;
KeycloakSession currentSession = inheritClientConnection(session, createSessionPersister3);
RealmModel realm = currentSession.realms().getRealmByName(realmName);
// Delete cache (persisted sessions are still kept)
@ -219,7 +217,7 @@ public class UserSessionInitializerTest extends AbstractTestRealmKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession createSessionPersister4) -> {
KeycloakSession currentSession = createSessionPersister4;
KeycloakSession currentSession = inheritClientConnection(session, createSessionPersister4);
RealmModel realm = currentSession.realms().getRealmByName(realmName);
ClientModel testApp = realm.getClientByClientId("test-app");

View file

@ -259,6 +259,7 @@ public class UserSessionProviderTest extends AbstractTestRealmKeycloakTest {
RealmModel realm = session.realms().getRealmByName("test");
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession kcSession) -> {
inheritClientConnection(session, kcSession);
createSessions(kcSession);
});
Map<String, Integer> clientSessionsKept = new HashMap<>();
@ -300,6 +301,7 @@ public class UserSessionProviderTest extends AbstractTestRealmKeycloakTest {
public void testRemoveUserSessionsByRealm(KeycloakSession session) {
RealmModel realm = session.realms().getRealmByName("test");
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession kcSession) -> {
inheritClientConnection(session, kcSession);
createSessions(kcSession);
});