Skip to content

Commit

Permalink
di code tracker test
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed Jul 18, 2024
1 parent aa2cfe7 commit 9e388f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/datadog/di/code_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module DI
# recreated when the DI component is created.
class CodeTracker
def initialize
@file_registry = Concurrent::Map.new
@registry = Concurrent::Map.new
end

def start
Expand All @@ -37,10 +37,10 @@ def start
#
# For now just map the path to the instruction sequence.
path = tp.instruction_sequence.path
file_registry[path] = tp.instruction_sequence
registry[path] = tp.instruction_sequence

# TODO fix this to properly deal with paths
file_registry[File.basename(path)] = tp.instruction_sequence
registry[File.basename(path)] = tp.instruction_sequence

DI.component&.hook_manager&.install_pending_line_hooks(path)
end
Expand All @@ -56,7 +56,7 @@ def active?
# Returns the RubVM::InstructionSequence (i.e. the compiled code)
# for the provided path.
def [](path)
file_registry[path]
registry[path]
end

def stop
Expand All @@ -65,12 +65,14 @@ def stop
# Clear the instance variable so that the trace point may be
# reinstated in the future.
@compiled_trace_point = nil
file_registry.clear
registry.clear
end

private

attr_reader :file_registry
# Mapping from paths of loaded files to RubyVM::InstructionSequence
# objects representing compiled code of those files.
attr_reader :registry
end
end
end
41 changes: 41 additions & 0 deletions spec/datadog/di/code_tracker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'datadog/di/code_tracker'

RSpec.describe Datadog::DI::CodeTracker do
let(:tracker) do
described_class.new
end

describe '.new' do
it 'creates an instance' do
expect(tracker).to be_a(described_class)
end
end

describe '#start' do
after do
tracker.stop
end

it 'tracks loaded files' do
# The expectations appear to be lazy-loaded, therefore
# we need to invoke the same expectation before starting
# code tracking as we'll be using later in the test.
expect(tracker.send(:registry)).to be_empty
tracker.start
# Should still be empty here.
expect(tracker.send(:registry)).to be_empty
require_relative 'code_tracker_test_class_1'
# TODO due to a hack we currently have 2 entries for every file,
# one with full path and one with basename only.
expect(tracker.send(:registry).each.to_a.length).to eq(2)

path = tracker.send(:registry).each.to_a.first.first
# The full path is dependent on the environment/system
# running the tests, but we can assert on the basename
# which will be the same.
expect(File.basename(path)).to eq('code_tracker_test_class_1.rb')
# And, we should in fact have a full path.
expect(path).to start_with('/')
end
end
end
2 changes: 2 additions & 0 deletions spec/datadog/di/code_tracker_test_class_1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class CodeTrackerTestClass1
end

0 comments on commit 9e388f1

Please sign in to comment.