wspurgin / rspec-sidekiq

RSpec for Sidekiq
https://github.com/wspurgin/rspec-sidekiq
Other
662 stars 133 forks source link

Add "immediately" matcher #210

Closed chrismaximin closed 1 year ago

chrismaximin commented 1 year ago

The issue

There is currently no way to check whether a job was scheduled for some point in the future, or enqueued immediately. Specifically, we can't use have_enqueued_sidekiq_job.at( ... ) if a job was enqueued immediately.

This is because when Sidekiq doesn't add a "at" option when the date was in the past, which rspec-sidekiq currently relies on: https://github.com/sidekiq/sidekiq/blob/bd1ca2b94ad73abdfd4347c83be527bb3ff60acb/lib/sidekiq/job.rb#L328C1-L329

This PR

This PR enables this:

# MyJob will enqueued immediately by Sidekiq, not scheduled.
# It is equivalent to writing `MyJob.perform_async("hello")`
MyJob.perform_at(5.seconds.ago, "hello")

expect(MyJob).to have_enqueued_sidekiq_job("hello").immediately # passes
wspurgin commented 1 year ago

Thanks @chrismaximin Could you clarify the first statement though?

There is currently no way to check whether a job was scheduled for some point in the future or enqueued immediately.

This statement, on first read, is not true. You can absolutely test that something was enqueued for the future...

I'm not against adding an explicit "no wait" option to the matcher - in other words testing, explicitly, that no delay was specified when enqueuing.

Could you add specs to enqueue_sidekiq_job_spec as well? If you wanna add some docs to both matchers, I'd be much obliged 👍

chrismaximin commented 1 year ago

There is currently no way to check whether a job was scheduled for some point in the future or enqueued immediately.

This statement, on first read, is not true. You can absolutely test that something was enqueued for the future...

The statement was too broad indeed. I meant "There is currently no way to check in a whether a job was scheduled or enqueued". I added docs in the README and in matchers/enqueue_sidekiq_job.rb for YARD, the other places being private.

wspurgin commented 1 year ago

I have a couple things to add for a 4.1 (composable support for example), that this will be included in. Thanks again for the contribution @chrismaximin