Skip to content

Commit 32277b7

Browse files
committed
Attempt to make AbstractSchedulingTaskExecutorTests more robust
1 parent 4b9f5a3 commit 32277b7

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

spring-context/src/test/java/org/springframework/scheduling/concurrent/AbstractSchedulingTaskExecutorTests.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
/**
4040
* @author Juergen Hoeller
41+
* @author Sam Brannen
4142
* @since 5.0.5
4243
*/
4344
public abstract class AbstractSchedulingTaskExecutorTests {
@@ -99,14 +100,12 @@ public void submitFailingRunnable() throws Exception {
99100

100101
@Test
101102
public void submitRunnableWithGetAfterShutdown() throws Exception {
102-
TestTask task1 = new TestTask(-1);
103-
Future<?> future1 = executor.submit(task1);
104-
TestTask task2 = new TestTask(-1);
105-
Future<?> future2 = executor.submit(task2);
103+
Future<?> future1 = executor.submit(new TestTask(-1));
104+
Future<?> future2 = executor.submit(new TestTask(-1));
106105
shutdownExecutor();
107106
assertThatExceptionOfType(CancellationException.class).isThrownBy(() -> {
108-
future1.get();
109-
future2.get();
107+
future1.get(1000, TimeUnit.MILLISECONDS);
108+
future2.get(1000, TimeUnit.MILLISECONDS);
110109
});
111110
}
112111

@@ -141,15 +140,23 @@ public void submitFailingListenableRunnable() throws Exception {
141140

142141
@Test
143142
public void submitListenableRunnableWithGetAfterShutdown() throws Exception {
144-
TestTask task1 = new TestTask(-1);
145-
ListenableFuture<?> future1 = executor.submitListenable(task1);
146-
TestTask task2 = new TestTask(-1);
147-
ListenableFuture<?> future2 = executor.submitListenable(task2);
143+
ListenableFuture<?> future1 = executor.submitListenable(new TestTask(-1));
144+
ListenableFuture<?> future2 = executor.submitListenable(new TestTask(-1));
148145
shutdownExecutor();
149-
assertThatExceptionOfType(CancellationException.class).isThrownBy(() -> {
150-
future1.get();
151-
future2.get();
152-
});
146+
147+
try {
148+
future1.get(1000, TimeUnit.MILLISECONDS);
149+
}
150+
catch (Exception ex) {
151+
/* ignore */
152+
}
153+
Awaitility.await()
154+
.atMost(4, TimeUnit.SECONDS)
155+
.pollInterval(10, TimeUnit.MILLISECONDS)
156+
.untilAsserted(() ->
157+
assertThatExceptionOfType(CancellationException.class).isThrownBy(() ->
158+
future2.get(1000, TimeUnit.MILLISECONDS)
159+
));
153160
}
154161

155162
@Test
@@ -171,10 +178,8 @@ public void submitFailingCallable() throws Exception {
171178

172179
@Test
173180
public void submitCallableWithGetAfterShutdown() throws Exception {
174-
TestCallable task1 = new TestCallable(-1);
175-
Future<?> future1 = executor.submit(task1);
176-
TestCallable task2 = new TestCallable(-1);
177-
Future<?> future2 = executor.submit(task2);
181+
Future<?> future1 = executor.submit(new TestCallable(-1));
182+
Future<?> future2 = executor.submit(new TestCallable(-1));
178183
shutdownExecutor();
179184
assertThatExceptionOfType(CancellationException.class).isThrownBy(() -> {
180185
future1.get(1000, TimeUnit.MILLISECONDS);
@@ -213,14 +218,13 @@ public void submitFailingListenableCallable() throws Exception {
213218

214219
@Test
215220
public void submitListenableCallableWithGetAfterShutdown() throws Exception {
216-
TestCallable task1 = new TestCallable(-1);
217-
ListenableFuture<?> future1 = executor.submitListenable(task1);
218-
TestCallable task2 = new TestCallable(-1);
219-
ListenableFuture<?> future2 = executor.submitListenable(task2);
221+
ListenableFuture<?> future1 = executor.submitListenable(new TestCallable(-1));
222+
ListenableFuture<?> future2 = executor.submitListenable(new TestCallable(-1));
220223
shutdownExecutor();
221-
future1.get(1000, TimeUnit.MILLISECONDS);
222-
assertThatExceptionOfType(CancellationException.class).isThrownBy(() ->
223-
future2.get(1000, TimeUnit.MILLISECONDS));
224+
assertThatExceptionOfType(CancellationException.class).isThrownBy(() -> {
225+
future1.get(1000, TimeUnit.MILLISECONDS);
226+
future2.get(1000, TimeUnit.MILLISECONDS);
227+
});
224228
}
225229

226230

0 commit comments

Comments
 (0)