discourse/spec/jobs/create_linked_topic_spec.rb
Alan Guo Xiang Tan 8a7b62b126
DEV: Fix threading error when running jobs immediately in system tests (#19811)
```
class Jobs::DummyDelayedJob < Jobs::Base
  def execute(args = {})
  end
end

RSpec.describe "Jobs.run_immediately!" do
  before { Jobs.run_immediately! }

  it "explodes" do
    current_user = Fabricate(:user)
    Jobs.enqueue_in(1.seconds, :dummy_delayed_job)
    sign_in(current_user)
  end
end
```

The test above will fail with the following error if `ActiveRecord::Base.connection_handler.clear_active_connections!` is called before the configured Capybara server checks out a connection from the connection pool.

```
     ActiveRecord::ActiveRecordError:
       Cannot expire connection, it is owned by a different thread: #<Thread:0x00007f437391df58@puma srv tp 001 /home/tgxworld/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/puma-6.0.2/lib/puma/thread_pool.rb:106 sleep_forever>. Current thread: #<Thread:0x00007f437d6cfc60 run>.
```

We're not exactly sure if this is an ActiveRecord bug or not but we've
invested too much time into investigating this problem. Fundamentally,
we also no longer understand why `ActiveRecord::Base.connection_handler.clear_active_connections!` is being called in an ensure block
within `Jobs::Base#perform` which was added in
ceddb6e0da 10 years ago. This
commit moves the logic for running jobs immediately out of the
`Jobs::Base#perform` method into another `Jobs::Base#perform_immediately` method such that
`ActiveRecord::Base.connection_handler.clear_active_connections!` is not
called. This change will only impact the test environment.
2023-01-10 13:41:25 +08:00

73 lines
2.3 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Jobs::CreateLinkedTopic do
it "returns when the post cannot be found" do
expect { Jobs::CreateLinkedTopic.new.execute(post_id: 1) }.not_to raise_error
end
context "with a post" do
fab!(:category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:user_1) { Fabricate(:user) }
fab!(:user_2) { Fabricate(:user) }
let :watching do
TopicUser.notification_levels[:watching]
end
let :tracking do
TopicUser.notification_levels[:tracking]
end
let :muted do
TopicUser.notification_levels[:muted]
end
before do
SiteSetting.auto_close_topics_create_linked_topic = true
Fabricate(:topic_user, notification_level: tracking, topic: topic, user: user_1)
Fabricate(:topic_user, notification_level: muted, topic: topic, user: user_2)
end
it "creates a linked topic" do
small_action_post =
Fabricate(
:post,
topic: topic,
post_type: Post.types[:small_action],
action_code: "closed.enabled",
)
Jobs::CreateLinkedTopic.new.execute(post_id: post.id)
raw_title = topic.title
topic.reload
new_topic = Topic.last
linked_topic = new_topic.linked_topic
expect(topic.title).to include(
I18n.t("create_linked_topic.topic_title_with_sequence", topic_title: raw_title, count: 1),
)
expect(topic.posts.last.raw).to include(
I18n.t(
"create_linked_topic.small_action_post_raw",
new_title: "[#{new_topic.title}](#{new_topic.url})",
),
)
expect(new_topic.title).to include(
I18n.t("create_linked_topic.topic_title_with_sequence", topic_title: raw_title, count: 2),
)
expect(new_topic.first_post.raw).to include(topic.url)
expect(new_topic.category.id).to eq(category.id)
expect(new_topic.topic_users.count).to eq(3)
expect(new_topic.topic_users.pluck(:notification_level)).to contain_exactly(
muted,
tracking,
watching,
)
expect(linked_topic.topic_id).to eq(new_topic.id)
expect(linked_topic.original_topic_id).to eq(topic.id)
expect(linked_topic.sequence).to eq(2)
end
end
end