Search code examples
ruby-on-railsrubymultithreadingrspecsidekiq

RSpec: testing sidekiq worker for race condition


Sometimes I have situation when the same worker running twice - when user sending requests fast, one by one. I added condition: if worker was already started and inside worker's payload is class Importer - don't allow to start queue second time.

Now I need to make a spec for it. I think, that to call smth like this is not a good way:

before { 2.times { Importer.perform } }

What is the best way to test worker for such race condition? (generally: in which way to test Sidekiq worker when you need to run same query twice or more)


Solution

  • Ok, so there are was no answers.

    I decided just to stub it. Inside Import class I have Sidekiq::Workers.new, so inside spec I placed let(:sidekiq_workers) block with all worker's parameters, like queue, payload, args and so on.

    Then inside it block I'm calling method, that checking if such query already exists. This test passes and returns "true". I have another it block without stubbed sidekiq workers and test passes with "false".