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