rubyrspecsidekiqrspec-sidekiq

Sidekiq testing perform_in


I wonder if there's anyway to test whether sidekiq properly executes a job in X seconds(or enqueues a job to be performed in X seconds), as specified in a perform_in call.

As far as I can tell, the docs don't allude to any mode that allows this. In fake mode, it seems the job is directly added to the queue and executed immediately at the drain call.


Solution

  • If you are in fake mode, you can access the your Workers jobs with MyWorker.jobs.

    If you scheduled your jobs with perform_in they should have a field at giving you the execution time (as float). I wrote a rspec test along the line of

    subject { MyWorker.jobs.first }    
    it 'is scheduled in 1 minute' do
      span = subject['at'] - Time.now.to_f
      expect(span.round).to eq 60
    end
    

    I hope that helps!