Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NoOpTaskScheduler for disabling @Scheduled tasks in test setups #28073

Closed
membersound opened this issue Feb 17, 2022 · 7 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@membersound
Copy link

membersound commented Feb 17, 2022

If an application defines a @Scheduled(fixedRateString = "PT6H") tasks, then this task will be running on any @SpringBootTest junit test that does not explicit mock the bean containing the scheduled task.

@Service
public class SchedulerService {
    @Scheduled(fixedRateString = "PT6H")
    public void runOnStartup() {

   }
}

This gets tedious if there are many more @Scheduled tasks in the application that launch on startup by usage of fixedRate or fixedRateString.

It would be great if there could be a possibility to completely deactivate the execution of @Scheduled tasks in junit.

Just like it is possible to deactivate any @Cacheable annotation by simply setting spring.cache.type=none dring junit tests.

Something like: spring.enable.scheduling=false (default: true)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 17, 2022
@bclozel bclozel transferred this issue from spring-projects/spring-boot Feb 18, 2022
@bclozel bclozel added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Feb 18, 2022
@wilkinsona
Copy link
Member

Boot has no control over whether or not scheduling is enabled as it's done in your code via @EnableScheduling. If you'd like a global property that can override @EnableScheduling (similar to Spring Framework's spring.jndi.ignore) this would have to be a Framework change. We've transferred your issue for the Framework team's consideration.

In the meantime, you could structure your code so that your @EnableScheduling-annotated class can be excluded when running your tests. One way to achieve that would be to use something like@Profile("!test") on the class that enables scheduling and then activate the test profile in your tests where you do not want scheduling to be enabled.

@sbrannen sbrannen changed the title Configuration to disable @Scheduled tasks during Junit tests Introduce configuration to disable @Scheduled tasks during tests Feb 18, 2022
@bclozel bclozel changed the title Introduce configuration to disable @Scheduled tasks during tests Introduce configuration to disable @Scheduled tasks Feb 18, 2022
@Drezir
Copy link
Contributor

Drezir commented Feb 21, 2022

I have tried to create this, please look at the patch I am sending. Thank you
Option_to_disable_scheduling.patch.zip
.

Leewei222 added a commit to Leewei222/spring-framework that referenced this issue Aug 23, 2023
Prior to this commit, only cron @scheduled tasks can be disabled with the
special cron expression value that indicates a disabled trigger: "-".

This commit enables this configuration to fixedDelay and fixedRate @scheduled
tasks.

Fixes spring-projectsgh-28073
@snicoll
Copy link
Member

snicoll commented Sep 29, 2023

Spring Framework has support for a no-op cache so I wonder if offering a no-op TaskScheduler would be a way to provide a consistent support here.

@wilkinsona if we did that, would you be ok to introduce a similar support in Spring Boot? I am asking as the example of caching is similar and yet Spring Boot is able to provide such a feature. The only difference is the lack of a no-op implementation.

@wilkinsona
Copy link
Member

That sounds fine, I think. I guess we'd do it through a new spring.task.scheduling.* property that auto-configures a no-op TaskScheduler instead of the existing one.

@snicoll
Copy link
Member

snicoll commented Sep 29, 2023

paging @jhoeller to see what he thinks of that idea.

@jhoeller jhoeller removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 21, 2023
@jhoeller jhoeller added this to the 6.1.3 milestone Dec 21, 2023
@jhoeller jhoeller self-assigned this Dec 21, 2023
@jhoeller jhoeller changed the title Introduce configuration to disable @Scheduled tasks Introduce NoOpTaskScheduler to disable @Scheduled tasks Dec 21, 2023
@jhoeller jhoeller changed the title Introduce NoOpTaskScheduler to disable @Scheduled tasks Introduce NoOpTaskScheduler for disabling @Scheduled tasks in test setups Dec 21, 2023
@jhoeller
Copy link
Contributor

Introduced NoOpTaskScheduler in org.springframework.scheduling.support now, to be picked up by Spring Boot for a new spring.task.scheduling.* property as suggested above (@wilkinsona @snicoll).

@wilkinsona
Copy link
Member

Thanks, @jhoeller. I've opened spring-projects/spring-boot#38954 on the Boot side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants