Search code examples
rubyunit-testingtimerufus-scheduler

How to test jobs which were scheduled (each one min, cron "* * * * *"), but not wait "real" one minute (rufus-scheduler vs virtual time)?


rufus-scheduler lib allows us to schedule tasks https://github.com/jmettraux/rufus-scheduler

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

scheduler.cron '* * * * *' do
  # do something every minute
end

In fact, when you want to test your app on top of rufus-scheduler you will need to sleep real 1 min which is too much for typical tests.

For example, in Reactor(Java) they provide StepVerifier.withVirtualTime in order to avoid long-running tests.`:

Are there any options for time manipulation within tests for rufus-scheduler or Ruby itself?

Current solution

rufus-scheduler has type in, thus for verification of app skeleton I'm using in=0.001s and sleep 0.4.

Yes, it takes half-of a second in the test but this is much better than 1 minute.


Solution

  • Have a look at

    or other "time travel" tools.

    I second @spickermann on "only test your own code".