From 64cbbde7cf255c853ba098f2cf3e899413ff4e0a Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 27 Jan 2022 14:41:22 +0100 Subject: [PATCH] Adding workaround unstable tests due to Infinispan 12.1.7 Closes #9867 --- .../testsuite/model/KeycloakModelTest.java | 27 +++++++++++++++++-- .../model/infinispan/CacheExpirationTest.java | 4 +-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/testsuite/model/src/test/java/org/keycloak/testsuite/model/KeycloakModelTest.java b/testsuite/model/src/test/java/org/keycloak/testsuite/model/KeycloakModelTest.java index 93374d5de41..c2df8d2ff28 100644 --- a/testsuite/model/src/test/java/org/keycloak/testsuite/model/KeycloakModelTest.java +++ b/testsuite/model/src/test/java/org/keycloak/testsuite/model/KeycloakModelTest.java @@ -59,6 +59,7 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -331,7 +332,27 @@ public abstract class KeycloakModelTest { public static void inIndependentFactories(int numThreads, int timeoutSeconds, Runnable task) throws InterruptedException { ExecutorService es = Executors.newFixedThreadPool(numThreads); try { - Callable independentTask = () -> inIndependentFactory(() -> { task.run(); return null; }); + /* + workaround for Infinispan 12.1.7.Final to prevent an internal Infinispan NullPointerException + when multiple nodes tried to join at the same time by starting them sequentially with 1 sec delay. + Already fixed in Infinispan 13. + https://issues.redhat.com/browse/ISPN-13231 + */ + Semaphore sem = new Semaphore(1); + Callable independentTask = () -> { + try { + sem.acquire(); + return inIndependentFactory(() -> { + Thread.sleep(1000); + sem.release(); + task.run(); + return null; + }); + } catch (Exception ex) { + LOG.error("Thread terminated with an exception", ex); + return null; + } + }; es.invokeAll( IntStream.range(0, numThreads) .mapToObj(i -> independentTask) @@ -339,7 +360,9 @@ public abstract class KeycloakModelTest { timeoutSeconds, TimeUnit.SECONDS ); } finally { - es.shutdownNow(); + LOG.debugf("waiting for threads to shutdown to avoid that one test pollutes another test"); + es.shutdown(); + LOG.debugf("shutdown of threads complete"); } } diff --git a/testsuite/model/src/test/java/org/keycloak/testsuite/model/infinispan/CacheExpirationTest.java b/testsuite/model/src/test/java/org/keycloak/testsuite/model/infinispan/CacheExpirationTest.java index b512298ed71..73b0d271c4b 100644 --- a/testsuite/model/src/test/java/org/keycloak/testsuite/model/infinispan/CacheExpirationTest.java +++ b/testsuite/model/src/test/java/org/keycloak/testsuite/model/infinispan/CacheExpirationTest.java @@ -80,7 +80,7 @@ public class CacheExpirationTest extends KeycloakModelTest { InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class); Cache cache = provider.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME); do { - try { Thread.sleep(1000); } catch (InterruptedException ex) {} + try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } } while (! cache.getAdvancedCache().getDistributionManager().isJoinComplete()); cache.keySet().forEach(s -> {}); }); @@ -102,7 +102,7 @@ public class CacheExpirationTest extends KeycloakModelTest { // Wait for at most 3 minutes which is much more than 15 seconds expiration set in DefaultInfinispanConnectionProviderFactory for (int i = 0; i < 3 * 60; i++) { - try { Thread.sleep(1000); } catch (InterruptedException ex) {} + try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } if (getNumberOfInstancesOfClass(AuthenticationSessionAuthNoteUpdateEvent.class) == 0) { break; }