Sidekiq试图在测试环境中运行

时间:2014-08-22 11:42:41

标签: ruby-on-rails rspec sidekiq sidetiq rspec-sidekiq

我有一个非常奇怪的问题:Sidekiq尝试在测试环境中建立与Redis的连接。 我使用sidekiq 3.2.2和rspec-sidekiq 2.0.0.beta在rails 4.1.4上使用ruby 2.1.2

这是我的测试

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

如果我尝试使用redis运行bundle exec rspec,我会得到相当不错的输出:

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.

但是,如果我尝试使用已停止的redis运行测试,我会得到这个:

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>'

我只是不知道为什么会这样。

我还在Github上的sidekiq上发了一个新问题https://github.com/mperham/sidekiq/issues/1907

你可以帮忙吗?

非常感谢。

更新

@infused提到有一个错误:https://github.com/tobiassvn/sidetiq/issues/103

以下是解决方法:

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

像魅力一样工作! :)

非常感谢

1 个答案:

答案 0 :(得分:1)

问题是您正在使用sidetiq {{1}}导致它在不应该尝试连接时{{1}}。