spring-guides / gs-scheduling-tasks

Scheduling Tasks :: Learn how to schedule tasks with Spring.
https://spring.io/guides/gs/scheduling-tasks/
Apache License 2.0
132 stars 164 forks source link

Testing schedulers #10

Closed Sebastian-Pietrzak closed 7 years ago

Sebastian-Pietrzak commented 7 years ago

It would be interesting to see, how we could actually properly test the schedulers (I mean without using Thread.sleep(x) :) ). Whether there's at least good configuration somehow from the context or anything alike.

gregturn commented 7 years ago

I'm not sold on that.

I grok testing what happens when it goes off, but the timer stuff itself borders on testing framework code.

I suppose you could set up an app context and induce the annotation that way, with the results mocked. But it sounds like a ceremony to simply verify @EnableScheduling and @Scheduled were put in the code.

Sebastian-Pietrzak commented 7 years ago

I see your point, I'm not trying to test internals, rather test that I use it correctly. What's my concern here is - what happens if I add some library to my service, which plays with context, resulting in @Scheduled not invoking my code. How should I prevent from that?

gregturn commented 7 years ago

At some point, you have to push things into production and let it run. You can test using alternative scheduled times. Like, if you want a task to run every 24 hours, a "test" would shorten that to something like 5 seconds. So the question arises, "How do I know it will run in 24 hours?" Simple answer, you don't. You can craft the unit test for 5 seconds, but do you critically need that? I think automating a 24-hour test is impractical.

The alternative would be to wire up something where if it DOESN'T fire at the expected interval, you generate a production alarm. I worked on real time control systems in grad school, and a low priority process cranked out a pulse signal. If two clock cycles passed without this signal, hardware would shut everything down. This was a fail-fast designed to ensure no one process gobbled up the clock.

You can think in similar terms for your own needs. In the meantime, I'm going to close this issue.

Sebastian-Pietrzak commented 7 years ago

True, there are some things we don't overcome or are too much effort for the value. In the end in terms of critical stuff, I thought of something similar as you mentioned (2nd option), rather than not having anything. Thanks for your thoughts anyway :)