1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import org .springframework .util .ConcurrencyThrottleSupport ;
22
22
23
23
import static org .assertj .core .api .Assertions .assertThat ;
24
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
24
25
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
25
26
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
26
27
31
32
*/
32
33
class SimpleAsyncTaskExecutorTests {
33
34
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
+
34
52
@ Test
35
53
void cannotExecuteWhenConcurrencyIsSwitchedOff () {
36
54
try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ()) {
@@ -41,35 +59,34 @@ void cannotExecuteWhenConcurrencyIsSwitchedOff() {
41
59
}
42
60
43
61
@ Test
44
- void throttleIsNotActiveByDefault () {
62
+ void taskRejectedWhenConcurrencyLimitReached () {
45
63
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 ()));
47
69
}
48
70
}
49
71
50
72
@ Test
51
73
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
+ }
58
81
}
59
82
60
83
@ Test
61
84
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" );
73
90
}
74
91
}
75
92
@@ -89,7 +106,12 @@ private static final class NoOpRunnable implements Runnable {
89
106
90
107
@ Override
91
108
public void run () {
92
- // no-op
109
+ try {
110
+ Thread .sleep (1000 );
111
+ }
112
+ catch (InterruptedException ex ) {
113
+ Thread .currentThread ().interrupt ();
114
+ }
93
115
}
94
116
}
95
117
0 commit comments