|
19 | 19 | import java.util.concurrent.CompletableFuture;
|
20 | 20 | import java.util.concurrent.CountDownLatch;
|
21 | 21 | import java.util.concurrent.Executor;
|
22 |
| -import java.util.concurrent.ExecutorService; |
23 |
| -import java.util.concurrent.Executors; |
24 | 22 | import java.util.concurrent.Future;
|
25 | 23 | import java.util.concurrent.TimeUnit;
|
26 | 24 | import java.util.concurrent.atomic.AtomicReference;
|
@@ -381,42 +379,66 @@ void enableAsyncUsesAutoConfiguredOneByDefaultEvenThoughSchedulingIsConfigured()
|
381 | 379 | }
|
382 | 380 |
|
383 | 381 | @Test
|
384 |
| - void shouldAliasApplicationExecutorToBootstrapExecutor() { |
| 382 | + void shouldAliasApplicationTaskExecutorToBootstrapExecutor() { |
385 | 383 | this.contextRunner.run((context) -> {
|
386 |
| - String[] aliases = context.getAliases("applicationTaskExecutor"); |
387 |
| - assertThat(aliases).containsExactly("bootstrapExecutor"); |
| 384 | + assertThat(context).hasSingleBean(Executor.class) |
| 385 | + .hasBean("applicationTaskExecutor") |
| 386 | + .hasBean("bootstrapExecutor"); |
| 387 | + assertThat(context.getAliases("applicationTaskExecutor")).containsExactly("bootstrapExecutor"); |
| 388 | + assertThat(context.getBean("bootstrapExecutor")).isSameAs(context.getBean("applicationTaskExecutor")); |
388 | 389 | });
|
389 | 390 | }
|
390 | 391 |
|
391 | 392 | @Test
|
392 |
| - void shouldNotAliasIfBootstrapExecutorIsDefined() { |
393 |
| - ExecutorService executor = Executors.newSingleThreadExecutor(); |
394 |
| - try { |
395 |
| - this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> executor) |
396 |
| - .withBean("bootstrapExecutor", Executor.class, () -> executor) |
397 |
| - .run((context) -> { |
398 |
| - assertThat(context).hasBean("applicationTaskExecutor"); |
399 |
| - String[] aliases = context.getAliases("applicationTaskExecutor"); |
400 |
| - assertThat(aliases).isEmpty(); |
401 |
| - }); |
402 |
| - } |
403 |
| - finally { |
404 |
| - executor.shutdownNow(); |
405 |
| - } |
| 393 | + void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorIsDefined() { |
| 394 | + this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> createCustomAsyncExecutor("app-")) |
| 395 | + .withBean("bootstrapExecutor", Executor.class, () -> createCustomAsyncExecutor("bootstrap-")) |
| 396 | + .run((context) -> { |
| 397 | + assertThat(context.getBeansOfType(Executor.class)).hasSize(2); |
| 398 | + assertThat(context).hasBean("applicationTaskExecutor").hasBean("bootstrapExecutor"); |
| 399 | + assertThat(context.getAliases("applicationTaskExecutor")).isEmpty(); |
| 400 | + assertThat(context.getBean("bootstrapExecutor")) |
| 401 | + .isNotSameAs(context.getBean("applicationTaskExecutor")); |
| 402 | + }); |
| 403 | + } |
| 404 | + |
| 405 | + @Test |
| 406 | + void shouldNotAliasApplicationTaskExecutorWhenApplicationTaskExecutorIsMissing() { |
| 407 | + this.contextRunner.withBean("customExecutor", Executor.class, () -> createCustomAsyncExecutor("custom-")) |
| 408 | + .run((context) -> assertThat(context).hasSingleBean(Executor.class) |
| 409 | + .hasBean("customExecutor") |
| 410 | + .doesNotHaveBean("applicationTaskExecutor") |
| 411 | + .doesNotHaveBean("bootstrapExecutor")); |
| 412 | + } |
| 413 | + |
| 414 | + @Test |
| 415 | + void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorRegisteredAsSingleton() { |
| 416 | + this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> createCustomAsyncExecutor("app-")) |
| 417 | + .withInitializer((context) -> context.getBeanFactory() |
| 418 | + .registerSingleton("bootstrapExecutor", createCustomAsyncExecutor("bootstrap-"))) |
| 419 | + .run((context) -> { |
| 420 | + assertThat(context.getBeansOfType(Executor.class)).hasSize(2); |
| 421 | + assertThat(context).hasBean("applicationTaskExecutor").hasBean("bootstrapExecutor"); |
| 422 | + assertThat(context.getAliases("applicationTaskExecutor")).isEmpty(); |
| 423 | + assertThat(context.getBean("bootstrapExecutor")) |
| 424 | + .isNotSameAs(context.getBean("applicationTaskExecutor")); |
| 425 | + }); |
406 | 426 | }
|
407 | 427 |
|
408 | 428 | @Test
|
409 |
| - void shouldNotAliasIfApplicationTaskExecutorIsMissing() { |
410 |
| - ExecutorService executor = Executors.newSingleThreadExecutor(); |
411 |
| - try { |
412 |
| - this.contextRunner.withBean("customExecutor", Executor.class, () -> executor).run((context) -> { |
413 |
| - assertThat(context).doesNotHaveBean("applicationTaskExecutor"); |
414 |
| - assertThat(context).doesNotHaveBean("bootstrapExecutor"); |
| 429 | + void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorAliasIsDefined() { |
| 430 | + Executor executor = Runnable::run; |
| 431 | + this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> executor) |
| 432 | + .withBean("customExecutor", Executor.class, () -> createCustomAsyncExecutor("custom")) |
| 433 | + .withInitializer((context) -> context.getBeanFactory().registerAlias("customExecutor", "bootstrapExecutor")) |
| 434 | + .run((context) -> { |
| 435 | + assertThat(context.getBeansOfType(Executor.class)).hasSize(2); |
| 436 | + assertThat(context).hasBean("applicationTaskExecutor").hasBean("customExecutor"); |
| 437 | + assertThat(context.getAliases("applicationTaskExecutor")).isEmpty(); |
| 438 | + assertThat(context.getAliases("customExecutor")).contains("bootstrapExecutor"); |
| 439 | + assertThat(context.getBean("bootstrapExecutor")).isNotSameAs(context.getBean("applicationTaskExecutor")) |
| 440 | + .isSameAs(context.getBean("customExecutor")); |
415 | 441 | });
|
416 |
| - } |
417 |
| - finally { |
418 |
| - executor.shutdownNow(); |
419 |
| - } |
420 | 442 | }
|
421 | 443 |
|
422 | 444 | private Executor createCustomAsyncExecutor(String threadNamePrefix) {
|
|
0 commit comments