ruby-on-railsrspecsidekiqsidetiqrspec-sidekiq

Sidekiq tries to run in test environment


I have a very strange issue: Sidekiq tries to establish connection to Redis in test environment. I'm using sidekiq 3.2.2 with rspec-sidekiq 2.0.0.beta on rails 4.1.4 with ruby 2.1.2

Here is my test

require 'rails_helper'

describe FetchWorker::FetchWorker do
 it 'runs in fetch queue' do
   expect(subject).to be_processed_in :fetch
 end
 it 'not retry when it fails' do 
   expect(subject).to be_retryable false
 end

 it 'enqueues the new job' do
   FactoryGirl.create(:history, running: false)
   FetchWorker::FetchWorker.perform_async 'FetchWorker', true
   expect(FetchWorker::FetchWorker).to have_enqueued_job('FetchWorker', true)
 end # enqueue job

 describe '#perform' do
  let!(:settleddate) { (Time.now - 2.minutes).iso8601(10) }
  let!(:lock_date) { (Time.now - 5.minutes).iso8601(10) }
   context 'success' do
    before do 
     FactoryGirl.create(:history, running: false, NEW_TRANSACTIONS: lock_date)
     FactoryGirl.create(:abp_transaction, settleddate: settleddate, settledyn: 1, transactiontypeid: 1, accountid: 10966)
    end
    it 'perform worker' do
     user = FactoryGirl.build_stubbed(:user, account_id: 10966)
     worker = FetchWorker::FetchWorker.new
     worker.perform
     expect(user.transactions.count).to eq(1)
    end # it perform worker
   end # context success

context 'fail' do
  it 'perform worker' do
    FactoryGirl.create(:history, running: false, NEW_TRANSACTIONS: lock_date)
    FactoryGirl.create(:abp_transaction, settleddate: settleddate, settledyn: 1, transactiontypeid: 1, accountid: 10966)
    user = FactoryGirl.build_stubbed(:user, account_id: 10966)
    worker = FetchWorker::FetchWorker.new
    allow(worker).to receive(:process_transactions).and_raise('some error')
    expect{ worker.perform }.to raise_error
   end # it perform worker
  end # context fail
 end # describe perform
end # describe FetchWorker

If I try to run bundle exec rspec with working redis, I'm getting pretty nice output:

retgoat@ubuntu:/media/sf_offside/agent_system$ bundle exec rspec
[rspec-sidekiq] WARNING! Sidekiq will *NOT* process jobs in this environment. See        https://github.com/philostler/rspec-sidekiq/wiki/FAQ-&-Troubleshooting
2014-08-22T11:35:52.377Z 5140 TID-4ehb4 INFO: Fetching      transactions from 2014-08-   22T18:30:52.3447179170+07:00.
2014-08-22T11:35:52.394Z 5140 TID-4ehb4 INFO: Lock released.
2014-08-22T11:35:52.451Z 5140 TID-4ehb4 INFO: Fetching transactions from 2014-08-         22T18:30:52.4160675140+07:00.

Finished in 29.88 seconds (files took 17.24 seconds to load)
119 examples, 0 failures
Coverage report generated for RSpec to /media/sf_offside/agent_system/coverage. 872 /    873 LOC (99.89%) covered.

But if I try to run tests with stopped redis I'm getting this:

retgoat@ubuntu:/media/sf_offside/agent_system$ bundle exec rspec
Coverage report generated for RSpec to /media/sf_offside/agent_system/coverage. 377 / 855 LOC (44.09%) covered.
/home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:309:in `rescue  in establish_connection': Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)    (Redis::CannotConnectError)
    from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:304:in  `establish_connection'
    from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:85:in `block in connect'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:266:in `with_reconnect'
    from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:84:in `connect'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:326:in `ensure_connected'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:197:in `block in process'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:279:in `logging'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:196:in `process'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:102:in `call'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:784:in `block in get'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:37:in `block in synchronize'
   from /home/retgoat/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:37:in `synchronize'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:783:in `get'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/sidetiq- 0.6.1/lib/sidetiq/schedulable.rb:65:in `get_schedulable_key'
    from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/sidetiq-0.6.1/lib/sidetiq/schedulable.rb:45:in `recurrence'
    from /media/sf_offside/agent_system/app/workers/fetch_worker.rb:6:in `<class:FetchWorker>'
   from /media/sf_offside/agent_system/app/workers/fetch_worker.rb:2:in `<module:FetchWorker>'
   from /media/sf_offside/agent_system/app/workers/fetch_worker.rb:1:in `<top (required)>'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:348:in `require_or_load'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:480:in `load_missing_constant'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:180:in `const_missing'
   from /media/sf_offside/agent_system/spec/workers/fetch_worker_spec.rb:3:in `<top (required)>'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-   3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
    from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `load'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `<main>'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
   from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

I just have no idea why it happens.

I'm also opened a new issue in sidekiq on Github https://github.com/mperham/sidekiq/issues/1907

Could you please help with that?

Many thanks.

UPDATE

As @infused mentioned there is a bug: https://github.com/tobiassvn/sidetiq/issues/103

Here is the workaround:

    recurrence { minutely(5) } unless Rails.env.test?

Worked like a charm! :)

Many thanks


Solution

  • The problem is that you are using sidetiq which has a bug which causes it to try to connect when it shouldn't.