test/scheduler_0_test.rb in rufus-scheduler-1.0.5 vs test/scheduler_0_test.rb in rufus-scheduler-1.0.6

- old
+ new

@@ -13,349 +13,349 @@ # # testing otime and the scheduler # -class SchedulerTest < Test::Unit::TestCase +class Scheduler0Test < Test::Unit::TestCase - #def setup - #end + #def setup + #end - #def teardown - #end + #def teardown + #end - def test_scheduler_0 + def test_scheduler_0 - #Thread.abort_on_exception = true + #Thread.abort_on_exception = true - $var = nil + $var = nil - scheduler = OpenWFE::Scheduler.new - scheduler.sstart + scheduler = OpenWFE::Scheduler.new + scheduler.sstart - sid = scheduler.schedule_in('2s', :schedulable => TestSchedulable.new) + sid = scheduler.schedule_in('2s', :schedulable => TestSchedulable.new) - assert \ - sid, - "scheduler_0 did not return a job id" + assert \ + sid, + "scheduler_0 did not return a job id" - assert \ - (not $var), - "scheduler_0 is blocking but should not" + assert \ + (not $var), + "scheduler_0 is blocking but should not" - sleep 2.5 + sleep 2.5 - assert_equal 0, scheduler.cron_job_count - assert_equal 0, scheduler.every_job_count - assert_equal 0, scheduler.at_job_count - assert_equal 0, scheduler.pending_job_count + assert_equal 0, scheduler.cron_job_count + assert_equal 0, scheduler.every_job_count + assert_equal 0, scheduler.at_job_count + assert_equal 0, scheduler.pending_job_count - scheduler.sstop + scheduler.sstop - #puts ">#{$var}<" + #puts ">#{$var}<" - assert_equal "ok", $var + assert_equal "ok", $var + end + + def test_scheduler_1 + + $var = nil + + scheduler = OpenWFE::Scheduler.new + scheduler.sstart + + sid = scheduler.schedule_in('1s') do + $var = "ok..1" end - def test_scheduler_1 + assert \ + sid, + "scheduler_1 did not return a job id" - $var = nil + assert \ + (not $var), + "scheduler_1 is blocking but should not" - scheduler = OpenWFE::Scheduler.new - scheduler.sstart + sleep 2 + scheduler.sstop - sid = scheduler.schedule_in('1s') do - $var = "ok..1" - end + #puts ">#{$var}<" - assert \ - sid, - "scheduler_1 did not return a job id" + assert "ok..1", $var + end - assert \ - (not $var), - "scheduler_1 is blocking but should not" + # + # test idea by ara.t.howard on the ruby-talk ml + # + def test_scheduler_2 - sleep 2 - scheduler.sstop + text = "" - #puts ">#{$var}<" + scheduler = OpenWFE::Scheduler.new() + scheduler.sstart - assert "ok..1", $var + scheduler.schedule_in("1s") do + text << "one" + sleep(2) end + scheduler.schedule_in("1s") do + text << "two" + end - # - # test idea by ara.t.howard on the ruby-talk ml - # - def test_scheduler_2 + sleep(2) - text = "" + scheduler.sstop - scheduler = OpenWFE::Scheduler.new() - scheduler.sstart + #puts text - scheduler.schedule_in("1s") do - text << "one" - sleep(2) - end - scheduler.schedule_in("1s") do - text << "two" - end + assert_equal text, "onetwo" + end - sleep(2) + # + # Testing schedule_every() + # + def test_scheduler_4 - scheduler.sstop + Thread.abort_on_exception = true - #puts text + scheduler = OpenWFE::Scheduler.new() + scheduler.sstart - assert_equal text, "onetwo" - end - # - # Testing schedule_every() - # - def test_scheduler_4 + # phase 0 - Thread.abort_on_exception = true + count = 0 - scheduler = OpenWFE::Scheduler.new() - scheduler.sstart + job_id = scheduler.schedule_every("1s") do + count += 1 + end - # - # phase 0 + #puts "0 job_id : " + job_id.to_s - count = 0 + sleep 3.5 - job_id = scheduler.schedule_every("1s") do - count += 1 - end + assert_equal 3, count - #puts "0 job_id : " + job_id.to_s + job = scheduler.get_job job_id - sleep 3.5 + assert_equal "1s", job.schedule_info - assert_equal 3, count + assert_job_count scheduler, 0, 0, 1 - job = scheduler.get_job job_id + # + # phase 1 - assert_equal "1s", job.schedule_info + es = EverySchedulable.new - assert_job_count scheduler, 0, 0, 1 + job_id = scheduler.schedule_every "500", es - # - # phase 1 + #puts "1 job_id : " + job_id.to_s + + #sleep(3.4) # was a bit soonish for JRuby... + sleep 3.5 - es = EverySchedulable.new + #assert_job_count scheduler, 0, 0, 2 + assert_equal 6, es.count - job_id = scheduler.schedule_every "500", es + scheduler.unschedule job_id - #puts "1 job_id : " + job_id.to_s - - #sleep(3.4) # was a bit soonish for JRuby... - sleep 3.5 + sleep 1 - #assert_job_count scheduler, 0, 0, 2 - assert_equal 6, es.count + assert_equal 6, es.count - scheduler.unschedule job_id + # done - sleep 1 + scheduler.sstop + end - assert_equal 6, es.count + # + # testing to see if the scheduler immediately executes schedule_in(t) + # requests where t < scheduler.frequency. + # (100ms < 250ms) + # + def test_scheduler_5 - # done + scheduler = OpenWFE::Scheduler.new + scheduler.sstart - scheduler.sstop + touched = false + + scheduler.schedule_in "100" do + touched = true end - # - # testing to see if the scheduler immediately executes schedule_in(t) - # requests where t < scheduler.frequency. - # (100ms < 250ms) - # - def test_scheduler_5 + assert touched - scheduler = OpenWFE::Scheduler.new - scheduler.sstart + scheduler.sstop + end - touched = false + # + # Testing to see if a second job with the same id discards the first one. + # + def test_scheduler_6 - scheduler.schedule_in "100" do - touched = true - end + scheduler = OpenWFE::Scheduler.new + scheduler.start - assert touched + #class << scheduler + # attr_reader :pending_jobs + #end - scheduler.sstop + value = nil + + scheduler.schedule_in "3s", :job_id => "job" do + value = 0 end + scheduler.schedule_in "2s", :job_id => "job" do + value = 1 + end - # - # Testing to see if a second job with the same id discards the first one. - # - def test_scheduler_6 + sleep 0.5 - scheduler = OpenWFE::Scheduler.new - scheduler.start + #puts scheduler.pending_jobs.collect { |j| j.job_id }.inspect + assert_job_count scheduler, 0, 1, 0 - #class << scheduler - # attr_reader :pending_jobs - #end + assert_nil value - value = nil + sleep 2.0 - scheduler.schedule_in "3s", :job_id => "job" do - value = 0 - end - scheduler.schedule_in "2s", :job_id => "job" do - value = 1 - end + #puts scheduler.pending_jobs.collect { |j| j.job_id }.inspect + assert_job_count scheduler, 0, 0, 0 - sleep 0.5 + assert_equal 1, value - #puts scheduler.pending_jobs.collect { |j| j.job_id }.inspect - assert_job_count scheduler, 0, 1, 0 + sleep 4 - assert_nil value + assert_equal 1, value - sleep 2.0 + scheduler.stop + end - #puts scheduler.pending_jobs.collect { |j| j.job_id }.inspect - assert_job_count scheduler, 0, 0, 0 + # + # Testing custom precision. + # + def test_scheduler_7 - assert_equal 1, value + scheduler = Rufus::Scheduler.new(:scheduler_precision => 0.100) - sleep 4 + assert_equal 0.100, scheduler.precision + end - assert_equal 1, value + # + # Making sure that a job scheduled in the past is executed immediately + # and not scheduled. + # + # This test also makes sure that schedule_at() understands the + # time.to_s format. + # + def test_8 - scheduler.stop - end + scheduler = Rufus::Scheduler.new + scheduler.start - # - # Testing custom precision. - # - def test_scheduler_7 + var = false - scheduler = Rufus::Scheduler.new(:scheduler_precision => 0.100) - - assert_equal 0.100, scheduler.precision + job_id = scheduler.schedule_at Time.now.to_s do + var = true end - # - # Making sure that a job scheduled in the past is executed immediately - # and not scheduled. - # - # This test also makes sure that schedule_at() understands the - # time.to_s format. - # - def test_8 + assert_equal var, true + assert_nil job_id + end - scheduler = Rufus::Scheduler.new - scheduler.start + # + # Scheduling in the past, with :discard_past set to true. + # + def test_8b - var = false + scheduler = OpenWFE::Scheduler.new + scheduler.start - job_id = scheduler.schedule_at Time.now.to_s do - var = true - end + var = nil - assert_equal var, true - assert_nil job_id + job_id = scheduler.schedule_at(Time.now.to_s, :discard_past => true) do + var = "something" end - # - # Scheduling in the past, with :discard_past set to true. - # - def test_8b + assert_nil var + assert_nil job_id - scheduler = OpenWFE::Scheduler.new - scheduler.start + scheduler.stop + end - var = nil + # + # Testing restarting the scheduler. + # + def test_9 - job_id = scheduler.schedule_at(Time.now.to_s, :discard_past => true) do - var = "something" - end + scheduler = OpenWFE::Scheduler.new + scheduler.start - assert_nil var - assert_nil job_id + value = nil - scheduler.stop + scheduler.schedule_in "2s" do + value = 0 end - # - # Testing restarting the scheduler. - # - def test_9 + assert_nil value - scheduler = OpenWFE::Scheduler.new - scheduler.start + scheduler.stop - value = nil + sleep 0.5 - scheduler.schedule_in "2s" do - value = 0 - end + scheduler.start - assert_nil value + assert_nil value - scheduler.stop + sleep 2 - sleep 0.5 + assert_equal value, 0 - scheduler.start + scheduler.stop + end - assert_nil value + def test_10 - sleep 2 - - assert_equal value, 0 - - scheduler.stop + e = nil + begin + OpenWFE::Scheduler.new.precision = 10 + rescue Exception => e end - def test_10 + assert_not_nil e, "exception not caught" + end - e = nil - begin - OpenWFE::Scheduler.new.precision = 10 - rescue Exception => e - end + protected - assert_not_nil e, "exception not caught" + class TestSchedulable + include OpenWFE::Schedulable + + def trigger (params) + $var = "ok" + end end - protected + class EverySchedulable + include OpenWFE::Schedulable - class TestSchedulable - include OpenWFE::Schedulable + attr_accessor :job_id, :count - def trigger (params) - $var = "ok" - end - end + def initialize + @job_id = -1 + @count = 0 + end - class EverySchedulable - include OpenWFE::Schedulable + def trigger (params) + #puts "toto" + @count += 1 + end + end - attr_accessor :job_id, :count + def assert_job_count (scheduler, cron, at, every) - def initialize - @job_id = -1 - @count = 0 - end - - def trigger (params) - #puts "toto" - @count += 1 - end - end - - def assert_job_count (scheduler, cron, at, every) - - assert_equal cron, scheduler.cron_job_count - assert_equal at, scheduler.at_job_count - assert_equal every, scheduler.every_job_count - assert_equal at + every, scheduler.pending_job_count - end + assert_equal cron, scheduler.cron_job_count + assert_equal at, scheduler.at_job_count + assert_equal every, scheduler.every_job_count + assert_equal at + every, scheduler.pending_job_count + end end