diff --git a/spring-context/src/test/java/org/springframework/resilience/ConcurrencyLimitTests.java b/spring-context/src/test/java/org/springframework/resilience/ConcurrencyLimitTests.java index 72bf334df4c..799d08a5301 100644 --- a/spring-context/src/test/java/org/springframework/resilience/ConcurrencyLimitTests.java +++ b/spring-context/src/test/java/org/springframework/resilience/ConcurrencyLimitTests.java @@ -19,6 +19,7 @@ package org.springframework.resilience; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; @@ -120,6 +121,10 @@ class ConcurrencyLimitTests { @Test void withPostProcessorForMethodWithRejection() throws Exception{ + if (ForkJoinPool.getCommonPoolParallelism() < 4) { + return; // not enough concurrency possible + } + AnnotatedMethodBean proxy = createProxy(AnnotatedMethodBean.class); AnnotatedMethodBean target = (AnnotatedMethodBean) AopProxyUtils.getSingletonTarget(proxy); @@ -128,7 +133,7 @@ class ConcurrencyLimitTests { futures.add(CompletableFuture.runAsync(proxy::rejectingOperation)); } Thread.sleep(10); - for (int i = 2; i < 10; i++) { + for (int i = 2; i < 4; i++) { futures.add(CompletableFuture.runAsync(() -> assertThatExceptionOfType(InvocationRejectedException.class).isThrownBy(proxy::rejectingOperation) .withMessageContaining(AnnotatedMethodBean.class.getName() + ".rejectingOperation") @@ -159,6 +164,10 @@ class ConcurrencyLimitTests { @Test void withPostProcessorForClassWithRejection() throws Exception { + if (ForkJoinPool.getCommonPoolParallelism() < 4) { + return; // not enough concurrency possible + } + AnnotatedClassBeanWithRejection proxy = createProxy(AnnotatedClassBeanWithRejection.class); AnnotatedClassBeanWithRejection target = (AnnotatedClassBeanWithRejection) AopProxyUtils.getSingletonTarget(proxy); @@ -174,7 +183,7 @@ class ConcurrencyLimitTests { assertThatExceptionOfType(InvocationRejectedException.class).isThrownBy(proxy::otherOperation) .withMessageContaining(AnnotatedClassBeanWithRejection.class.getName()) .satisfies(ex -> assertThat(ex.getTarget() == target)))); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 4; i++) { futures.add(CompletableFuture.runAsync(proxy::overrideOperation)); } CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); diff --git a/spring-core/src/test/java/org/springframework/core/task/SyncTaskExecutorTests.java b/spring-core/src/test/java/org/springframework/core/task/SyncTaskExecutorTests.java index b94da01be96..56de9c319d1 100644 --- a/spring-core/src/test/java/org/springframework/core/task/SyncTaskExecutorTests.java +++ b/spring-core/src/test/java/org/springframework/core/task/SyncTaskExecutorTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; @@ -94,6 +95,10 @@ class SyncTaskExecutorTests { @Test void taskRejectedWhenConcurrencyLimitReached() throws Exception { + if (ForkJoinPool.getCommonPoolParallelism() < 4) { + return; // not enough concurrency possible + } + SyncTaskExecutor executor = new SyncTaskExecutor(); executor.setConcurrencyLimit(2); executor.setRejectTasksWhenLimitReached(true); @@ -104,7 +109,7 @@ class SyncTaskExecutorTests { futures.add(CompletableFuture.runAsync(() -> executor.execute(target::concurrentOperation))); } Thread.sleep(10); - for (int i = 2; i < 10; i++) { + for (int i = 2; i < 4; i++) { futures.add(CompletableFuture.runAsync(() -> assertThatExceptionOfType(TaskRejectedException.class).isThrownBy(() -> executor.execute(target::concurrentOperation)))); }