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

Kredis namespace isn't set properly for rails system tests #152

Closed
arjun810 opened this issue May 15, 2024 · 1 comment
Closed

Kredis namespace isn't set properly for rails system tests #152

arjun810 opened this issue May 15, 2024 · 1 comment

Comments

@arjun810
Copy link

I ran into a bug where my system tests weren't running as expected when parallelized. I discovered that the Puma threads spawned by Capybara don't have the namespace set (see https://github.com/rails/kredis/blob/57cedf59a50a0ef81779b5259806821fc4a15423/lib/ kredis/railtie.rb#L8). This is because the namespacing is done using Thread.current, and the threads spawned by Puma wouldn't have Thread.current[:kredis_namespace] set.

I have a workaround for my system tests, but it'd be nice if this worked out of the box.

For my workaround, I have a module that my ApplicationSystemTestCase includes:

module KredisNamespaceFix
  def self.included(base)
    base.class_eval do
      setup do
        add_kredis_namespace_before_action
      end

      parallelize_setup do |worker|
        Rails.application.config.x.kredis_test_namespace = "test-#{worker}"
      end
    end
  end

  def add_kredis_namespace_before_action
    ApplicationController.class_eval do
      before_action :set_kredis_namespace_for_test

      private

      def set_kredis_namespace_for_test
        Kredis.namespace = Rails.application.config.x.kredis_test_namespace
      end
    end
  end
end

This seems to work, but it was a bit painful to track down.

@jeremy
Copy link
Member

jeremy commented Dec 19, 2024

That is painful. Ran into this also; partially fixed in #156.

Namespacing should be up-front config rather than a thread local, but changing that is a major break. Addressed that with Kredis.global_namespace in #158.

@jeremy jeremy closed this as completed Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants