Skip to content

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

Closed

Description

@arjun810

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.

Activity

  1. jeremy commented on Dec 19, 2024

    @jeremy
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions