What's the best way to spec the scheduler? #42

Closed
dnagir opened this Issue Nov 9, 2012 · 3 comments

Comments

Projects
None yet
2 participants
@dnagir

dnagir commented Nov 9, 2012

When defining the tasks to run I really want to spec them to ensure the tasks were actually scheduled correctly.

I have the following in mind, but not sure what's the best way to do it.

class TickTack
  attr_reader :scheduler

 def initialize
   @scheduler = Rufus::PlainScheduler.new # Note that I don't want to actually start it!

    scheduler.cron '0 22 * * 1-5' do
      # every day of the week at 22:00 (10pm)
      Security.activate
    end
 end

  def start!
    scheduler.start
  end
end


# Now I want to spec it similarly to:

describe TickTack do

  def tick
    subject.scheduler.step #????
  end

  it "should activate alarm on the weekdays after 10pm" do
    Timecop.travel Chronic.parse("next monday 22:01")
    Security.should_receive(:activate)
    tick
  end


  it "should not activate alarm on the weekdays before 10pm" do
    Timecop.travel Chronic.parse("next monday 8pm")
    Security.should_not_receive(:activate)
    tick
  end


  it "should not activate alarm on the weekend after 10pm" do
    Timecop.travel Chronic.parse("next sunday 22:01")
    Security.should_not_receive(:activate)
    tick
  end
end

But I don't know if that's a a good way of doing it.

@jmettraux

This comment has been minimized.

Show comment Hide comment
@jmettraux

jmettraux Nov 9, 2012

Owner

Hello,

don't fall into the trap of testing rufus-scheduler. You're supposed to test your code.

You have to start the scheduler to make it check for jobs to trigger, else it's inert.

One way to test without starting the scheduler would be to make sure the jobs are queried with the right info. See Scheduler#jobs or #cron_jobs.

Best regards,

John

Owner

jmettraux commented Nov 9, 2012

Hello,

don't fall into the trap of testing rufus-scheduler. You're supposed to test your code.

You have to start the scheduler to make it check for jobs to trigger, else it's inert.

One way to test without starting the scheduler would be to make sure the jobs are queried with the right info. See Scheduler#jobs or #cron_jobs.

Best regards,

John

@jmettraux jmettraux closed this Nov 9, 2012

@dnagir

This comment has been minimized.

Show comment Hide comment
@dnagir

dnagir Nov 9, 2012

Thanks John.

Well, scheduling the jobs is the code I write (thus is mine) and do want to test it.

Checking the right jobs might be fine, but I'd rather want to make sure that the blocks are called at the correct times.
It's not for testing the scheduler, but rather that I provided correct information for the scheduler.

But maybe it's just not worth the effort...

dnagir commented Nov 9, 2012

Thanks John.

Well, scheduling the jobs is the code I write (thus is mine) and do want to test it.

Checking the right jobs might be fine, but I'd rather want to make sure that the blocks are called at the correct times.
It's not for testing the scheduler, but rather that I provided correct information for the scheduler.

But maybe it's just not worth the effort...

@jmettraux

This comment has been minimized.

Show comment Hide comment
@jmettraux

jmettraux Nov 9, 2012

Owner

It's not for testing the scheduler, but rather that I provided correct information for the scheduler.

Like I said above, you can query the scheduler via #jobs or #cron_jobs and check that the jobs in there hold the right information. That exactly matches your "that I provided correct information for the scheduler".

Best regards.

Owner

jmettraux commented Nov 9, 2012

It's not for testing the scheduler, but rather that I provided correct information for the scheduler.

Like I said above, you can query the scheduler via #jobs or #cron_jobs and check that the jobs in there hold the right information. That exactly matches your "that I provided correct information for the scheduler".

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment