Search in sources :

Example 1 with HotRodServerRule

use of org.keycloak.testsuite.model.HotRodServerRule in project keycloak by keycloak.

the class UserSessionInitializerTest method testUserSessionPropagationBetweenSites.

@Test
@RequireProvider(value = UserSessionProvider.class, only = InfinispanUserSessionProviderFactory.PROVIDER_ID)
public void testUserSessionPropagationBetweenSites() throws InterruptedException {
    AtomicInteger index = new AtomicInteger();
    AtomicReference<String> userSessionId = new AtomicReference<>();
    AtomicReference<List<Boolean>> containsSession = new AtomicReference<>(new LinkedList<>());
    Object lock = new Object();
    Optional<HotRodServerRule> hotRodServer = getParameters(HotRodServerRule.class).findFirst();
    inIndependentFactories(4, 300, () -> {
        synchronized (lock) {
            if (index.incrementAndGet() == 1) {
                // create a user session in the first node
                UserSessionModel userSessionModel = withRealm(realmId, (session, realm) -> {
                    final UserModel user = session.users().getUserByUsername(realm, "user1");
                    return session.sessions().createUserSession(realm, user, "un1", "ip1", "auth", false, null, null);
                });
                userSessionId.set(userSessionModel.getId());
            } else {
                // try to get the user session at other nodes and also at different sites
                inComittedTransaction(session -> {
                    InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class);
                    Cache<String, Object> localSessions = provider.getCache(USER_SESSION_CACHE_NAME);
                    containsSession.get().add(localSessions.containsKey(userSessionId.get()));
                    if (hotRodServer.isPresent()) {
                        RemoteCache<String, Object> remoteSessions = provider.getRemoteCache(USER_SESSION_CACHE_NAME);
                        containsSession.get().add(remoteSessions.containsKey(userSessionId.get()));
                    }
                });
            }
        }
    });
    assertThat(containsSession.get(), everyItem(is(true)));
    // 3 nodes (first node just creates the session), with Hot Rod server we have local + remote cache, without just local cache
    int size = hotRodServer.isPresent() ? 6 : 3;
    assertThat(containsSession.get().size(), is(size));
}
Also used : HotRodServerRule(org.keycloak.testsuite.model.HotRodServerRule) UserSessionModel(org.keycloak.models.UserSessionModel) AtomicReference(java.util.concurrent.atomic.AtomicReference) UserModel(org.keycloak.models.UserModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LinkedList(java.util.LinkedList) List(java.util.List) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) RequireProvider(org.keycloak.testsuite.model.RequireProvider)

Aggregations

LinkedList (java.util.LinkedList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)1 UserModel (org.keycloak.models.UserModel)1 UserSessionModel (org.keycloak.models.UserSessionModel)1 HotRodServerRule (org.keycloak.testsuite.model.HotRodServerRule)1 KeycloakModelTest (org.keycloak.testsuite.model.KeycloakModelTest)1 RequireProvider (org.keycloak.testsuite.model.RequireProvider)1