kredis icon indicating copy to clipboard operation
kredis copied to clipboard

Kredis namespace isn't set properly for rails system tests

Open arjun810 opened this issue 1 year ago • 0 comments

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.

arjun810 avatar May 15 '24 06:05 arjun810