rails icon indicating copy to clipboard operation
rails copied to clipboard

`CurrentAttribute`s are cleared when a job gets executed inline in tests

Open Earlopain opened this issue 9 months ago • 8 comments

Steps to reproduce

I have a test case like this

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
end

require "active_support/railtie"
require "active_job/railtie"
require "minitest/autorun"

class TestApp < Rails::Application
  config.load_defaults 7.1

  config.active_job.queue_adapter = :inline
  config.eager_load = false
end

Rails.application.initialize!

class Current < ActiveSupport::CurrentAttributes
  attribute :user
end

class DummyJob < ActiveJob::Base
  def perform
  end
end

class CurrentAttributeTest < ActiveSupport::TestCase
  test "CurrentAttributes" do
    Current.user = "test"
    DummyJob.perform_later

    assert_equal("test", Current.user, "CurrentAttributes reset")
  end
end

Using the inline queue adapter the second assertion failed. ~~I tried making a self-contained test script but couldn't manage, the attributes only seem to be reset when I run it in a full-blown rails app. I have created a sample app here https://github.com/Earlopain/rails-current-attributes, you can just run rake test there.~~

I found issue #37526 and pr #37568 which seem to be about the same thing and it looks like they were closed as fixed/completed. Don't know what to make of that, just wanted to document.

Expected behavior

CurrentAttributes are preserved.

Actual behavior

CurrentAttributes are cleared.

System configuration

Rails version: main

Ruby version: 3.2.2

Earlopain avatar Sep 11 '23 21:09 Earlopain