Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some strategies to unit test a scheduler?

This post started out as "What are some common patterns in unit testing multi-threaded code ?", but I found some other discussions on SO that generally agreed that "It is Hard (TM)" and "It Depends (TM)". So I thought that reducing the scope of the question would be more useful.

Background : We are implementing a simple scheduler that gives you a way to register callbacks when starting and stopping jobs and of course configure the frequency of scheduling. Currently, we're making a lightweight wrapper around java.util.Timer.

Aspects:

  • I haven't found a way to test this scheduler by relying on only public interfaces (something like addJob(jobSchedule, jobArgs,jobListener) , removeJob(jobId)).

  • How do I time the fact that the the job was called according to the schedule specified ?

like image 906
StudioEvoque Avatar asked May 31 '09 08:05

StudioEvoque


People also ask

What is the testing strategy used for unit testing?

Unit Testing Techniques:Black Box Testing - Using which the user interface, input and output are tested. White Box Testing - used to test each one of those functions behaviour is tested. Gray Box Testing - Used to execute tests, risks and assessment methods.

What are the two types of unit testing techniques?

There are 2 types of Unit Testing: Manual, and Automated.

How do I test a scheduled spring boot?

How do I test @Scheduled job tasks in my spring-boot application? What do you want to test exactly? If you want to test that work() does what it's supposed to do, you can test it like any other method of any other bean: you create an instance of the bean, call the method, and test that it does what it's supposed to do.


2 Answers

you could use a recorder object that record the order, timings and other useful stuff in each unit test of your scheduler. The test is simple:

  1. create a recorder object
  2. configure the schedule
  3. execute a unit test
  4. check that recorder object is "compatible" with the schedule
like image 112
dfa Avatar answered Oct 06 '22 22:10

dfa


One thing also to remember is that you don't need to test that Timer works. You can write a mock version of Timer (by extending the class or using EasyMock) that simply checks that you are calling it correctly, possibly even replacing enough that you don't need threads at all. In this case that might be more work than needed if your job listener has enough callbacks to track the scheduler.

The other important thing to remember is that when testing the scheduler, use custom jobs that track how the scheduler is working; when testing scheduled jobs, call the callbacks directly and not through the scheduler. You may have a higher level integration test that checks both together, depending on the system.

like image 37
Kathy Van Stone Avatar answered Oct 07 '22 00:10

Kathy Van Stone