Skip to content

Commit

Permalink
use a single local context
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 8, 2023
1 parent d113122 commit a64abeb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 50 deletions.
13 changes: 3 additions & 10 deletions lib/datadog/ci/context/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module CI
module Context
class Local
def initialize
@key = "datadog_ci_active_test_#{Local.next_instance_id}"
@key = :datadog_ci_active_test

self.active_test = nil
end

def activate_test!(test)
Expand Down Expand Up @@ -39,15 +41,6 @@ def active_test
Thread.current[@key]
end

UNIQUE_INSTANCE_MUTEX = Mutex.new
UNIQUE_INSTANCE_GENERATOR = Datadog::Core::Utils::Sequence.new

private_constant :UNIQUE_INSTANCE_MUTEX, :UNIQUE_INSTANCE_GENERATOR

def self.next_instance_id
UNIQUE_INSTANCE_MUTEX.synchronize { UNIQUE_INSTANCE_GENERATOR.next }
end

private

def active_test=(test)
Expand Down
7 changes: 1 addition & 6 deletions sig/datadog/ci/context/local.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Datadog
module CI
module Context
class Local
@key: String
@key: Symbol

def initialize: () -> void

Expand All @@ -12,11 +12,6 @@ module Datadog

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

UNIQUE_INSTANCE_MUTEX: untyped
UNIQUE_INSTANCE_GENERATOR: untyped

def self.next_instance_id: () -> untyped

private

def active_test=: (Datadog::CI::Test? test) -> untyped
Expand Down
40 changes: 6 additions & 34 deletions spec/datadog/ci/context/local_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
let(:ci_test) { Datadog::CI::Test.new(tracer_span) }
let(:ci_test2) { Datadog::CI::Test.new(tracer_span) }

def fiber_active_tests
Thread.current.keys.select { |k| k.to_s.start_with?("datadog_ci_active_test_") }
end

describe "#activate_test!" do
context "when a test is already active" do
it "raises an error" do
Expand Down Expand Up @@ -43,26 +39,14 @@ def fiber_active_tests
end
end

context "with multiple local contexts" do
let(:local_context_1) { described_class.new }
let(:local_context_2) { described_class.new }

it "does not share the active test" do
local_context_1.activate_test!(ci_test)
local_context_2.activate_test!(ci_test2)

expect(local_context_1.active_test).to be(ci_test)
expect(local_context_2.active_test).to be(ci_test2)
end
end

context "with multiple fibers" do
it "create one fiber-local variable per fiber" do
subject.activate_test!(ci_test)

Fiber.new do
expect { subject.activate_test!(ci_test2) }
.to change { fiber_active_tests.size }.from(0).to(1)
subject.activate_test!(ci_test2)

expect(subject.active_test).to be(ci_test2)
end.resume

expect(subject.active_test).to be(ci_test)
Expand All @@ -74,8 +58,9 @@ def fiber_active_tests
subject.activate_test!(ci_test)

Thread.new do
expect { subject.activate_test!(ci_test2) }
.to change { fiber_active_tests.size }.from(0).to(1)
subject.activate_test!(ci_test2)

expect(subject.active_test).to be(ci_test2)
end.join

expect(subject.active_test).to be(ci_test)
Expand Down Expand Up @@ -123,18 +108,5 @@ def fiber_active_tests
expect(subject.active_test).to be(ci_test)
end
end

context "with multiple local contexts" do
let(:local_context_1) { described_class.new }
let(:local_context_2) { described_class.new }

it "does not share the active test" do
local_context_1.activate_test!(ci_test)
local_context_2.activate_test!(ci_test2)

expect(local_context_1.active_test).to be(ci_test)
expect(local_context_2.active_test).to be(ci_test2)
end
end
end
end

0 comments on commit a64abeb

Please sign in to comment.