11/*
2- * Copyright 2002-2023 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import org .springframework .util .ConcurrencyThrottleSupport ;
2222
2323import static org .assertj .core .api .Assertions .assertThat ;
24+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2425import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
2526import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
2627
3132 */
3233class SimpleAsyncTaskExecutorTests {
3334
35+ @ Test
36+ void isActiveUntilClose () {
37+ SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ();
38+ assertThat (executor .isActive ()).isTrue ();
39+ assertThat (executor .isThrottleActive ()).isFalse ();
40+ executor .close ();
41+ assertThat (executor .isActive ()).isFalse ();
42+ assertThat (executor .isThrottleActive ()).isFalse ();
43+ }
44+
45+ @ Test
46+ void throwsExceptionWhenSuppliedWithNullRunnable () {
47+ try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ()) {
48+ assertThatIllegalArgumentException ().isThrownBy (() -> executor .execute (null ));
49+ }
50+ }
51+
3452 @ Test
3553 void cannotExecuteWhenConcurrencyIsSwitchedOff () {
3654 try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ()) {
@@ -41,35 +59,34 @@ void cannotExecuteWhenConcurrencyIsSwitchedOff() {
4159 }
4260
4361 @ Test
44- void throttleIsNotActiveByDefault () {
62+ void taskRejectedWhenConcurrencyLimitReached () {
4563 try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ()) {
46- assertThat (executor .isThrottleActive ()).as ("Concurrency throttle must not default to being active (on)" ).isFalse ();
64+ executor .setConcurrencyLimit (1 );
65+ executor .setRejectTasksWhenLimitReached (true );
66+ assertThat (executor .isThrottleActive ()).isTrue ();
67+ executor .execute (new NoOpRunnable ());
68+ assertThatExceptionOfType (TaskRejectedException .class ).isThrownBy (() -> executor .execute (new NoOpRunnable ()));
4769 }
4870 }
4971
5072 @ Test
5173 void threadNameGetsSetCorrectly () {
52- final String customPrefix = "chankPop#" ;
53- final Object monitor = new Object ();
54- SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor (customPrefix );
55- ThreadNameHarvester task = new ThreadNameHarvester (monitor );
56- executeAndWait (executor , task , monitor );
57- assertThat (task .getThreadName ()).startsWith (customPrefix );
74+ String customPrefix = "chankPop#" ;
75+ Object monitor = new Object ();
76+ try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor (customPrefix )) {
77+ ThreadNameHarvester task = new ThreadNameHarvester (monitor );
78+ executeAndWait (executor , task , monitor );
79+ assertThat (task .getThreadName ()).startsWith (customPrefix );
80+ }
5881 }
5982
6083 @ Test
6184 void threadFactoryOverridesDefaults () {
62- final Object monitor = new Object ();
63- SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor (runnable -> new Thread (runnable , "test" ));
64- ThreadNameHarvester task = new ThreadNameHarvester (monitor );
65- executeAndWait (executor , task , monitor );
66- assertThat (task .getThreadName ()).isEqualTo ("test" );
67- }
68-
69- @ Test
70- void throwsExceptionWhenSuppliedWithNullRunnable () {
71- try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ()) {
72- assertThatIllegalArgumentException ().isThrownBy (() -> executor .execute (null ));
85+ Object monitor = new Object ();
86+ try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor (runnable -> new Thread (runnable , "test" ))) {
87+ ThreadNameHarvester task = new ThreadNameHarvester (monitor );
88+ executeAndWait (executor , task , monitor );
89+ assertThat (task .getThreadName ()).isEqualTo ("test" );
7390 }
7491 }
7592
@@ -89,7 +106,12 @@ private static final class NoOpRunnable implements Runnable {
89106
90107 @ Override
91108 public void run () {
92- // no-op
109+ try {
110+ Thread .sleep (1000 );
111+ }
112+ catch (InterruptedException ex ) {
113+ Thread .currentThread ().interrupt ();
114+ }
93115 }
94116 }
95117
0 commit comments