Skip to content

Commit

Permalink
add one more test case that confirms that background jobs are covered…
Browse files Browse the repository at this point in the history
… by multithreading code coverage
  • Loading branch information
anmarchenko committed Jun 7, 2024
1 parent e892c14 commit bdbd256
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion spec/datadog/ci/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

context "when the worker has started" do
context "when the worker is running" do
let(:queue) { Queue.new }
let(:queue) { Thread::Queue.new }
subject(:worker) { described_class.new { queue.pop } }

it do
Expand Down
31 changes: 29 additions & 2 deletions spec/ddcov/ddcov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def thread_local_cov
let(:threading_mode) { :single }

it "collects coverage for each thread separately" do
t1_queue = Queue.new
t2_queue = Queue.new
t1_queue = Thread::Queue.new
t2_queue = Thread::Queue.new

t1 = Thread.new do
cov = thread_local_cov
Expand Down Expand Up @@ -230,6 +230,33 @@ def thread_local_cov
expect(coverage.keys).to include(absolute_path("calculator/operations/add.rb"))
expect(coverage.keys).to include(absolute_path("calculator/operations/multiply.rb"))
end

it "collects coverage for background threads that started before the coverage collection" do
jobs_queue = Thread::Queue.new
background_jobs_worker = Thread.new do
loop do
job = jobs_queue.pop
break if job == :done

job.call
end
end

cov = described_class.new(root: root, threading_mode: :multi)
cov.start

jobs_queue << -> { expect(calculator.add(1, 2)).to eq(3) }
jobs_queue << -> { expect(calculator.multiply(1, 2)).to eq(2) }

jobs_queue << :done

background_jobs_worker.join

coverage = cov.stop
expect(coverage.size).to eq(2)
expect(coverage.keys).to include(absolute_path("calculator/operations/add.rb"))
expect(coverage.keys).to include(absolute_path("calculator/operations/multiply.rb"))
end
end

context "when threading mode is invalid" do
Expand Down

0 comments on commit bdbd256

Please sign in to comment.