Skip to content

Rails 6 inconsistently overrides ActiveJob queue_adapter setting with TestAdapter #37270

@searls

Description

@searls

Summary

Like many folks, I set this in my config/environments/test.rb file:

config.active_job.queue_adapter = :inline  

The reason I do this is to have easy-to-test, deterministic behavior of any potentially async delayed jobs triggered by the application. Under Rails 5, all of my tests (children of ActiveSupport::TestCase and ActionDispatch::SystemTestCase alike) would respect this queue adapter. However, under Rails 6, any built-in Rails test case that includes ActiveJob::TestHelper will also override this setting for all descendants of ActiveJob::Base (relevant source).

The net effect of this is that my perform_later and deliver_later calls to jobs aren't being fired synchronously (or, in fact, at all) during my system tests, which is leading to test failures upon upgrading to Rails 6.

Steps to reproduce

Here are two tests that demonstrates the problem:

require "test_helper"

class JobTest < ActiveSupport::TestCase
  class SomeJob < ActiveJob::Base
    cattr_accessor :job_ran

    def perform
      @@job_ran = true
    end
  end

  def test_some_job
    assert_equal :inline, Rails.application.config.active_job.queue_adapter
    assert_equal ActiveJob::QueueAdapters::InlineAdapter, SomeJob.queue_adapter.class

    SomeJob.perform_later

    assert_equal true, SomeJob.job_ran
  end
end

class JobSystemTest < ActionDispatch::SystemTestCase
  class OtherJob < ActiveJob::Base
    cattr_accessor :job_ran

    def perform
      @@job_ran = true
    end
  end

  def test_other_job
    assert_equal :inline, Rails.application.config.active_job.queue_adapter
    assert_equal ActiveJob::QueueAdapters::InlineAdapter, OtherJob.queue_adapter.class

    OtherJob.perform_later

    assert_equal true, OtherJob.job_ran
  end
end

Expected behavior

I would expect the behavior to have remained as it did under Rails 5, where the active_job.queue_adapter setting would be respected for all of Rails' test types.

Actual behavior

The first test, which descends from ActiveSupport::TestCase will pass and behave as it did under Rails 5. However, the second test, descending from from ActionDispatch::SystemTestCase will now fail under Rails 6, because OtherJob's queue_adapter will have been reset by ActiveJob::TestHelper to be an instance of TestAdapter

JobSystemTest#test_other_job [/Users/justin/code/testdouble/present/test/lib/job_test.rb:33]:
--- expected
+++ actual
@@ -1 +1 @@
-ActiveJob::QueueAdapters::InlineAdapter
+ActiveJob::QueueAdapters::TestAdapter

System configuration

Rails version: 6.0.0 as well as 6-0-stable, as of today.

Ruby version: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions