|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +class SolidQueue::ConcurrencyLockTest < ActiveSupport::TestCase |
| 6 | + class NonOverlappingJob < ActiveJob::Base |
| 7 | + limits_concurrency key: "concurrency-lock-test", duration: 1.minute |
| 8 | + |
| 9 | + def perform(name) |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + setup do |
| 14 | + @process = SolidQueue::Process.register(kind: "Worker", pid: 42, name: "worker-123") |
| 15 | + end |
| 16 | + |
| 17 | + # From the reproduction in https://github.com/rails/solid_queue/issues/827 |
| 18 | + test "a job that outlived its lock does not release the lock a later job acquired" do |
| 19 | + a = claim(NonOverlappingJob.perform_later("A")) |
| 20 | + |
| 21 | + travel_to 2.minutes.from_now |
| 22 | + SolidQueue::Semaphore.expired.delete_all |
| 23 | + |
| 24 | + b = claim(NonOverlappingJob.perform_later("B")) |
| 25 | + c = job_for(NonOverlappingJob.perform_later("C")) |
| 26 | + assert c.blocked? |
| 27 | + |
| 28 | + a.perform |
| 29 | + |
| 30 | + assert b.job.reload.claimed? |
| 31 | + assert c.reload.blocked? |
| 32 | + end |
| 33 | + |
| 34 | + test "a job that outlived its lock does not leave the key over its limit" do |
| 35 | + a = claim(NonOverlappingJob.perform_later("A")) |
| 36 | + |
| 37 | + travel_to 2.minutes.from_now |
| 38 | + SolidQueue::Semaphore.expired.delete_all |
| 39 | + |
| 40 | + b = claim(NonOverlappingJob.perform_later("B")) |
| 41 | + c = job_for(NonOverlappingJob.perform_later("C")) |
| 42 | + d = job_for(NonOverlappingJob.perform_later("D")) |
| 43 | + |
| 44 | + a.perform |
| 45 | + b.perform |
| 46 | + claim_job(c) |
| 47 | + |
| 48 | + assert d.reload.blocked? |
| 49 | + end |
| 50 | + |
| 51 | + test "a job that got its slot gives it back even if its class is missing when it runs" do |
| 52 | + a = claim(NonOverlappingJob.perform_later("A")) |
| 53 | + b = job_for(NonOverlappingJob.perform_later("B")) |
| 54 | + assert b.blocked? |
| 55 | + |
| 56 | + # A worker on a release without the class claims and runs A |
| 57 | + a.job.update_column(:class_name, "RemovedJobClass") |
| 58 | + a = SolidQueue::ClaimedExecution.find(a.id) |
| 59 | + |
| 60 | + assert_raises(SolidQueue::Job::ClassMissingError) { a.perform } |
| 61 | + |
| 62 | + assert b.reload.ready? |
| 63 | + assert_equal 0, semaphore.value |
| 64 | + end |
| 65 | + |
| 66 | + test "a job that skipped its limits because its class was missing doesn't give back a slot" do |
| 67 | + a = claim(NonOverlappingJob.perform_later("A")) |
| 68 | + b = job_for(NonOverlappingJob.perform_later("B")) |
| 69 | + |
| 70 | + # Dispatched by a process without the class, so it takes no slot |
| 71 | + x = SolidQueue::Job.create!(queue_name: "default", class_name: "RemovedJobClass", active_job_id: SecureRandom.uuid, |
| 72 | + arguments: { "job_class" => "RemovedJobClass", "arguments" => [ "X" ] }, concurrency_key: a.job.concurrency_key) |
| 73 | + assert x.ready? |
| 74 | + |
| 75 | + # ...and run by one that has it |
| 76 | + x.update_columns(class_name: NonOverlappingJob.name, arguments: x.arguments.merge("job_class" => NonOverlappingJob.name)) |
| 77 | + claim_job(x).perform |
| 78 | + |
| 79 | + assert a.job.reload.claimed? |
| 80 | + assert b.reload.blocked? |
| 81 | + end |
| 82 | + |
| 83 | + test "a job dispatched before Solid Queue recorded its semaphore gives back its slot by key" do |
| 84 | + a = claim(NonOverlappingJob.perform_later("A")) |
| 85 | + b = job_for(NonOverlappingJob.perform_later("B")) |
| 86 | + a.job.update_column(:arguments, a.job.arguments.except(SolidQueue::Job::ConcurrencyControls::CONCURRENCY_LOCK_KEY)) |
| 87 | + a = SolidQueue::ClaimedExecution.find(a.id) |
| 88 | + |
| 89 | + a.perform |
| 90 | + |
| 91 | + assert b.reload.ready? |
| 92 | + end |
| 93 | + |
| 94 | + test "a blocked job records the semaphore it gets when it's released" do |
| 95 | + a = claim(NonOverlappingJob.perform_later("A")) |
| 96 | + b = job_for(NonOverlappingJob.perform_later("B")) |
| 97 | + assert a.job.concurrency_lock["semaphore_id"].present? |
| 98 | + assert_not b.concurrency_lock_recorded? |
| 99 | + |
| 100 | + a.perform |
| 101 | + |
| 102 | + assert b.reload.ready? |
| 103 | + assert_equal semaphore.id, b.concurrency_lock["semaphore_id"] |
| 104 | + assert_equal 1, b.concurrency_lock["limit"] |
| 105 | + end |
| 106 | + |
| 107 | + test "jobs enqueued in bulk record the semaphore they get" do |
| 108 | + ActiveJob.perform_all_later([ NonOverlappingJob.new("A"), NonOverlappingJob.new("B") ]) |
| 109 | + a, b = SolidQueue::Job.where(class_name: NonOverlappingJob.name).order(:id).to_a |
| 110 | + |
| 111 | + assert a.ready? |
| 112 | + assert_equal semaphore.id, a.concurrency_lock["semaphore_id"] |
| 113 | + assert b.blocked? |
| 114 | + assert_not b.concurrency_lock_recorded? |
| 115 | + end |
| 116 | + |
| 117 | + private |
| 118 | + def semaphore |
| 119 | + SolidQueue::Semaphore.sole |
| 120 | + end |
| 121 | + |
| 122 | + def job_for(active_job) |
| 123 | + SolidQueue::Job.find_by!(active_job_id: active_job.job_id) |
| 124 | + end |
| 125 | + |
| 126 | + def claim(active_job) |
| 127 | + claim_job(job_for(active_job)) |
| 128 | + end |
| 129 | + |
| 130 | + def claim_job(job) |
| 131 | + SolidQueue::ReadyExecution.claim(job.queue_name, 1, @process.id) |
| 132 | + SolidQueue::ClaimedExecution.find_by!(job_id: job.id) |
| 133 | + end |
| 134 | +end |
0 commit comments