TaskSchedulingAutoConfigurationTests Class — spring-boot Architecture
Architecture documentation for the TaskSchedulingAutoConfigurationTests class in TaskSchedulingAutoConfigurationTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java lines 63–375
class TaskSchedulingAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(TestConfiguration.class)
.withConfiguration(AutoConfigurations.of(TaskSchedulingAutoConfiguration.class));
@Test
void noSchedulingDoesNotExposeTaskScheduler() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(TaskScheduler.class));
}
@Test
void noSchedulingDoesNotExposeScheduledBeanLazyInitializationExcludeFilter() {
this.contextRunner
.run((context) -> assertThat(context).doesNotHaveBean(ScheduledBeanLazyInitializationExcludeFilter.class));
}
@Test
void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(ThreadPoolTaskSchedulerBuilder.class);
assertThat(context).hasSingleBean(ThreadPoolTaskScheduler.class);
});
}
@Test
void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
this.contextRunner
.withPropertyValues("spring.task.scheduling.shutdown.await-termination=true",
"spring.task.scheduling.shutdown.await-termination-period=30s",
"spring.task.scheduling.thread-name-prefix=scheduling-test-")
.withUserConfiguration(SchedulingConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(TaskExecutor.class);
TaskExecutor taskExecutor = context.getBean(TaskExecutor.class);
TestBean bean = context.getBean(TestBean.class);
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(taskExecutor).hasFieldOrPropertyWithValue("waitForTasksToCompleteOnShutdown", true);
assertThat(taskExecutor).hasFieldOrPropertyWithValue("awaitTerminationMillis", 30000L);
assertThat(bean.threadNames).allMatch((name) -> name.contains("scheduling-test-"));
});
}
@Test
void simpleAsyncTaskSchedulerBuilderShouldReadProperties() {
this.contextRunner
.withPropertyValues("spring.task.scheduling.simple.concurrency-limit=1",
"spring.task.scheduling.thread-name-prefix=scheduling-test-",
"spring.task.scheduling.shutdown.await-termination=true",
"spring.task.scheduling.shutdown.await-termination-period=30s")
.withUserConfiguration(SchedulingConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
assertThat(builder).hasFieldOrPropertyWithValue("threadNamePrefix", "scheduling-test-");
assertThat(builder).hasFieldOrPropertyWithValue("concurrencyLimit", 1);
assertThat(builder).hasFieldOrPropertyWithValue("taskTerminationTimeout", Duration.ofSeconds(30));
});
}
@Test
@EnabledForJreRange(min = JRE.JAVA_21)
void simpleAsyncTaskSchedulerBuilderShouldUseVirtualThreadsIfEnabled() {
this.contextRunner.withPropertyValues("spring.threads.virtual.enabled=true")
.withUserConfiguration(SchedulingConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
assertThat(builder).hasFieldOrPropertyWithValue("virtualThreads", true);
});
}
@Test
@EnabledForJreRange(min = JRE.JAVA_21)
void simpleAsyncTaskSchedulerBuilderShouldUsePlatformThreadsByDefault() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
assertThat(builder).hasFieldOrPropertyWithValue("virtualThreads", null);
});
}
@Test
void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
.withBean(TaskDecorator.class, OrderedTaskDecorator::new)
.run((context) -> {
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
assertThat(context).hasSingleBean(TaskDecorator.class);
TaskDecorator taskDecorator = context.getBean(TaskDecorator.class);
SimpleAsyncTaskScheduler scheduler = context.getBean(SimpleAsyncTaskSchedulerBuilder.class).build();
assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator);
});
}
@Test
void simpleAsyncTaskSchedulerBuilderShouldApplyCompositeTaskDecorator() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
.withBean("taskDecorator1", TaskDecorator.class, () -> new OrderedTaskDecorator(1))
.withBean("taskDecorator2", TaskDecorator.class, () -> new OrderedTaskDecorator(3))
.withBean("taskDecorator3", TaskDecorator.class, () -> new OrderedTaskDecorator(2))
.run((context) -> {
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
SimpleAsyncTaskScheduler scheduler = context.getBean(SimpleAsyncTaskSchedulerBuilder.class).build();
assertThat(scheduler).extracting("taskDecorator")
.isInstanceOf(CompositeTaskDecorator.class)
.extracting("taskDecorators")
.asInstanceOf(InstanceOfAssertFactories.list(TaskDecorator.class))
.containsExactly(context.getBean("taskDecorator1", TaskDecorator.class),
context.getBean("taskDecorator3", TaskDecorator.class),
context.getBean("taskDecorator2", TaskDecorator.class));
});
}
@Test
void threadPoolTaskSchedulerBuilderShouldApplyTaskDecorator() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
.withBean(TaskDecorator.class, OrderedTaskDecorator::new)
.run((context) -> {
assertThat(context).hasSingleBean(ThreadPoolTaskSchedulerBuilder.class);
assertThat(context).hasSingleBean(TaskDecorator.class);
TaskDecorator taskDecorator = context.getBean(TaskDecorator.class);
ThreadPoolTaskScheduler scheduler = context.getBean(ThreadPoolTaskSchedulerBuilder.class).build();
assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator);
});
}
@Test
void threadPoolTaskSchedulerBuilderShouldApplyCompositeTaskDecorator() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
.withBean("taskDecorator1", TaskDecorator.class, () -> new OrderedTaskDecorator(1))
.withBean("taskDecorator2", TaskDecorator.class, () -> new OrderedTaskDecorator(3))
.withBean("taskDecorator3", TaskDecorator.class, () -> new OrderedTaskDecorator(2))
.run((context) -> {
assertThat(context).hasSingleBean(ThreadPoolTaskSchedulerBuilder.class);
ThreadPoolTaskScheduler scheduler = context.getBean(ThreadPoolTaskSchedulerBuilder.class).build();
assertThat(scheduler).extracting("taskDecorator")
.isInstanceOf(CompositeTaskDecorator.class)
.extracting("taskDecorators")
.asInstanceOf(InstanceOfAssertFactories.list(TaskDecorator.class))
.containsExactly(context.getBean("taskDecorator1", TaskDecorator.class),
context.getBean("taskDecorator3", TaskDecorator.class),
context.getBean("taskDecorator2", TaskDecorator.class));
});
}
@Test
void simpleAsyncTaskSchedulerBuilderShouldApplyCustomizers() {
SimpleAsyncTaskSchedulerCustomizer customizer = (scheduler) -> {
};
this.contextRunner.withBean(SimpleAsyncTaskSchedulerCustomizer.class, () -> customizer)
.withUserConfiguration(SchedulingConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
assertThat(builder).extracting("customizers")
.asInstanceOf(InstanceOfAssertFactories.collection(SimpleAsyncTaskSchedulerCustomizer.class))
.containsExactly(customizer);
});
}
@Test
void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")
.withUserConfiguration(SchedulingConfiguration.class, ThreadPoolTaskSchedulerCustomizerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(TaskExecutor.class);
TestBean bean = context.getBean(TestBean.class);
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(bean.threadNames).allMatch((name) -> name.contains("customized-scheduler-"));
});
}
@Test
void enableSchedulingWithExistingTaskSchedulerBacksOff() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, TaskSchedulerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(TaskScheduler.class);
assertThat(context.getBean(TaskScheduler.class)).isInstanceOf(TestTaskScheduler.class);
TestBean bean = context.getBean(TestBean.class);
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(bean.threadNames).containsExactly("test-1");
});
}
@Test
void enableSchedulingWithExistingScheduledExecutorServiceBacksOff() {
this.contextRunner
.withUserConfiguration(SchedulingConfiguration.class, ScheduledExecutorServiceConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean(TaskScheduler.class);
assertThat(context).hasSingleBean(ScheduledExecutorService.class);
TestBean bean = context.getBean(TestBean.class);
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(bean.threadNames).allMatch((name) -> name.contains("pool-"));
});
}
@Test
void enableSchedulingWithLazyInitializationInvokeScheduledMethods() {
List<String> threadNames = new ArrayList<>();
new ApplicationContextRunner()
.withInitializer(
(context) -> context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()))
.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")
.withBean(LazyTestBean.class, () -> new LazyTestBean(threadNames))
.withUserConfiguration(SchedulingConfiguration.class)
.withConfiguration(AutoConfigurations.of(TaskSchedulingAutoConfiguration.class))
.run((context) -> {
// No lazy lookup.
Awaitility.waitAtMost(Duration.ofSeconds(3)).until(() -> !threadNames.isEmpty());
assertThat(threadNames).allMatch((name) -> name.contains("scheduling-test-"));
});
}
@Configuration(proxyBeanMethods = false)
@EnableScheduling
static class SchedulingConfiguration {
}
@Configuration(proxyBeanMethods = false)
static class TaskSchedulerConfiguration {
@Bean
TaskScheduler customTaskScheduler() {
return new TestTaskScheduler();
}
}
@Configuration(proxyBeanMethods = false)
static class ScheduledExecutorServiceConfiguration {
@Bean
ScheduledExecutorService customScheduledExecutorService() {
return Executors.newScheduledThreadPool(2);
}
}
@Configuration(proxyBeanMethods = false)
static class ThreadPoolTaskSchedulerCustomizerConfiguration {
@Bean
ThreadPoolTaskSchedulerCustomizer testTaskSchedulerCustomizer() {
return ((taskScheduler) -> taskScheduler.setThreadNamePrefix("customized-scheduler-"));
}
}
@Configuration(proxyBeanMethods = false)
static class SchedulingConfigurerConfiguration implements SchedulingConfigurer {
private final TaskScheduler taskScheduler = new TestTaskScheduler();
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(this.taskScheduler);
}
}
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
TestBean testBean() {
return new TestBean();
}
}
static class TestBean {
private final Set<String> threadNames = ConcurrentHashMap.newKeySet();
private final CountDownLatch latch = new CountDownLatch(1);
@Scheduled(fixedRate = 60000)
void accumulate() {
this.threadNames.add(Thread.currentThread().getName());
this.latch.countDown();
}
}
static class LazyTestBean {
private final List<String> threadNames;
LazyTestBean(List<String> threadNames) {
this.threadNames = threadNames;
}
@Scheduled(fixedRate = 2000)
void accumulate() {
this.threadNames.add(Thread.currentThread().getName());
}
}
static class TestTaskScheduler extends ThreadPoolTaskScheduler {
TestTaskScheduler() {
setPoolSize(1);
setThreadNamePrefix("test-");
afterPropertiesSet();
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free