Adding central time offset reset in model tests as it was missing for AuthenticationSessionTest and UserSessionPersisterProviderTest

Also adding try/finally in other places in the integration tests where it was missing.

Closes #12530
This commit is contained in:
Alexander Schwartz 2022-06-15 17:10:55 +02:00 committed by Hynek Mlnařík
parent 39cff0750c
commit 71e7982a49
4 changed files with 46 additions and 36 deletions

View file

@ -142,16 +142,18 @@ public class UserSessionProviderTest extends AbstractTestRealmKeycloakTest {
UserSessionModel[] sessions = createSessions(session);
Time.setOffset(100);
try {
UserSessionModel userSession = session.sessions().getUserSession(realm, sessions[0].getId());
assertSession(userSession, session.users().getUserByUsername(realm, "user1"), "127.0.0.1", started, started, "test-app", "third-party");
UserSessionModel userSession = session.sessions().getUserSession(realm, sessions[0].getId());
assertSession(userSession, session.users().getUserByUsername(realm, "user1"), "127.0.0.1", started, started, "test-app", "third-party");
userSession.restartSession(realm, session.users().getUserByUsername(realm, "user2"), "user2", "127.0.0.6", "form", true, null, null);
userSession.restartSession(realm, session.users().getUserByUsername(realm, "user2"), "user2", "127.0.0.6", "form", true, null, null);
userSession = session.sessions().getUserSession(realm, sessions[0].getId());
assertSession(userSession, session.users().getUserByUsername(realm, "user2"), "127.0.0.6", started + 100, started + 100);
Time.setOffset(0);
userSession = session.sessions().getUserSession(realm, sessions[0].getId());
assertSession(userSession, session.users().getUserByUsername(realm, "user2"), "127.0.0.6", started + 100, started + 100);
} finally {
Time.setOffset(0);
}
}
@Test

View file

@ -1077,8 +1077,12 @@ public class ClientAuthSignedJWTTest extends AbstractKeycloakTest {
}
Time.setOffset(tokenTimeOffset);
String jwt = jwtProvider.createSignedRequestToken(app1.getClientId(), getRealmInfoUrl());
Time.setOffset(0);
String jwt;
try {
jwt = jwtProvider.createSignedRequestToken(app1.getClientId(), getRealmInfoUrl());
} finally {
Time.setOffset(0);
}
return doClientCredentialsGrantRequest(jwt);
}

View file

@ -118,37 +118,38 @@ public class LastSessionRefreshUnitTest extends AbstractKeycloakTest {
@Override
public void run(KeycloakSession session) {
// Long timer interval. No message due the timer wasn't executed
CrossDCLastSessionRefreshStore customStore1 = createStoreInstance(session, 100000, 10);
Time.setOffset(100);
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
Assert.assertEquals(0, counter.get());
// Long timer interval. No message due the timer wasn't executed
CrossDCLastSessionRefreshStore customStore1 = createStoreInstance(session, 100000, 10);
Time.setOffset(100);
// Short timer interval 10 ms. 1 message due the interval is executed and lastRun was in the past due to Time.setOffset
CrossDCLastSessionRefreshStore customStore2 = createStoreInstance(session, 10, 10);
Time.setOffset(200);
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
Assert.assertEquals(0, counter.get());
// Short timer interval 10 ms. 1 message due the interval is executed and lastRun was in the past due to Time.setOffset
CrossDCLastSessionRefreshStore customStore2 = createStoreInstance(session, 10, 10);
Time.setOffset(200);
Retry.execute(() -> {
Assert.assertEquals(1, counter.get());
}, 100, 10);
Retry.execute(() -> {
Assert.assertEquals(1, counter.get());
}, 100, 10);
Assert.assertEquals(1, counter.get());
// Another sleep won't send message. lastRun was updated
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
throw new RuntimeException();
// Another sleep won't send message. lastRun was updated
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
Assert.assertEquals(1, counter.get());
} finally {
Time.setOffset(0);
}
Assert.assertEquals(1, counter.get());
Time.setOffset(0);
}
}

View file

@ -24,6 +24,7 @@ import org.keycloak.authorization.AuthorizationSpi;
import org.keycloak.authorization.DefaultAuthorizationProviderFactory;
import org.keycloak.authorization.store.StoreFactorySpi;
import org.keycloak.cluster.ClusterSpi;
import org.keycloak.common.util.Time;
import org.keycloak.component.ComponentFactoryProviderFactory;
import org.keycloak.component.ComponentFactorySpi;
import org.keycloak.events.EventStoreSpi;
@ -557,13 +558,15 @@ public abstract class KeycloakModelTest {
}
@Before
public void createEnvironment() {
public final void createEnvironment() {
Time.setOffset(0);
USE_DEFAULT_FACTORY = isUseSameKeycloakSessionFactoryForAllThreads();
KeycloakModelUtils.runJobInTransaction(getFactory(), this::createEnvironment);
}
@After
public void cleanEnvironment() {
public final void cleanEnvironment() {
Time.setOffset(0);
if (getFactory() == null) {
reinitializeKeycloakSessionFactory();
}