Skip to content

Commit

Permalink
very rough idea with storing test retry callback in Thread.current to…
Browse files Browse the repository at this point in the history
… make it accessible in Minitest hooks
  • Loading branch information
anmarchenko committed Aug 8, 2024
1 parent b891aaf commit 3225649
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/datadog/ci/contrib/minitest/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def init_plugins(*args)
test_visibility_component.start_test_module(Ext::FRAMEWORK)
end

def run_one_method(klass, method_name)
return super unless datadog_configuration[:enabled]

result = nil
# retries here
test_retries_component.with_retries do |test_finished_callback|
Thread.current[:__dd_retry_callback] = test_finished_callback

result = super
end
result
end

private

def datadog_configuration
Expand All @@ -37,6 +50,10 @@ def datadog_configuration
def test_visibility_component
Datadog.send(:components).test_visibility
end

def test_retries_component
Datadog.send(:components).test_retries
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/datadog/ci/contrib/minitest/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def after_teardown
test_span = test_visibility_component.active_test
return super unless test_span

finish_with_result(test_span, result_code)
finish_with_result(test_span, result_code, Thread.current[:__dd_retry_callback])
if Helpers.parallel?(self.class)
finish_with_result(test_span.test_suite, result_code)
end
Expand All @@ -60,7 +60,7 @@ def after_teardown

private

def finish_with_result(span, result_code)
def finish_with_result(span, result_code, callback = nil)
return unless span

case result_code
Expand All @@ -71,6 +71,9 @@ def finish_with_result(span, result_code)
when "S"
span.skipped!(reason: failure.message)
end

callback.call(span) if callback

span.finish
end

Expand Down
43 changes: 43 additions & 0 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -948,4 +948,47 @@ def test_with_background_thread
)
end
end

context "with flaky test and test retries enabled" do
include_context "CI mode activated" do
let(:integration_name) { :minitest }

let(:flaky_test_retries_enabled) { true }
end

before do
Minitest.run([])
end

before(:context) do
Thread.current[:dd_coverage_collector] = nil

Minitest::Runnable.reset

require_relative "helpers/addition_helper"
class SomeTestWithThreads < Minitest::Test
def test_with_background_thread
# add thread to test that code coverage is collected
t = Thread.new do
AdditionHelper.add(1, 2)
end
t.join
assert true
end
end
end

it "does not cover the background thread" do
skip if PlatformHelpers.jruby?

expect(test_spans).to have(1).item
expect(coverage_events).to have(1).item

# expect that background thread is not covered
cov_event = find_coverage_for_test(first_test_span)
expect(cov_event.coverage.keys).not_to include(
absolute_path("helpers/addition_helper.rb")
)
end
end
end

0 comments on commit 3225649

Please sign in to comment.