0

我有一个简单的工作人员正在访问自己队列的大小:

需要'sidekiq/api'

class TestWorker
  include Sidekiq::Worker

  def perform(*args)
    Sidekiq::Queue.new('test').size
  end
end

然后我正在测试这个工人:

require 'rails_helper'

RSpec.describe TestWorker, type: :worker do
  describe '#perform' do
    it 'executes the job without connecting to Redis in the testing environment' do
      require 'sidekiq/testing'
      Sidekiq::Testing.fake! do
        TestWorker.perform_async
        TestWorker.drain
      end
    end
  end
end

此测试失败,因为它尝试访问 redis:

1) TestWorker#perform executes the job without connecting to Redis in the testing environment
     Failure/Error: Sidekiq::Queue.new('test').size

     Redis::CannotConnectError:
       Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:344:in `rescue in establish_connection'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:328:in `establish_connection'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:99:in `block in connect'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:291:in `with_reconnect'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:98:in `connect'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:363:in `ensure_connected'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:219:in `block in process'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:304:in `logging'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:218:in `process'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:118:in `call'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis.rb:1062:in `block in llen'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis.rb:1061:in `llen'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/api.rb:217:in `block in size'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq.rb:95:in `block in redis'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/connection_pool-2.2.2/lib/connection_pool.rb:65:in `block (2 levels) in with'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/connection_pool-2.2.2/lib/connection_pool.rb:64:in `handle_interrupt'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/connection_pool-2.2.2/lib/connection_pool.rb:64:in `block in with'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/connection_pool-2.2.2/lib/connection_pool.rb:61:in `handle_interrupt'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/connection_pool-2.2.2/lib/connection_pool.rb:61:in `with'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq.rb:92:in `redis'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/api.rb:217:in `size'
     # ./app/workers/test_worker.rb:7:in `perform'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/testing.rb:301:in `execute_job'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/testing.rb:296:in `block in process_job'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/middleware/chain.rb:128:in `block in invoke'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/middleware/chain.rb:133:in `invoke'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/testing.rb:295:in `process_job'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/testing.rb:279:in `drain'
     # ./spec/workers/test_worker_spec.rb:8:in `block (4 levels) in <top (required)>'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/testing.rb:16:in `__set_test_mode'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/sidekiq-5.1.3/lib/sidekiq/testing.rb:30:in `fake!'
     # ./spec/workers/test_worker_spec.rb:6:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # IO::EINPROGRESSWaitWritable:
     #   Operation now in progress - connect(2) would block
     #   /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/connection/ruby.rb:180:in `connect_addrinfo'

根据与 Sidekiq::Testing.fake 相关的 Sidekiq 文档!“Sidekiq 不是将作业推送到 Redis,而是将它们推送到您可以访问的作业数组中”

为什么Sidekiq::Queue.new('test').size在测试环境中尝试连接 Redis 而不是检查作业数组的大小?

有没有办法Sidekiq::Queue使用Sidekiq::Testing.fake!或我应该嘲笑它?

4

1 回答 1

2

Sidekiq 的 API 没有测试模式,[它] 会一直打 Redis。

https://github.com/mperham/sidekiq/wiki/testing#api

于 2018-08-03T14:23:53.080 回答