Skip to content

Commit

Permalink
ConcurrentSpan tests, add missing test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 24, 2023
1 parent 93138cf commit 89ef625
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/datadog/ci/ext/app_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Ext
module AppTypes
TYPE_TEST = "test"
TYPE_TEST_SESSION = "test_session_end"

CI_SPAN_TYPES = [TYPE_TEST, TYPE_TEST_SESSION].freeze
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/app_types.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Datadog
module AppTypes
TYPE_TEST: "test"
TYPE_TEST_SESSION: "test_session_end"

CI_SPAN_TYPES: Array[String]
end
end
end
Expand Down
118 changes: 118 additions & 0 deletions spec/datadog/ci/concurrent_span_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
THREADS_COUNT = 10
REPEAT_COUNT = 20

RSpec.describe Datadog::CI::ConcurrentSpan do
describe "#finish" do
it "calls SpanOperation#stop once" do
REPEAT_COUNT.times do
tracer_span = Datadog::Tracing::SpanOperation.new("operation")
ci_span = described_class.new(tracer_span)

expect(tracer_span).to receive(:stop).once

(1..THREADS_COUNT).map do
Thread.new do
ci_span.finish
end
end.map(&:join)
end
end
end

# the following tests make sure that ConcurrentSpan works exactly like Span
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, name: "span_name", type: "test") }
subject(:span) { described_class.new(tracer_span) }

describe "#passed!" do
it "sets the status to PASS" do
expect(tracer_span).to receive(:set_tag).with("test.status", "pass")

span.passed!
end
end

describe "#failed!" do
context "when exception is nil" do
it "sets the status to FAIL" do
expect(tracer_span).to receive(:status=).with(1)
expect(tracer_span).to receive(:set_tag).with("test.status", "fail")

span.failed!
end
end

context "when exception is provided" do
it "sets the status to FAIL" do
expect(tracer_span).to receive(:status=).with(1)
expect(tracer_span).to receive(:set_tag).with("test.status", "fail")
expect(tracer_span).to receive(:set_error).with("error")

span.failed!(exception: "error")
end
end
end

describe "#skipped!" do
context "when exception is nil" do
it "sets the status to SKIP" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).not_to receive(:set_error)

span.skipped!
end
end

context "when exception is provided" do
it "sets the status to SKIP and sets error" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).to receive(:set_error).with("error")

span.skipped!(exception: "error")
end
end

context "when reason is nil" do
it "doesn't set the skip reason tag" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).not_to receive(:set_tag).with("test.skip_reason", "reason")

span

span.skipped!
end
end

context "when reason is provided" do
it "sets the skip reason tag" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).to receive(:set_tag).with("test.skip_reason", "reason")

span.skipped!(reason: "reason")
end
end
end

describe "#set_tag" do
it "sets the tag" do
expect(tracer_span).to receive(:set_tag).with("foo", "bar")

span.set_tag("foo", "bar")
end
end

describe "#set_tags" do
it "sets the tags" do
expect(tracer_span).to receive(:set_tags).with({"foo" => "bar", "baz" => "qux"})

span.set_tags("foo" => "bar", "baz" => "qux")
end
end

describe "#set_metric" do
it "sets the metric" do
expect(tracer_span).to receive(:set_metric).with("foo", "bar")

span.set_metric("foo", "bar")
end
end
end
6 changes: 5 additions & 1 deletion spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,11 @@
recorder.start_test_session(service_name: service)
end

before { deactivate_test_session }
before do
ci_session

deactivate_test_session
end

it { expect(recorder.active_test_session).to be_nil }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
end

before do
produce_test_session_trace
produce_test_session_trace(with_http_span: true)
end

subject { described_class.serializer(trace, ci_span) }
Expand All @@ -24,5 +24,10 @@
let(:ci_span) { first_test_span }
it { is_expected.to be_kind_of(Datadog::CI::TestVisibility::Serializers::TestV2) }
end

context "with a http request span" do
let(:ci_span) { first_other_span }
it { is_expected.to be_kind_of(Datadog::CI::TestVisibility::Serializers::Span) }
end
end
end
24 changes: 24 additions & 0 deletions spec/datadog/ci/test_visibility/serializers/test_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,28 @@
end
end
end

describe "#valid?" do
context "test_session_id" do
before do
produce_test_session_trace
end

context "when test_session_id is not nil" do
it "returns true" do
expect(subject.valid?).to eq(true)
end
end

context "when test_session_id is nil" do
before do
test_session_span.clear_tag("_test.session_id")
end

it "returns false" do
expect(subject.valid?).to eq(false)
end
end
end
end
end
24 changes: 24 additions & 0 deletions spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@
end
end
end

describe "#valid?" do
context "test_session_id" do
before do
produce_test_session_trace
end

context "when test_session_id is not nil" do
it "returns true" do
expect(subject.valid?).to eq(true)
end
end

context "when test_session_id is nil" do
before do
first_test_span.clear_tag("_test.session_id")
end

it "returns false" do
expect(subject.valid?).to eq(false)
end
end
end
end
end
6 changes: 5 additions & 1 deletion spec/support/tracer_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def produce_test_session_trace(
skip_reason: nil, start_time: Time.now, duration_seconds: 2,
with_http_span: false
)
allow(Process).to receive(:clock_gettime).and_return(
0, duration_seconds, 2 * duration_seconds, 3 * duration_seconds
)

test_session = Datadog::CI.start_test_session(
service_name: service,
tags: {
Expand Down Expand Up @@ -90,7 +94,7 @@ def first_test_span
end

def first_other_span
spans.find { |span| span.type != "test" }
spans.find { |span| !Datadog::CI::Ext::AppTypes::CI_SPAN_TYPES.include?(span.type) }
end

# Returns spans and caches it (similar to +let(:spans)+).
Expand Down

0 comments on commit 89ef625

Please sign in to comment.