gpt4 book ai didi

ruby-on-rails - Sidekiq::测试.假的!不伪造 Sidekiq::Queue

转载 作者:行者123 更新时间:2023-12-03 08:59:41 31 4
gpt4 key购买 nike

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

需要“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::QueueSidekiq::Testing.fake! 一起工作,还是我应该模拟它?

最佳答案

Sidekiq's API does not have a testing mode, [it] will always hit Redis.

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

关于ruby-on-rails - Sidekiq::测试.假的!不伪造 Sidekiq::Queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669660/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com