Skip to content

Commit

Permalink
Add common ThreadingHelper module for specs (mastodon#29116)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored and Ember-ruby committed Mar 19, 2024
1 parent a8b7800 commit d850eb9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 57 deletions.
14 changes: 5 additions & 9 deletions spec/lib/request_pool_spec.rb
Expand Up @@ -33,18 +33,14 @@

subject

threads = Array.new(5) do
Thread.new do
subject.with('http://example.com') do |http_client|
http_client.get('/').flush
# Nudge scheduler to yield and exercise the full pool
sleep(0.01)
end
multi_threaded_execution(5) do
subject.with('http://example.com') do |http_client|
http_client.get('/').flush
# Nudge scheduler to yield and exercise the full pool
sleep(0.01)
end
end

threads.map(&:join)

expect(subject.size).to be > 1
end

Expand Down
13 changes: 2 additions & 11 deletions spec/models/account_spec.rb
Expand Up @@ -1035,19 +1035,10 @@
it 'increments the count in multi-threaded an environment when account_stat is not yet initialized' do
subject

increment_by = 15
wait_for_start = true

threads = Array.new(increment_by) do
Thread.new do
true while wait_for_start
described_class.find(subject.id).increment_count!(:followers_count)
end
multi_threaded_execution(15) do
described_class.find(subject.id).increment_count!(:followers_count)
end

wait_for_start = false
threads.each(&:join)

expect(subject.reload.followers_count).to eq 15
end
end
Expand Down
30 changes: 8 additions & 22 deletions spec/models/concerns/account/counters_spec.rb
Expand Up @@ -6,31 +6,26 @@
let!(:account) { Fabricate(:account) }

describe '#increment_count!' do
let(:increment_by) { 15 }

it 'increments the count' do
expect(account.followers_count).to eq 0
account.increment_count!(:followers_count)
expect(account.followers_count).to eq 1
end

it 'increments the count in multi-threaded an environment' do
increment_by = 15
wait_for_start = true

threads = Array.new(increment_by) do
Thread.new do
true while wait_for_start
account.increment_count!(:statuses_count)
end
multi_threaded_execution(increment_by) do
account.increment_count!(:statuses_count)
end

wait_for_start = false
threads.each(&:join)

expect(account.statuses_count).to eq increment_by
end
end

describe '#decrement_count!' do
let(:decrement_by) { 10 }

it 'decrements the count' do
account.followers_count = 15
account.save!
Expand All @@ -40,22 +35,13 @@
end

it 'decrements the count in multi-threaded an environment' do
decrement_by = 10
wait_for_start = true

account.statuses_count = 15
account.save!

threads = Array.new(decrement_by) do
Thread.new do
true while wait_for_start
account.decrement_count!(:statuses_count)
end
multi_threaded_execution(decrement_by) do
account.decrement_count!(:statuses_count)
end

wait_for_start = false
threads.each(&:join)

expect(account.statuses_count).to eq 5
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Expand Up @@ -86,6 +86,7 @@ def sign_in(resource, _deprecated = nil, scope: nil)
config.include ActiveSupport::Testing::TimeHelpers
config.include Chewy::Rspec::Helpers
config.include Redisable
config.include ThreadingHelpers
config.include SignedRequestHelpers, type: :request
config.include CommandLineHelpers, type: :cli

Expand Down
22 changes: 7 additions & 15 deletions spec/services/resolve_account_service_spec.rb
Expand Up @@ -219,27 +219,19 @@
end

it 'processes one remote account at a time using locks' do
wait_for_start = true
fail_occurred = false
return_values = Concurrent::Array.new

threads = Array.new(5) do
Thread.new do
true while wait_for_start

begin
return_values << described_class.new.call('foo@ap.example.com')
rescue ActiveRecord::RecordNotUnique
fail_occurred = true
ensure
RedisConfiguration.pool.checkin if Thread.current[:redis]
end
multi_threaded_execution(5) do
begin
return_values << described_class.new.call('foo@ap.example.com')
rescue ActiveRecord::RecordNotUnique
fail_occurred = true
ensure
RedisConfiguration.pool.checkin if Thread.current[:redis]
end
end

wait_for_start = false
threads.each(&:join)

expect(fail_occurred).to be false
expect(return_values).to_not include(nil)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/support/threading_helpers.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module ThreadingHelpers
def multi_threaded_execution(thread_count)
wait_for_start = true

threads = Array.new(thread_count) do
Thread.new do
true while wait_for_start
yield
end
end

wait_for_start = false
threads.each(&:join)
end
end

0 comments on commit d850eb9

Please sign in to comment.