spec/scheduler_spec.rb in rufus-scheduler-3.0.7 vs spec/scheduler_spec.rb in rufus-scheduler-3.0.8

- old
+ new

@@ -18,44 +18,46 @@ t = Thread.list.find { |t| t[:name] == "rufus_scheduler_#{scheduler.object_id}_scheduler" } - t[:rufus_scheduler].should == scheduler + expect(t[:rufus_scheduler]).to eq(scheduler) end it 'sets :name and :rufus_scheduler in the scheduler thread local vars' do scheduler = Rufus::Scheduler.new - scheduler.thread[:name].should == + expect(scheduler.thread[:name]).to eq( "rufus_scheduler_#{scheduler.object_id}_scheduler" - scheduler.thread[:rufus_scheduler].should == + ) + expect(scheduler.thread[:rufus_scheduler]).to eq( scheduler + ) end it 'accepts a :frequency => integer option' do scheduler = Rufus::Scheduler.new(:frequency => 2) - scheduler.frequency.should == 2 + expect(scheduler.frequency).to eq(2) end it 'accepts a :frequency => "2h1m" option' do scheduler = Rufus::Scheduler.new(:frequency => '2h1m') - scheduler.frequency.should == 3600 * 2 + 60 + expect(scheduler.frequency).to eq(3600 * 2 + 60) end it 'accepts a :thread_name option' do scheduler = Rufus::Scheduler.new(:thread_name => 'oliphant') t = Thread.list.find { |t| t[:name] == 'oliphant' } - t[:rufus_scheduler].should == scheduler + expect(t[:rufus_scheduler]).to eq(scheduler) end #it 'accepts a :min_work_threads option' do # scheduler = Rufus::Scheduler.new(:min_work_threads => 9) # scheduler.min_work_threads.should == 9 @@ -63,11 +65,11 @@ it 'accepts a :max_work_threads option' do scheduler = Rufus::Scheduler.new(:max_work_threads => 9) - scheduler.max_work_threads.should == 9 + expect(scheduler.max_work_threads).to eq(9) end end before :each do @scheduler = Rufus::Scheduler.new @@ -83,21 +85,21 @@ j = nil job = @scheduler.schedule_in('0s') { |jj| j = jj } sleep 0.4 - j.should == job + expect(j).to eq(job) end it 'passes the trigger time as second block argument' do t = nil @scheduler.schedule_in('0s') { |jj, tt| t = tt } sleep 0.4 - t.class.should == Time + expect(t.class).to eq(Time) end class MyHandler attr_reader :counter def initialize @@ -114,11 +116,11 @@ @scheduler.schedule_in('0s', mh) sleep 0.4 - mh.counter.should == 1 + expect(mh.counter).to eq(1) end class MyOtherHandler attr_reader :counter def initialize @@ -133,11 +135,11 @@ job = @scheduler.schedule_in('0s', MyOtherHandler.new) sleep 0.4 - job.handler.counter.should == 1 + expect(job.handler.counter).to eq(1) end it 'accepts a class as callable' do job = @@ -148,20 +150,20 @@ end end) sleep 0.4 - job.handler.value.should == 7 + expect(job.handler.value).to eq(7) end it 'raises if the scheduler is shutting down' do @scheduler.shutdown - lambda { + expect { @scheduler.in('0s') { puts 'hhhhhhhhhhhello!!' } - }.should raise_error(Rufus::Scheduler::NotRunningError) + }.to raise_error(Rufus::Scheduler::NotRunningError) end end describe '#in / #at' do @@ -194,69 +196,69 @@ it 'accepts a duration and schedules an InJob' do j = @scheduler.schedule '1s' do; end - j.class.should == Rufus::Scheduler::InJob - j.original.should == '1s' + expect(j.class).to eq(Rufus::Scheduler::InJob) + expect(j.original).to eq('1s') end it 'accepts a point in time and schedules an AtJob' do j = @scheduler.schedule '2070/12/24 23:00' do; end - j.class.should == Rufus::Scheduler::AtJob - j.next_time.strftime('%Y %m %d').should == '2070 12 24' + expect(j.class).to eq(Rufus::Scheduler::AtJob) + expect(j.next_time.strftime('%Y %m %d')).to eq('2070 12 24') end it 'accepts a cron string and schedules a CronJob' do j = @scheduler.schedule '* * * * *' do; end - j.class.should == Rufus::Scheduler::CronJob + expect(j.class).to eq(Rufus::Scheduler::CronJob) end end describe '#repeat' do it 'accepts a duration and schedules an EveryJob' do j = @scheduler.repeat '1s' do; end - j.class.should == Rufus::Scheduler::EveryJob + expect(j.class).to eq(Rufus::Scheduler::EveryJob) end it 'accepts a cron string and schedules a CronJob' do j = @scheduler.repeat '* * * * *' do; end - j.class.should == Rufus::Scheduler::CronJob + expect(j.class).to eq(Rufus::Scheduler::CronJob) end end describe '#unschedule(job_or_work_id)' do it 'accepts job ids' do job = @scheduler.schedule_in '10d' do; end - job.unscheduled_at.should == nil + expect(job.unscheduled_at).to eq(nil) @scheduler.unschedule(job.id) - job.unscheduled_at.should_not == nil + expect(job.unscheduled_at).not_to eq(nil) end it 'accepts jobs' do job = @scheduler.schedule_in '10d' do; end - job.unscheduled_at.should == nil + expect(job.unscheduled_at).to eq(nil) @scheduler.unschedule(job) - job.unscheduled_at.should_not == nil + expect(job.unscheduled_at).not_to eq(nil) end it 'carefully unschedules repeat jobs' do counter = 0 @@ -270,62 +272,62 @@ c = counter @scheduler.unschedule(job) sleep 1.5 - counter.should == c + expect(counter).to eq(c) end end describe '#uptime' do it 'returns the uptime as a float' do - @scheduler.uptime.should >= 0.0 + expect(@scheduler.uptime).to be >= 0.0 end end describe '#uptime_s' do it 'returns the uptime as a human readable string' do sleep 1 - @scheduler.uptime_s.should match(/^[12]s\d+$/) + expect(@scheduler.uptime_s).to match(/^[12]s\d+$/) end end describe '#join' do it 'joins the scheduler thread' do t = Thread.new { @scheduler.join; Thread.current['a'] = 'over' } - t['a'].should == nil + expect(t['a']).to eq(nil) @scheduler.shutdown sleep(1) - t['a'].should == 'over' + expect(t['a']).to eq('over') end end describe '#job(job_id)' do it 'returns nil if there is no corresponding Job instance' do - @scheduler.job('nada').should == nil + expect(@scheduler.job('nada')).to eq(nil) end it 'returns the corresponding Job instance' do job_id = @scheduler.in '10d' do; end sleep(1) # give it some time to get scheduled - @scheduler.job(job_id).job_id.should == job_id + expect(@scheduler.job(job_id).job_id).to eq(job_id) end end # describe '#find_by_tag(t)' do # @@ -351,32 +353,32 @@ describe '#threads' do it 'just lists the main thread (scheduler thread) when no job is scheduled' do - @scheduler.threads.should == [ @scheduler.thread ] + expect(@scheduler.threads).to eq([ @scheduler.thread ]) end it 'lists all the threads a scheduler uses' do @scheduler.in '0s' do sleep(2) end sleep 0.4 - @scheduler.threads.size.should == 2 + expect(@scheduler.threads.size).to eq(2) end end describe '#work_threads(:all | :vacant)' do it 'returns an empty array when the scheduler has not yet done anything' do - @scheduler.work_threads.should == [] - @scheduler.work_threads(:all).should == [] - @scheduler.work_threads(:vacant).should == [] + expect(@scheduler.work_threads).to eq([]) + expect(@scheduler.work_threads(:all)).to eq([]) + expect(@scheduler.work_threads(:vacant)).to eq([]) end it 'lists the [vacant] work threads in the pool' do @scheduler.in '0s' do @@ -387,26 +389,26 @@ end sleep 0.7 if @scheduler.work_threads.size == 1 - @scheduler.work_threads.size.should == 1 - @scheduler.work_threads(:all).size.should == 1 - @scheduler.work_threads(:vacant).size.should == 0 + expect(@scheduler.work_threads.size).to eq(1) + expect(@scheduler.work_threads(:all).size).to eq(1) + expect(@scheduler.work_threads(:vacant).size).to eq(0) else - @scheduler.work_threads.size.should == 2 - @scheduler.work_threads(:all).size.should == 2 - @scheduler.work_threads(:vacant).size.should == 1 + expect(@scheduler.work_threads.size).to eq(2) + expect(@scheduler.work_threads(:all).size).to eq(2) + expect(@scheduler.work_threads(:vacant).size).to eq(1) end end end describe '#work_threads(:active)' do it 'returns [] when there are no jobs running' do - @scheduler.work_threads(:active).should == [] + expect(@scheduler.work_threads(:active)).to eq([]) end it 'returns the list of threads of the running jobs' do job = @@ -414,18 +416,18 @@ sleep 1 end sleep 0.4 - @scheduler.work_threads(:active).size.should == 1 + expect(@scheduler.work_threads(:active).size).to eq(1) t = @scheduler.work_threads(:active).first - t.class.should == Thread - t[@scheduler.thread_key].should == true - t[:rufus_scheduler_job].should == job - t[:rufus_scheduler_time].should_not == nil + expect(t.class).to eq(Thread) + expect(t[@scheduler.thread_key]).to eq(true) + expect(t[:rufus_scheduler_job]).to eq(job) + expect(t[:rufus_scheduler_time]).not_to eq(nil) end it 'does not return threads from other schedulers' do scheduler = Rufus::Scheduler.new @@ -435,11 +437,11 @@ sleep(1) end sleep 0.4 - scheduler.work_threads(:active).should == [] + expect(scheduler.work_threads(:active)).to eq([]) scheduler.shutdown end end @@ -457,21 +459,21 @@ describe '#max_work_threads' do it 'returns the max job thread count' do - @scheduler.max_work_threads.should == 28 + expect(@scheduler.max_work_threads).to eq(28) end end describe '#max_work_threads=' do it 'sets the max job thread count' do @scheduler.max_work_threads = 14 - @scheduler.max_work_threads.should == 14 + expect(@scheduler.max_work_threads).to eq(14) end end #describe '#kill_all_work_threads' do # @@ -495,11 +497,11 @@ describe '#running_jobs' do it 'returns [] when there are no running jobs' do - @scheduler.running_jobs.should == [] + expect(@scheduler.running_jobs).to eq([]) end it 'returns a list of running Job instances' do job = @@ -507,12 +509,12 @@ sleep(1) end sleep 0.4 - job.running?.should == true - @scheduler.running_jobs.should == [ job ] + expect(job.running?).to eq(true) + expect(@scheduler.running_jobs).to eq([ job ]) end it 'does not return twice the same job' do job = @@ -520,12 +522,12 @@ sleep(5) end sleep 1.5 - job.running?.should == true - @scheduler.running_jobs.should == [ job ] + expect(job.running?).to eq(true) + expect(@scheduler.running_jobs).to eq([ job ]) end end describe '#running_jobs(:tag/:tags => x)' do @@ -538,16 +540,19 @@ sleep 3 end sleep 0.4 - @scheduler.running_jobs(:tag => 't0').map(&:original).should == + expect(@scheduler.running_jobs(:tag => 't0').map(&:original)).to eq( %w[ 0.1s ] - @scheduler.running_jobs(:tag => 't1').map(&:original).should == + ) + expect(@scheduler.running_jobs(:tag => 't1').map(&:original)).to eq( %w[ 0.2s ] - @scheduler.running_jobs(:tags => %w[ t0 t1 ]).map(&:original).should == + ) + expect(@scheduler.running_jobs(:tags => %w[ t0 t1 ]).map(&:original)).to eq( [] + ) end end describe '#occurrences(time0, time1)' do @@ -559,49 +564,49 @@ j3 = @scheduler.schedule_interval '5m' do; end j4 = @scheduler.schedule_cron '* * * * *' do; end h = @scheduler.occurrences(Time.now + 4 * 60, Time.now + 11 * 60) - h.size.should == 5 - h[j0].should == [ j0.next_time ] - h[j1].should == [ j1.next_time ] - h[j2].size.should == 2 - h[j3].size.should == 2 - h[j4].size.should == 7 + expect(h.size).to eq(5) + expect(h[j0]).to eq([ j0.next_time ]) + expect(h[j1]).to eq([ j1.next_time ]) + expect(h[j2].size).to eq(2) + expect(h[j3].size).to eq(2) + expect(h[j4].size).to eq(7) end it 'returns a [ [ time, job ], ... ] of job occurrences when :timeline' do j0 = @scheduler.schedule_in '5m' do; end j1 = @scheduler.schedule_in '10m' do; end a = @scheduler.occurrences(Time.now + 4 * 60, Time.now + 11 * 60, :timeline) - a[0][0].should be_within_1s_of(Time.now + 5 * 60) - a[0][1].should == j0 - a[1][0].should be_within_1s_of(Time.now + 10 * 60) - a[1][1].should == j1 + expect(a[0][0]).to be_within_1s_of(Time.now + 5 * 60) + expect(a[0][1]).to eq(j0) + expect(a[1][0]).to be_within_1s_of(Time.now + 10 * 60) + expect(a[1][1]).to eq(j1) end it 'respects :first_at for repeat jobs' do j0 = @scheduler.schedule_every '5m', :first_in => '10m' do; end h = @scheduler.occurrences(Time.now + 4 * 60, Time.now + 16 * 60) - h[j0][0].should be_within_1s_of(Time.now + 10 * 60) - h[j0][1].should be_within_1s_of(Time.now + 15 * 60) + expect(h[j0][0]).to be_within_1s_of(Time.now + 10 * 60) + expect(h[j0][1]).to be_within_1s_of(Time.now + 15 * 60) end it 'respects :times for repeat jobs' do j0 = @scheduler.schedule_every '1m', :times => 10 do; end h = @scheduler.occurrences(Time.now + 4 * 60, Time.now + 16 * 60) - h[j0].size.should == 6 + expect(h[j0].size).to eq(6) end end describe '#timeline(time0, time1)' do @@ -610,14 +615,14 @@ j0 = @scheduler.schedule_in '5m' do; end j1 = @scheduler.schedule_in '10m' do; end a = @scheduler.timeline(Time.now + 4 * 60, Time.now + 11 * 60) - a[0][0].should be_within_1s_of(Time.now + 5 * 60) - a[0][1].should == j0 - a[1][0].should be_within_1s_of(Time.now + 10 * 60) - a[1][1].should == j1 + expect(a[0][0]).to be_within_1s_of(Time.now + 5 * 60) + expect(a[0][1]).to eq(j0) + expect(a[1][0]).to be_within_1s_of(Time.now + 10 * 60) + expect(a[1][1]).to eq(j1) end end #-- # management methods @@ -627,11 +632,11 @@ it 'blanks the uptime' do @scheduler.shutdown - @scheduler.uptime.should == nil + expect(@scheduler.uptime).to eq(nil) end it 'shuts the scheduler down' do @scheduler.shutdown @@ -641,18 +646,18 @@ t = Thread.list.find { |t| t[:name] == "rufus_scheduler_#{@scheduler.object_id}" } - t.should == nil + expect(t).to eq(nil) end it 'has a #stop alias' do @scheduler.stop - @scheduler.uptime.should == nil + expect(@scheduler.uptime).to eq(nil) end #it 'has a #close alias' end @@ -669,14 +674,14 @@ sleep 0.4 @scheduler.shutdown(:wait) - counter.should == 1 - @scheduler.uptime.should == nil - @scheduler.running_jobs.should == [] - @scheduler.threads.should == [] + expect(counter).to eq(1) + expect(@scheduler.uptime).to eq(nil) + expect(@scheduler.running_jobs).to eq([]) + expect(@scheduler.threads).to eq([]) end end describe '#shutdown(:kill)' do @@ -697,14 +702,14 @@ @scheduler.shutdown(:kill) sleep 1.4 - counter.should == 0 - @scheduler.uptime.should == nil - @scheduler.running_jobs.should == [] - @scheduler.threads.should == [] + expect(counter).to eq(0) + expect(@scheduler.uptime).to eq(nil) + expect(@scheduler.running_jobs).to eq([]) + expect(@scheduler.threads).to eq([]) end end describe '#pause' do @@ -714,11 +719,11 @@ @scheduler.pause sleep(3) - job.last_time.should == nil + expect(job.last_time).to eq(nil) end end describe '#resume' do @@ -729,57 +734,65 @@ @scheduler.pause sleep(1) @scheduler.resume sleep(2) - job.last_time.should_not == nil + expect(job.last_time).not_to eq(nil) end end describe '#paused?' do it 'returns true if the scheduler is paused' do @scheduler.pause - @scheduler.paused?.should == true + expect(@scheduler.paused?).to eq(true) end it 'returns false if the scheduler is not paused' do - @scheduler.paused?.should == false + expect(@scheduler.paused?).to eq(false) @scheduler.pause @scheduler.resume - @scheduler.paused?.should == false + expect(@scheduler.paused?).to eq(false) end end describe '#down?' do it 'returns true when the scheduler is down' do @scheduler.shutdown - @scheduler.down?.should == true + expect(@scheduler.down?).to eq(true) end it 'returns false when the scheduler is up' do - @scheduler.down?.should == false + expect(@scheduler.down?).to eq(false) end end + describe '#up?' do + + it 'returns true when the scheduler is up' do + + expect(@scheduler.up?).to eq(true) + end + end + #-- # job methods #++ describe '#jobs' do it 'is empty at the beginning' do - @scheduler.jobs.should == [] + expect(@scheduler.jobs).to eq([]) end it 'returns the list of scheduled jobs' do @scheduler.in '10d' do; end @@ -787,22 +800,22 @@ sleep(1) jobs = @scheduler.jobs - jobs.collect { |j| j.original }.sort.should == %w[ 10d 1w ] + expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 10d 1w ]) end it 'returns all the jobs (even those pending reschedule)' do @scheduler.in '0s', :blocking => true do sleep 2 end sleep 0.4 - @scheduler.jobs.size.should == 1 + expect(@scheduler.jobs.size).to eq(1) end it 'does not return unscheduled jobs' do job = @@ -812,34 +825,37 @@ sleep 0.4 job.unschedule - @scheduler.jobs.size.should == 0 + expect(@scheduler.jobs.size).to eq(0) end end describe '#jobs(:tag / :tags => x)' do it 'returns [] when there are no jobs with the corresponding tag' do - @scheduler.jobs(:tag => 'nada').should == [] - @scheduler.jobs(:tags => %w[ nada hello ]).should == [] + expect(@scheduler.jobs(:tag => 'nada')).to eq([]) + expect(@scheduler.jobs(:tags => %w[ nada hello ])).to eq([]) end it 'returns the jobs with the corresponding tag' do @scheduler.in '10d', :tag => 't0' do; end @scheduler.every '2h', :tag => %w[ t0 t1 ] do; end @scheduler.every '3h' do; end - @scheduler.jobs(:tags => 't0').map(&:original).sort.should == + expect(@scheduler.jobs(:tags => 't0').map(&:original).sort).to eq( %w[ 10d 2h ] - @scheduler.jobs(:tags => 't1').map(&:original).sort.should == + ) + expect(@scheduler.jobs(:tags => 't1').map(&:original).sort).to eq( %w[ 2h ] - @scheduler.jobs(:tags => [ 't1', 't0' ]).map(&:original).sort.should == + ) + expect(@scheduler.jobs(:tags => [ 't1', 't0' ]).map(&:original).sort).to eq( %w[ 2h ] + ) end end describe '#every_jobs' do @@ -849,11 +865,11 @@ @scheduler.in '10d' do; end @scheduler.every '5m' do; end jobs = @scheduler.every_jobs - jobs.collect { |j| j.original }.sort.should == %w[ 5m ] + expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 5m ]) end end describe '#at_jobs' do @@ -863,11 +879,11 @@ @scheduler.in '10d' do; end @scheduler.every '5m' do; end jobs = @scheduler.at_jobs - jobs.collect { |j| j.original }.sort.should == [ '2030/12/12 12:10:00' ] + expect(jobs.collect { |j| j.original }.sort).to eq([ '2030/12/12 12:10:00' ]) end end describe '#in_jobs' do @@ -877,11 +893,11 @@ @scheduler.in '10d' do; end @scheduler.every '5m' do; end jobs = @scheduler.in_jobs - jobs.collect { |j| j.original }.sort.should == %w[ 10d ] + expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 10d ]) end end describe '#cron_jobs' do @@ -892,11 +908,11 @@ @scheduler.every '5m' do; end @scheduler.cron '* * * * *' do; end jobs = @scheduler.cron_jobs - jobs.collect { |j| j.original }.sort.should == [ '* * * * *' ] + expect(jobs.collect { |j| j.original }.sort).to eq([ '* * * * *' ]) end end describe '#interval_jobs' do @@ -908,11 +924,11 @@ @scheduler.cron '* * * * *' do; end @scheduler.interval '7m' do; end jobs = @scheduler.interval_jobs - jobs.collect { |j| j.original }.sort.should == %w[ 7m ] + expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 7m ]) end end #-- # callbacks @@ -933,11 +949,11 @@ $out << job.id end sleep 0.7 - $out.should == [ "pre #{job_id}", job_id ] + expect($out).to eq([ "pre #{job_id}", job_id ]) end it 'accepts the job and the triggerTime as argument' do $tt = nil @@ -950,13 +966,13 @@ @scheduler.in '0.5s' do; end sleep 0.7 - $tt.class.should == Time - $tt.should > start - $tt.should < Time.now + expect($tt.class).to eq(Time) + expect($tt).to be > start + expect($tt).to be < Time.now end context 'when it returns false' do it 'prevents the job from triggering' do @@ -973,11 +989,11 @@ $out << job.id end sleep 0.7 - $out.should == [ "pre #{job_id}" ] + expect($out).to eq([ "pre #{job_id}" ]) end end end describe '#on_post_trigger' do @@ -995,11 +1011,11 @@ $out << job.id end sleep 0.7 - $out.should == [ job_id, "post #{job_id}" ] + expect($out).to eq([ job_id, "post #{job_id}" ]) end end #-- # misc @@ -1015,19 +1031,19 @@ it 'returns a singleton instance of the scheduler' do s0 = Rufus::Scheduler.singleton s1 = Rufus::Scheduler.s - s0.class.should == Rufus::Scheduler - s1.object_id.should == s0.object_id + expect(s0.class).to eq(Rufus::Scheduler) + expect(s1.object_id).to eq(s0.object_id) end it 'accepts initialization parameters' do s = Rufus::Scheduler.singleton(:max_work_threads => 77) s = Rufus::Scheduler.singleton(:max_work_threads => 42) - s.max_work_threads.should == 77 + expect(s.max_work_threads).to eq(77) end end end