Skip to content

Commit

Permalink
Simplify tests and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
rosa committed Nov 20, 2023
1 parent 858d3a4 commit f410cf6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 58 deletions.
23 changes: 0 additions & 23 deletions test/fixtures/solid_queue/jobs.yml
Original file line number Diff line number Diff line change
@@ -1,23 +0,0 @@
add_to_buffer_job:
queue_name: background
active_job_id: b55784ad-ff9c-4c02-8d6c-016089bb3e09
class_name: AddToBufferJob
arguments: "{\"job_class\":\"AddToBufferJob\", \"job_id\":\"b55784ad-ff9c-4c02-8d6c-016089bb3e09\", \"provider_job_id\":null, \"queue_name\":\"background\", \"priority\":0, \"arguments\":[42], \"executions\":0, \"exception_executions\":{}, \"locale\":\"en\", \"timezone\":\"UTC\", \"enqueued_at\":\"2023-02-23T09:54:28Z\"}"
priority: 0

raising_job:
queue_name: background
class_name: RaisingJob
active_job_id: 60c93269-c3d0-44b1-958e-826cd145c456
arguments: "{\"job_class\":\"RaisingJob\",\"job_id\":\"60c93269-c3d0-44b1-958e-826cd145c456\",\"provider_job_id\":null,\"queue_name\":\"background\",\"priority\":0,\"arguments\":[{\"_aj_serialized\":\"ActiveJob::Serializers::ModuleSerializer\", \"value\":\"RuntimeError\"}, 2],\"executions\":0,\"exception_executions\":{},\"locale\":\"en\",\"timezone\":\"UTC\",\"enqueued_at\":\"2023-02-23T09:36:04Z\"}"
priority: 0

<% 5.times do |i| %>
<% active_job_id = SecureRandom.uuid %>
job_<%= i %>:
queue_name: fixtures
active_job_id: <%= active_job_id %>
class_name: AddToBufferJob
arguments: "{\"job_class\":\"AddToBufferJob\",\"job_id\":\"<%= active_job_id %>\",\"queue_name\":\"fixtures\",\"priority\":<%= i %>,\"arguments\":[],\"executions\":0,\"exception_executions\":{},\"locale\":\"en\",\"timezone\":\"UTC\",\"enqueued_at\":\"2023-02-20T10:33:02Z\"}"
priority: <%= i %>
<% end %>
20 changes: 11 additions & 9 deletions test/models/solid_queue/claimed_execution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
end

test "perform job successfully" do
job = solid_queue_jobs(:add_to_buffer_job)
claimed_execution = prepare_and_claim_job(job)
claimed_execution = prepare_and_claim_job AddToBufferJob.perform_later(42)
job = claimed_execution.job
assert_not job.finished?

assert_difference -> { SolidQueue::ClaimedExecution.count }, -1 do
claimed_execution.perform
Expand All @@ -20,8 +21,8 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
end

test "perform job that fails" do
job = solid_queue_jobs(:raising_job)
claimed_execution = prepare_and_claim_job(job)
claimed_execution = prepare_and_claim_job RaisingJob.perform_later(RuntimeError, 2)
job = claimed_execution.job

assert_difference -> { SolidQueue::ClaimedExecution.count } => -1, -> { SolidQueue::FailedExecution.count } => 1 do
claimed_execution.perform
Expand All @@ -40,8 +41,7 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
subscriber = ErrorBuffer.new

with_error_subscriber(subscriber) do
job = solid_queue_jobs(:raising_job)
claimed_execution = prepare_and_claim_job(job)
claimed_execution = prepare_and_claim_job RaisingJob.perform_later(RuntimeError, 2)

claimed_execution.perform
end
Expand All @@ -51,8 +51,8 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
end

test "release" do
job = solid_queue_jobs(:add_to_buffer_job)
claimed_execution = prepare_and_claim_job(job)
claimed_execution = prepare_and_claim_job AddToBufferJob.perform_later(42)
job = claimed_execution.job

assert_difference -> { SolidQueue::ClaimedExecution.count } => -1, -> { SolidQueue::ReadyExecution.count } => 1 do
claimed_execution.release
Expand All @@ -62,7 +62,9 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
end

private
def prepare_and_claim_job(job)
def prepare_and_claim_job(active_job)
job = SolidQueue::Job.find_by(active_job_id: active_job.job_id)

job.prepare_for_execution
job.reload.ready_execution.claim(@process.id)
job.reload.claimed_execution
Expand Down
49 changes: 25 additions & 24 deletions test/models/solid_queue/ready_execution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

class SolidQueue::ReadyExecutionTest < ActiveSupport::TestCase
setup do
@jobs = SolidQueue::Job.where(queue_name: "fixtures")
@jobs.each(&:prepare_for_execution)
5.times do |i|
AddToBufferJob.set(queue: "backend", priority: 5 - i).perform_later(i)
end

@jobs = SolidQueue::Job.where(queue_name: "backend")
end

test "claim all jobs for existing queue" do
assert_claimed_jobs(@jobs.count) do
SolidQueue::ReadyExecution.claim("fixtures", @jobs.count + 1, 42)
SolidQueue::ReadyExecution.claim("backend", @jobs.count + 1, 42)
end

@jobs.each do |job|
Expand All @@ -25,7 +28,7 @@ class SolidQueue::ReadyExecutionTest < ActiveSupport::TestCase

test "claim some jobs for existing queue" do
assert_claimed_jobs(2) do
SolidQueue::ReadyExecution.claim("fixtures", 2, 42)
SolidQueue::ReadyExecution.claim("backend", 2, 42)
end

@jobs.order(:priority).first(2).each do |job|
Expand All @@ -40,8 +43,8 @@ class SolidQueue::ReadyExecutionTest < ActiveSupport::TestCase
end

test "claim individual job" do
job = solid_queue_jobs(:add_to_buffer_job)
job.prepare_for_execution
AddToBufferJob.perform_later("hey")
job = SolidQueue::Job.last

assert_claimed_jobs(1) do
job.ready_execution.claim(42)
Expand All @@ -52,48 +55,46 @@ class SolidQueue::ReadyExecutionTest < ActiveSupport::TestCase
end

test "claim jobs using a list of queues" do
(SolidQueue::Job.all - @jobs).each(&:prepare_for_execution)
AddToBufferJob.perform_later("hey")

assert_claimed_jobs(SolidQueue::Job.count) do
SolidQueue::ReadyExecution.claim(%w[ fixtures background ], SolidQueue::Job.count + 1, 42)
assert_claimed_jobs(6) do
SolidQueue::ReadyExecution.claim(%w[ backend background ], SolidQueue::Job.count + 1, 42)
end
end

test "claim jobs using a wildcard" do
(SolidQueue::Job.all - @jobs).each(&:prepare_for_execution)
AddToBufferJob.perform_later("hey")

assert_claimed_jobs(SolidQueue::Job.count) do
assert_claimed_jobs(6) do
SolidQueue::ReadyExecution.claim("*", SolidQueue::Job.count + 1, 42)
end
end

test "claim jobs using a wildcard and having paused queues" do
other_jobs = SolidQueue::Job.all - @jobs
other_jobs.each(&:prepare_for_execution)
AddToBufferJob.perform_later("hey")

SolidQueue::Queue.find_by_name("fixtures").pause
SolidQueue::Queue.find_by_name("backend").pause

assert_claimed_jobs(other_jobs.count) do
assert_claimed_jobs(1) do
SolidQueue::ReadyExecution.claim("*", SolidQueue::Job.count + 1, 42)
end
end

test "claim jobs using queue prefixes" do
assert_claimed_jobs(2) do
SolidQueue::ReadyExecution.claim("fix*", 2, 42)
end
AddToBufferJob.perform_later("hey")

@jobs.order(:priority).first(2).each do |job|
assert_not job.reload.ready?
assert job.claimed?
assert_claimed_jobs(1) do
SolidQueue::ReadyExecution.claim("backgr*", SolidQueue::Job.count + 1, 42)
end

assert @jobs.none?(&:claimed?)
end

test "claim jobs using both exact names and prefixes" do
(SolidQueue::Job.all - @jobs).each(&:prepare_for_execution)
AddToBufferJob.perform_later("hey")

assert_claimed_jobs(SolidQueue::Job.count) do
SolidQueue::ReadyExecution.claim(%w[ fix* background ], SolidQueue::Job.count + 1, 42)
assert_claimed_jobs(6) do
SolidQueue::ReadyExecution.claim(%w[ backe* background ], SolidQueue::Job.count + 1, 42)
end
end

Expand Down
2 changes: 0 additions & 2 deletions test/unit/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class QueueTest < ActiveSupport::TestCase
setup do
SolidQueue::Job.delete_all

5.times do
AddToBufferJob.perform_later "hey!"
end
Expand Down

0 comments on commit f410cf6

Please sign in to comment.