Skip to content

Commit

Permalink
Make tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
sudoremo committed Dec 12, 2024
1 parent 5649261 commit 8ac6c88
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
20 changes: 20 additions & 0 deletions test/lib/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def self.env
class WorkhorseTest < ActiveSupport::TestCase
def setup
remove_pids!
clear_locks_and_db_threads!
Workhorse.silence_watcher = true
Workhorse::DbJob.delete_all
end
Expand All @@ -43,6 +44,21 @@ def setup

attr_reader :daemon

def clear_locks_and_db_threads!
Workhorse::DbJob.connection.execute("INSTALL SONAME 'metadata_lock_info'")

Workhorse::DbJob.connection.execute('SELECT RELEASE_ALL_LOCKS()')

locking_pids = Workhorse::DbJob.connection.execute(<<~SQL.squish).to_a.flatten
SELECT THREAD_ID FROM information_schema.metadata_lock_info
WHERE THREAD_ID != connection_id()
SQL

locking_pids.each { |pid| Workhorse::DbJob.connection.execute("KILL #{pid}") }

Workhorse::DbJob.connection.execute('SELECT RELEASE_ALL_LOCKS()')
end

def remove_pids!
Dir[Rails.root.join('tmp', 'pids', '*')].each do |file|
FileUtils.rm file
Expand Down Expand Up @@ -74,13 +90,16 @@ def capture_log(level: :debug)
def work(time = 2, options = {})
options[:pool_size] ||= 5
options[:polling_interval] ||= 1
options[:auto_terminate] = options.fetch(:auto_terminate, false)

with_worker(options) do
sleep time
end
end

def work_until(max: 50, interval: 0.1, **options, &block)
options[:auto_terminate] = options.fetch(:auto_terminate, false)

w = Workhorse::Worker.new(**options)
w.start
return with_retries(max, interval: interval, &block)
Expand Down Expand Up @@ -143,6 +162,7 @@ def capture_stderr
username: ENV.fetch('DB_USERNAME', nil) || 'root',
password: ENV.fetch('DB_PASSWORD', nil) || '',
host: ENV.fetch('DB_HOST', nil) || '127.0.0.1',
port: ENV.fetch('DB_PORT', nil) || 3306,
pool: 10
)

Expand Down
10 changes: 3 additions & 7 deletions test/workhorse/db_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
class Workhorse::DbJobTest < WorkhorseTest
def test_reset_succeeded
job = Workhorse.enqueue(BasicJob.new(sleep_time: 0))
work 0.5
job.reload
assert_equal 'succeeded', job.state

work_until { assert_equal 'succeeded', job.reload.state }
job.reset!

assert_clean job
assert_clean job.reload
end

def test_reset_failed
job = Workhorse.enqueue FailingTestJob
job = Workhorse.enqueue FailingTestJob.new
work 0.5
job.reload
assert_equal 'failed', job.state
Expand Down
2 changes: 1 addition & 1 deletion test/workhorse/performer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_success
end

def test_exception
Workhorse.enqueue FailingTestJob
Workhorse.enqueue FailingTestJob.new
work 0.2, polling_interval: 0.2
assert_equal 'failed', Workhorse::DbJob.first.state
end
Expand Down
29 changes: 15 additions & 14 deletions test/workhorse/poller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,26 +219,27 @@ def test_clean_stuck_jobs_running
[true, false].each do |clean|
Workhorse::DbJob.delete_all

Workhorse.clean_stuck_jobs = true
Workhorse.clean_stuck_jobs = clean
with_daemon do
Workhorse.enqueue BasicJob.new(sleep_time: 5)
sleep 0.2
kill_deamon_workers

assert_equal 1, Workhorse::DbJob.count
assert_equal 'started', Workhorse::DbJob.first.state
with_retries do
assert_equal 'started', Workhorse::DbJob.first.state
end

work 0.1 if clean
kill_deamon_workers

assert_equal 1, Workhorse::DbJob.count
assert_equal 'started', Workhorse::DbJob.first.state

Workhorse::DbJob.first.tap do |job|
if clean
assert_equal 'failed', job.state
assert_match(/started by PID #{daemon.workers.first.pid}/, job.last_error)
assert_match(/on host #{Socket.gethostname}/, job.last_error)
else
assert_equal 'started', job.state
work_until do
Workhorse::DbJob.first.tap do |job|
if clean
assert_equal 'failed', job.state
assert_match(/started by PID #{daemon.workers.first.pid}/, job.last_error)
assert_match(/on host #{Socket.gethostname}/, job.last_error)
else
assert_equal 'started', job.state
end
end
end
end
Expand Down

0 comments on commit 8ac6c88

Please sign in to comment.