简体   繁体   English

如何测试预定的作业(每分钟,cron“* * * * *”),但不等待“真实”一分钟(rufus-scheduler vs 虚拟时间)?

[英]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 rufus-scheduler lib 允许我们安排任务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.事实上,当你想在rufus-scheduler之上测试你的应用程序时,你需要real 1 min睡眠real 1 min ,这对于典型的测试来说太多了。

For example, in Reactor(Java) they provide StepVerifier.withVirtualTime in order to avoid long-running tests.`:例如,在Reactor(Java)他们提供StepVerifier.withVirtualTime以避免长时间运行的测试。`:

Are there any options for time manipulation within tests for rufus-scheduler or Ruby itself?rufus-schedulerRuby本身的测试中是否有任何时间操作选项?

Current solution当前解决方案

rufus-scheduler has type in , thus for verification of app skeleton I'm using in=0.001s and sleep 0.4 . rufus-scheduler有类型in ,因此为了验证应用程序框架,我使用in=0.001ssleep 0.4

Yes, it takes half-of a second in the test but this is much better than 1 minute.是的,测试需要半秒,但这比 1 分钟要好得多。

Have a look at看一下

or other "time travel" tools.或其他“时间旅行”工具。

I second @spickermann on "only test your own code".我第二次@spickermann 说“只测试你自己的代码”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM