Skip to content

Commit

Permalink
wip broken
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 21, 2023
1 parent 1ab459c commit cb563bc
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/datadog/ci/context/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
module Datadog
module CI
module Context
# This context is shared between threads and represents the current test session.
class Global
def initialize
@mutex = Mutex.new
@test_session = nil
end

def active_test_session
@test_session
end

def activate_test_session!(test_session)
@mutex.synchronize do
raise "Nested test sessions are not supported. Currently active test session: #{@test_session}" unless @test_session.nil?
Expand All @@ -17,16 +22,8 @@ def activate_test_session!(test_session)
end
end

def deactivate_test_session!(test_session)
@mutex.synchronize do
return if @test_session.nil?

if @test_session == test_session
@test_session = nil
else
raise "Trying to deactivate test session #{test_session}, but currently active test session is #{@test_session}"
end
end
def deactivate_test_session!
@mutex.synchronize { @test_session = nil }
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative "ext/test"
require_relative "ext/environment"

require_relative "context/global"
require_relative "context/local"

require_relative "span"
Expand Down Expand Up @@ -96,6 +97,10 @@ def active_test
@local_context.active_test
end

def active_test_session
@global_context.active_test_session
end

def deactivate_test(test)
@local_context.deactivate_test!(test)
end
Expand Down
19 changes: 19 additions & 0 deletions sig/datadog/ci/context/global.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Datadog
module CI
module Context
class Global
@mutex: Thread::Mutex

@test_session: Datadog::CI::TestSession?

def initialize: () -> void

def active_test_session: () -> Datadog::CI::TestSession?

def activate_test_session!: (Datadog::CI::TestSession test_session) -> void

def deactivate_test_session!: () -> void
end
end
end
end
5 changes: 5 additions & 0 deletions sig/datadog/ci/recorder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module Datadog
class Recorder
@environment_tags: Hash[String, String]
@local_context: Datadog::CI::Context::Local
@global_context: Datadog::CI::Context::Global

attr_reader environment_tags: Hash[String, String]

def trace_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Test span) -> untyped } -> untyped

def trace: (String span_type, String span_name, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def start_test_session: (?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::TestSession

def active_test: () -> Datadog::CI::Test?

def active_span: () -> Datadog::CI::Span?
Expand All @@ -24,6 +27,8 @@ module Datadog

def build_test: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::Test

def build_test_session: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::TestSession

def build_span: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::Span
end
end
Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/test_session.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Datadog
module CI
class TestSession < Span
@mutex: Thread::Mutex
end
end
end
51 changes: 51 additions & 0 deletions spec/datadog/ci/context/global_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
RSpec.describe Datadog::CI::Context::Global do
subject { described_class.new }

let(:tracer_span) { double(Datadog::Tracing::SpanOperation, name: "test.session") }
let(:session) { Datadog::CI::TestSession.new(tracer_span) }

describe "#activate_test_session!" do
context "when a test session is already active" do
before do
subject.activate_test_session!(Datadog::CI::TestSession.new(tracer_span))
end

it "raises an error" do
expect { subject.activate_test_session!(session) }.to(
raise_error(
RuntimeError,
"Nested test sessions are not supported. Currently active test session: " \
"#{session}"
)
)
end
end

context "when no test session is active" do
it "activates the test session" do
subject.activate_test_session!(session)
expect(subject.active_test_session).to be(session)
end
end
end

describe "#deactivate_test_session!" do
context "when a test session is active" do
before do
subject.activate_test_session!(session)
end

it "deactivates the test session" do
subject.deactivate_test_session!
expect(subject.active_test_session).to be_nil
end
end

context "when no test session is active" do
it "does nothing" do
subject.deactivate_test_session!
expect(subject.active_test_session).to be_nil
end
end
end
end
26 changes: 26 additions & 0 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,30 @@ def expect_initialized_span
it { is_expected.to be_nil }
end
end

describe "#start_test_session" do
subject(:start_test_session) { recorder.start_test_session(service_name: service) }

let(:session_operation_name) { "test.session" }

let(:span_op) { Datadog::Tracing::SpanOperation.new(session_operation_name) }
let(:ci_test_session) { instance_double(Datadog::CI::TestSession) }

before do
allow(Datadog::Tracing)
.to receive(:trace)
.with(
session_operation_name,
{
span_type: Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION,
service: service
}
)
.and_return(span_op)

allow(Datadog::CI::TestSession).to receive(:new).with(span_op).and_return(ci_test_session)
end

it { is_expected.to be(ci_test_session) }
end
end

0 comments on commit cb563bc

Please sign in to comment.