Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidekiq::Status tries to connect to Redis in RSpec #41

Open
andrewt77 opened this issue Oct 1, 2023 · 4 comments
Open

Sidekiq::Status tries to connect to Redis in RSpec #41

andrewt77 opened this issue Oct 1, 2023 · 4 comments

Comments

@andrewt77
Copy link

Ruby version: 3.0.1
Rails version: 7.0.7
Sidekiq / Pro / Enterprise version(s): 7.1.3

sidekiq initializer:

require 'sidekiq'
require 'sidekiq-status'

# SIDEKIQ_STATUS_EXPIRATION = 48.hours.to_i
Sidekiq.configure_client do |config|
  config.redis = { :size => 1 }
  Sidekiq.strict_args!(false)
  # Sidekiq::Status.configure_client_middleware(config,
                                              # expiration: SIDEKIQ_STATUS_EXPIRATION)
end

After updating from Sidekiq 6 to Sidekiq 7, running sidekiq worker tests with RSpec that involve using Sidekiq::Status or Sidekiq::Batch modules are failing because they are trying to connect to a redis server (which should not happen in RSpec, and also confirmed that in Sidekiq 6 it works as expected).

Even trying to put the Sidekiq::Status.configure_client_middleware line in the sidekiq initializer (commented for now) and trying to run a RSpec test with sidekiq will result into the following error:

RedisClient::CannotConnectError:
       Connection refused - connect(2) for 127.0.0.1:6379
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client/ruby_connection.rb:117:in `connect'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client/ruby_connection.rb:49:in `initialize'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client.rb:688:in `new'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client.rb:688:in `block in connect'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client/middlewares.rb:12:in `connect'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client.rb:687:in `connect'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client.rb:674:in `raw_connection'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client.rb:641:in `ensure_connected'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client.rb:415:in `multi'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client/decorator.rb:55:in `multi'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-status-3.0.3/lib/sidekiq-status/storage.rb:17:in `block in store_for_id'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-status-3.0.3/lib/sidekiq-status/storage.rb:112:in `block in redis_connection'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/connection_pool-2.4.1/lib/connection_pool.rb:110:in `block (2 levels) in with'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/connection_pool-2.4.1/lib/connection_pool.rb:109:in `handle_interrupt'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/connection_pool-2.4.1/lib/connection_pool.rb:109:in `block in with'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/connection_pool-2.4.1/lib/connection_pool.rb:106:in `handle_interrupt'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/connection_pool-2.4.1/lib/connection_pool.rb:106:in `with'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-status-3.0.3/lib/sidekiq-status/storage.rb:111:in `redis_connection'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-status-3.0.3/lib/sidekiq-status/storage.rb:16:in `store_for_id'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-status-3.0.3/lib/sidekiq-status/client_middleware.rb:42:in `call'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/middleware/chain.rb:182:in `traverse'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/middleware/chain.rb:183:in `block in traverse'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-batch-0.1.9/lib/sidekiq/batch/middleware.rb:11:in `call'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/middleware/chain.rb:182:in `traverse'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/middleware/chain.rb:173:in `invoke'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/client.rb:88:in `push'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/job.rb:361:in `client_push'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/job.rb:198:in `perform_async'
     # /Users/andrew/.rvm/gems/ruby-3.0.1/gems/sidekiq-7.1.3/lib/sidekiq/job.rb:286:in `perform_async'
     # ./spec/workers/associations_dac_billing_report_spec.rb:154:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Errno::ECONNREFUSED:
     #   Connection refused - connect(2) for 127.0.0.1:6379
     #   /Users/andrew/.rvm/gems/ruby-3.0.1/gems/redis-client-0.17.0/lib/redis_client/ruby_connection.rb:117:in `connect'

If I comment that line then the initializer runs successfully, but then it will fail where I use Sidekiq::Status or Sidekiq::Batch in the workers that are being tested. For Sidekiq::Batch I found a workaround with rspec-sidekiq, but I can't find one to prevent Sidekiq::Status to connect to redis server.

I'm using the latest sidekiq-status, 3.0.3, and we really want to upgrade the Sidekiq gem from 6 to 7, so any help would be appreciated, thank you!

@devsheva
Copy link

I have the same problem, if I remove the include in my Job of Sidekiq Status it works

@devsheva
Copy link

Found the problem, somehow the sidekiq-status module is required after requiring sidekiq-status/testing/inline.

This solutions works, but I think is a temporary solution:

spec_helper.rb

  config.before(:suite) { require 'sidekiq-status/testing/inline' }

@angusm73
Copy link

This solutions works, but I think is a temporary solution:

spec_helper.rb

  config.before(:suite) { require 'sidekiq-status/testing/inline' }

This wasn't working for me, instead I've completely removed the middleware from my test environment. Here's what my Sidekiq initializer looks like:

require "sidekiq"
require "sidekiq-status"

Sidekiq.configure_client do |config|
  break if Rails.env.test?

  Sidekiq::Status.configure_client_middleware config, expiration: 60.minutes.to_i
end

Sidekiq.configure_server do |config|
  break if Rails.env.test?

  Sidekiq::Status.configure_server_middleware config, expiration: 60.minutes.to_i
  Sidekiq::Status.configure_client_middleware config, expiration: 60.minutes.to_i
end

@devsheva
Copy link

@angusm73 this is actually a good solution. we still need to figure out why is this happening though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants