25
25
import java .util .concurrent .Executor ;
26
26
import java .util .concurrent .Executors ;
27
27
import java .util .concurrent .Future ;
28
+ import java .util .concurrent .TimeUnit ;
28
29
30
+ import org .awaitility .Awaitility ;
29
31
import org .junit .Test ;
30
32
import org .mockito .Mockito ;
31
33
@@ -163,7 +165,7 @@ public void customAsyncAnnotationIsPropagated() {
163
165
Object bean = ctx .getBean (CustomAsyncBean .class );
164
166
assertTrue (AopUtils .isAopProxy (bean ));
165
167
boolean isAsyncAdvised = false ;
166
- for (Advisor advisor : ((Advised )bean ).getAdvisors ()) {
168
+ for (Advisor advisor : ((Advised ) bean ).getAdvisors ()) {
167
169
if (advisor instanceof AsyncAnnotationAdvisor ) {
168
170
isAsyncAdvised = true ;
169
171
break ;
@@ -184,85 +186,129 @@ public void aspectModeAspectJAttemptsToRegisterAsyncAspect() {
184
186
185
187
@ Test
186
188
public void customExecutorBean () throws InterruptedException {
189
+ // Arrange
187
190
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
188
191
ctx .register (CustomExecutorBean .class );
189
192
ctx .refresh ();
190
-
191
193
AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
194
+ // Act
192
195
asyncBean .work ();
193
- Thread .sleep (500 );
196
+ // Assert
197
+ Awaitility .await ()
198
+ .atMost (500 , TimeUnit .MILLISECONDS )
199
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
200
+ .until (() -> asyncBean .getThreadOfExecution () != null );
194
201
assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Custom-" ));
195
-
196
202
ctx .close ();
197
203
}
198
204
199
205
@ Test
200
206
public void customExecutorConfig () throws InterruptedException {
207
+ // Arrange
201
208
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
202
209
ctx .register (CustomExecutorConfig .class );
203
210
ctx .refresh ();
204
-
205
211
AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
212
+ // Act
206
213
asyncBean .work ();
207
- Thread .sleep (500 );
214
+ // Assert
215
+ Awaitility .await ()
216
+ .atMost (500 , TimeUnit .MILLISECONDS )
217
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
218
+ .until (() -> asyncBean .getThreadOfExecution () != null );
208
219
assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Custom-" ));
220
+ ctx .close ();
221
+ }
209
222
210
- TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler )
211
- ctx .getBean ("exceptionHandler" );
223
+ @ Test
224
+ public void customExecutorConfigWithThrowsException () {
225
+ // Arrange
226
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
227
+ ctx .register (CustomExecutorConfig .class );
228
+ ctx .refresh ();
229
+ AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
230
+ Method method = ReflectionUtils .findMethod (AsyncBean .class , "fail" );
231
+ TestableAsyncUncaughtExceptionHandler exceptionHandler =
232
+ (TestableAsyncUncaughtExceptionHandler ) ctx .getBean ("exceptionHandler" );
212
233
assertFalse ("handler should not have been called yet" , exceptionHandler .isCalled ());
213
-
234
+ // Act
214
235
asyncBean .fail ();
215
- Thread .sleep (500 );
216
- Method method = ReflectionUtils .findMethod (AsyncBean .class , "fail" );
217
- exceptionHandler .assertCalledWith (method , UnsupportedOperationException .class );
218
-
236
+ // Assert
237
+ Awaitility .await ()
238
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
239
+ .atMost (500 , TimeUnit .MILLISECONDS )
240
+ .untilAsserted (() -> exceptionHandler .assertCalledWith (method , UnsupportedOperationException .class ));
219
241
ctx .close ();
220
242
}
221
243
222
244
@ Test
223
245
public void customExecutorBeanConfig () throws InterruptedException {
246
+ // Arrange
224
247
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
225
248
ctx .register (CustomExecutorBeanConfig .class , ExecutorPostProcessor .class );
226
249
ctx .refresh ();
227
-
228
250
AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
251
+ // Act
229
252
asyncBean .work ();
230
- Thread .sleep (500 );
253
+ // Assert
254
+ Awaitility .await ()
255
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
256
+ .atMost (500 , TimeUnit .MILLISECONDS )
257
+ .until (() -> asyncBean .getThreadOfExecution () != null );
231
258
assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Post-" ));
259
+ ctx .close ();
260
+ }
232
261
233
- TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler )
234
- ctx .getBean ("exceptionHandler" );
262
+ @ Test
263
+ public void customExecutorBeanConfigWithThrowsException () {
264
+ // Arrange
265
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
266
+ ctx .register (CustomExecutorBeanConfig .class , ExecutorPostProcessor .class );
267
+ ctx .refresh ();
268
+ AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
269
+ TestableAsyncUncaughtExceptionHandler exceptionHandler =
270
+ (TestableAsyncUncaughtExceptionHandler ) ctx .getBean ("exceptionHandler" );
235
271
assertFalse ("handler should not have been called yet" , exceptionHandler .isCalled ());
236
-
237
- asyncBean .fail ();
238
- Thread .sleep (500 );
239
272
Method method = ReflectionUtils .findMethod (AsyncBean .class , "fail" );
240
- exceptionHandler .assertCalledWith (method , UnsupportedOperationException .class );
241
-
273
+ // Act
274
+ asyncBean .fail ();
275
+ // Assert
276
+ Awaitility .await ()
277
+ .atMost (500 , TimeUnit .MILLISECONDS )
278
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
279
+ .untilAsserted (() -> exceptionHandler .assertCalledWith (method , UnsupportedOperationException .class ));
242
280
ctx .close ();
243
281
}
244
282
245
283
@ Test // SPR-14949
246
284
public void findOnInterfaceWithInterfaceProxy () throws InterruptedException {
285
+ // Arrange
247
286
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (Spr14949ConfigA .class );
248
-
249
287
AsyncInterface asyncBean = ctx .getBean (AsyncInterface .class );
288
+ // Act
250
289
asyncBean .work ();
251
- Thread .sleep (500 );
290
+ // Assert
291
+ Awaitility .await ()
292
+ .atMost (500 , TimeUnit .MILLISECONDS )
293
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
294
+ .until (() -> asyncBean .getThreadOfExecution () != null );
252
295
assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Custom-" ));
253
-
254
296
ctx .close ();
255
297
}
256
298
257
299
@ Test // SPR-14949
258
300
public void findOnInterfaceWithCglibProxy () throws InterruptedException {
301
+ // Arrange
259
302
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (Spr14949ConfigB .class );
260
-
261
303
AsyncInterface asyncBean = ctx .getBean (AsyncInterface .class );
304
+ // Act
262
305
asyncBean .work ();
263
- Thread .sleep (500 );
306
+ // Assert
307
+ Awaitility .await ()
308
+ .atMost (500 , TimeUnit .MILLISECONDS )
309
+ .pollInterval (10 , TimeUnit .MILLISECONDS )
310
+ .until (()-> asyncBean .getThreadOfExecution () != null );
264
311
assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Custom-" ));
265
-
266
312
ctx .close ();
267
313
}
268
314
@@ -390,7 +436,8 @@ public AsyncBean asyncBean() {
390
436
@ EnableAsync
391
437
static class AsyncConfigWithMockito {
392
438
393
- @ Bean @ Lazy
439
+ @ Bean
440
+ @ Lazy
394
441
public AsyncBean asyncBean () {
395
442
return Mockito .mock (AsyncBean .class );
396
443
}
0 commit comments