Skip to content

Commit ff26698

Browse files
martin-kanishmlnarik
authored andcommitted
Stabilize testCreateUserSessionsParallel model test
1 parent 65348e4 commit ff26698

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionProviderModelTest.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.Set;
5454
import java.util.UUID;
5555
import java.util.concurrent.ConcurrentHashMap;
56+
import java.util.concurrent.CountDownLatch;
5657
import java.util.concurrent.atomic.AtomicReference;
5758
import java.util.stream.Collectors;
5859

@@ -296,20 +297,40 @@ public void testCreateUserSessionsParallel() throws InterruptedException {
296297
(usMapStorageProvider == null || ConcurrentHashMapStorageProviderFactory.PROVIDER_ID.equals(usMapStorageProvider)));
297298

298299
Set<String> userSessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>());
300+
CountDownLatch latch = new CountDownLatch(4);
299301

300-
inIndependentFactories(4, 30, () -> withRealm(realmId, (session, realm) -> {
301-
UserModel user = session.users().getUserByUsername(realm, "user1");
302-
UserSessionModel userSession = session.sessions().createUserSession(realm, user, "user1", "", "", false, null, null);
303-
userSessionIds.add(userSession.getId());
302+
inIndependentFactories(4, 30, () -> {
303+
withRealm(realmId, (session, realm) -> {
304+
UserModel user = session.users().getUserByUsername(realm, "user1");
305+
UserSessionModel userSession = session.sessions().createUserSession(realm, user, "user1", "", "", false, null, null);
306+
userSessionIds.add(userSession.getId());
304307

305-
return null;
306-
}));
308+
latch.countDown();
309+
310+
return null;
311+
});
312+
313+
// wait for other nodes to finish
314+
try {
315+
latch.await();
316+
} catch (InterruptedException e) {
317+
throw new RuntimeException(e);
318+
}
307319

308-
assertThat(userSessionIds, Matchers.iterableWithSize(4));
320+
assertThat(userSessionIds, Matchers.iterableWithSize(4));
309321

310-
withRealm(realmId, (session, realm) -> {
311-
userSessionIds.forEach(id -> Assert.assertNotNull(session.sessions().getUserSession(realm, id)));
312-
return null;
322+
// wait a bit to allow replication
323+
try {
324+
Thread.sleep(1000);
325+
} catch (InterruptedException e) {
326+
throw new RuntimeException(e);
327+
}
328+
329+
withRealm(realmId, (session, realm) -> {
330+
userSessionIds.forEach(id -> Assert.assertNotNull(session.sessions().getUserSession(realm, id)));
331+
332+
return null;
333+
});
313334
});
314335
}
315336
}

0 commit comments

Comments
 (0)