ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.53k stars 262 forks source link

Change to CurrentAttributes leads to broken ActiveJob testing #335

Open tmaier opened 4 months ago

tmaier commented 4 months ago

I have ActsAsTenant version 1.0.1 running in my Rails 7.0 application.

I just wanted to switch from Sidekiq to GoodJob. For this, I also changed all my jobs from Sidekiq Workers to ActiveJob jobs.

I realized that some tests started to fail in an odd way. Specifically, current_tenant was suddenly not set anymore.

I suspect there are multiple issues going on

One has been mentioned in #328. I think ActsAsTenant.with_tenant should be used here

The new issue I want to report here is that ActiveJob and ActiveSupport::CurrentAttributes have undesired side effects.

I learned at https://github.com/bensheldon/good_job/blob/ab5129030067f1c97253d90b3d1ef96da25ff733/lib/good_job/current_thread.rb#L8 the following:

module GoodJob
  # Thread-local attributes for passing values from Instrumentation.
  # (Cannot use ActiveSupport::CurrentAttributes because ActiveJob resets it)
  module CurrentThread
    # ...

This pointed me to the following issue at rails: https://github.com/rails/rails/issues/49227 It confirms that "CurrentAttributes are cleared when a job gets executed inline in test".

This means inline tests of ActiveJobs are broken. Side effects on other gems, like GoodJob have yet to be determined. I am checking this now...