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); UserSessionModel[] sessions = createSessions(session);
Time.setOffset(100); Time.setOffset(100);
try {
UserSessionModel userSession = session.sessions().getUserSession(realm, sessions[0].getId()); 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"); 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()); userSession = session.sessions().getUserSession(realm, sessions[0].getId());
assertSession(userSession, session.users().getUserByUsername(realm, "user2"), "127.0.0.6", started + 100, started + 100); assertSession(userSession, session.users().getUserByUsername(realm, "user2"), "127.0.0.6", started + 100, started + 100);
} finally {
Time.setOffset(0); Time.setOffset(0);
}
} }
@Test @Test

View file

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

View file

@ -118,37 +118,38 @@ public class LastSessionRefreshUnitTest extends AbstractKeycloakTest {
@Override @Override
public void run(KeycloakSession session) { 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 { try {
Thread.sleep(50); // Long timer interval. No message due the timer wasn't executed
} catch (InterruptedException ie) { CrossDCLastSessionRefreshStore customStore1 = createStoreInstance(session, 100000, 10);
throw new RuntimeException(); Time.setOffset(100);
}
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 try {
CrossDCLastSessionRefreshStore customStore2 = createStoreInstance(session, 10, 10); Thread.sleep(50);
Time.setOffset(200); } 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()); Assert.assertEquals(1, counter.get());
}, 100, 10);
Assert.assertEquals(1, counter.get()); // Another sleep won't send message. lastRun was updated
try {
// Another sleep won't send message. lastRun was updated Thread.sleep(50);
try { } catch (InterruptedException ie) {
Thread.sleep(50); throw new RuntimeException();
} 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.DefaultAuthorizationProviderFactory;
import org.keycloak.authorization.store.StoreFactorySpi; import org.keycloak.authorization.store.StoreFactorySpi;
import org.keycloak.cluster.ClusterSpi; import org.keycloak.cluster.ClusterSpi;
import org.keycloak.common.util.Time;
import org.keycloak.component.ComponentFactoryProviderFactory; import org.keycloak.component.ComponentFactoryProviderFactory;
import org.keycloak.component.ComponentFactorySpi; import org.keycloak.component.ComponentFactorySpi;
import org.keycloak.events.EventStoreSpi; import org.keycloak.events.EventStoreSpi;
@ -557,13 +558,15 @@ public abstract class KeycloakModelTest {
} }
@Before @Before
public void createEnvironment() { public final void createEnvironment() {
Time.setOffset(0);
USE_DEFAULT_FACTORY = isUseSameKeycloakSessionFactoryForAllThreads(); USE_DEFAULT_FACTORY = isUseSameKeycloakSessionFactoryForAllThreads();
KeycloakModelUtils.runJobInTransaction(getFactory(), this::createEnvironment); KeycloakModelUtils.runJobInTransaction(getFactory(), this::createEnvironment);
} }
@After @After
public void cleanEnvironment() { public final void cleanEnvironment() {
Time.setOffset(0);
if (getFactory() == null) { if (getFactory() == null) {
reinitializeKeycloakSessionFactory(); reinitializeKeycloakSessionFactory();
} }