# # Testing OpenWFE # # John Mettraux at openwfe.org # # Sun Oct 29 16:18:25 JST 2006 # require 'test/unit' #require 'openwfe/otime' require 'openwfe/util/scheduler' # # testing otime and the scheduler # class SchedulerTest < Test::Unit::TestCase #def setup #end #def teardown #end def test_scheduler_0 $var = nil scheduler = OpenWFE::Scheduler.new() scheduler.sstart sid = scheduler.schedule_in('2s', TestSchedulable.new, nil) assert \ sid, "scheduler_0 did not return a job id" assert \ (not $var), "scheduler_0 is blocking but should not" sleep(2.5) scheduler.sstop #puts ">#{$var}<" assert \ ($var == "ok"), "scheduler_0 didn't work" end def test_scheduler_1 $var = nil scheduler = OpenWFE::Scheduler.new() scheduler.sstart sid = scheduler.schedule_in('1s') do $var = "ok..1" end assert \ sid, "scheduler_1 did not return a job id" assert \ (not $var), "scheduler_1 is blocking but should not" sleep(2) scheduler.sstop #puts ">#{$var}<" assert \ ($var == "ok..1"), "scheduler_1 didn't work" end # # test idea by ara.t.howard on the ruby-talk ml # def test_scheduler_2 text = "" scheduler = OpenWFE::Scheduler.new() scheduler.sstart scheduler.schedule_in("1s") do text << "one" sleep(2) end scheduler.schedule_in("1s") do text << "two" end sleep(2) scheduler.sstop #puts text assert text == "onetwo" end # # test Scheduler::is_cron_string(s) # def test_scheduler_3 assert OpenWFE::Scheduler.is_cron_string("* * * * *") assert OpenWFE::Scheduler.is_cron_string("10/2 * * * *") assert (not OpenWFE::Scheduler.is_cron_string("10d10m")) end # # Testing schedule_every() # def test_scheduler_4 scheduler = OpenWFE::Scheduler.new() scheduler.sstart # # phase 0 count = 0 scheduler.schedule_every("1s") do count += 1 end sleep(3.5) assert_equal count, 3 # # phase 1 es = EverySchedulable.new job_id = scheduler.schedule_every("500", es, nil) sleep(3.2) assert_equal es.count, 6 scheduler.unschedule(job_id) sleep(1) assert_equal es.count, 6 # done scheduler.sstop end protected class TestSchedulable include OpenWFE::Schedulable def trigger (params) $var = "ok" end end class EverySchedulable include OpenWFE::Schedulable attr_accessor :job_id, :count def initialize @job_id = -1 @count = 0 end def trigger (params) #puts "toto" @count += 1 end end end