Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Testing Sidekiq::Status correctly? #63

Closed
nikhgupta opened this issue Aug 5, 2015 · 4 comments
Closed

Testing Sidekiq::Status correctly? #63

nikhgupta opened this issue Aug 5, 2015 · 4 comments

Comments

@nikhgupta
Copy link

I am using Sidekiq::Status inside an app that relies heavily on the status reported by this gem, esp. since the status is displayed to the users of this application.

I found the current way of testing a bit brittle, since all it does is to mock the return status as :complete. I understand the fact that inline way of testing sidekiq will always give :complete status. But, Sidekiq also has Sidekiq::Testing.fake!, which when used should return status as per the job's actual status. That is, I should be able to do this:

RSpec.describe MyWorker do
  it 'returns the current status of the job' do
    jid = described_class.perform_async(user.id, echo_url)
    expect(Sidekiq::Status.status(jid)).to eq(:queued)
    described_class.drain
    expect(Sidekiq::Status.status(jid)).to eq(:complete)
  end
end

However, since Sidekiq does not run Server Middlewares with such tests, we can add:

require 'sidekiq/testing'

module Sidekiq::Worker::ClassMethods
  def execute_job(worker, args)
    Sidekiq::Status::ServerMiddleware.new.call(worker, nil, nil) do
      worker.perform(*args)
    end
  end
end

So, I would like to know if this is a valid way to test this gem? And, if so, would you be willing to accept a PR? Also, I am still not sure how to test nil status with this, when the job expires after the given expiration time.

@dmitry
Copy link

dmitry commented Aug 1, 2016

@nikhgupta have you solved this issue?

@lastobelus
Copy link

lastobelus commented Oct 18, 2016

It would also be good if we could test unschedule with Sidekiq::Testing.fake!

@lastobelus
Copy link

This would be somewhat involved...essentially creating an entire fake Sidekiq::Status::Storage

  • creating Sidekiq::Status::Testing analogous to Sidekiq::Testing, for switching between inline, fake and disable modes
  • changing sidekiq_status/testing/inline to set inline mode
  • wrap Sidekiq::Testing.server_middleware to call the Sidekiq::Status server middleware
  • for fake! mode, provide a Sidekiq::Status::Storage fake that stores statuses in the Sidekiq::Queues job hashes, and whose delete_and_unschedule deletes job hashes.

Not sure if this is worthwhile. Currently I get by by setting expectations on Sidekiq::Status.unschedule and I am not testing the statuses that get set during the execution of jobs. However, a fake! mode that collected the sequence of statuses would also provide an interesting way to write flow/integration tests of complicated workers. But for this it might be easier to just stub the at method.

@kenaniah
Copy link
Collaborator

If you don't mind actually pushing jobs to redis during testing, I would recommend using the following:

require 'sidekiq/testing'
Sidekiq::Testing.disable! # allows jobs to be pushed to redis. Use #enable! to reverse

Or, simply not requiring sidekiq/testing in the first place.

Our own test suite functions by not including / disabling Sidekiq's testing mocks, and our spec_helper.rb file would be a great place to start if you're looking to do full integration tests.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants