Skip to content

Commit d3b854c

Browse files
committed
test that code tracker maintains mappings after datadog is loaded
1 parent 4cc2d79 commit d3b854c

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

spec/datadog/di/init_spec.rb

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,64 @@
44
RSpec.describe 'DI initializer' do
55
di_test
66

7-
context 'when loaded initially into a clean process' do
8-
it 'loads only DI code tracker' do
9-
script = <<-SCRIPT
10-
if defined?(Datadog) && Datadog.constants != %i(VERSION)
11-
raise "Datadog code loaded too early"
12-
end
7+
BOOTSTRAP_SCRIPT = <<-SCRIPT
8+
if defined?(Datadog) && Datadog.constants != %i(VERSION)
9+
raise "Datadog code loaded too early"
10+
end
1311
14-
require 'datadog/di/init'
12+
require 'datadog/di/init'
1513
16-
if Datadog.constants.sort != %i(DI VERSION)
17-
raise "Too many datadog components loaded: \#{Datadog.constants}"
18-
end
14+
if Datadog.constants.sort != %i(DI VERSION)
15+
raise "Too many datadog components loaded: \#{Datadog.constants}"
16+
end
1917
20-
unless Datadog::DI.code_tracker
21-
raise "Code tracker not instantiated"
22-
end
18+
unless Datadog::DI.code_tracker
19+
raise "Code tracker not instantiated"
20+
end
2321
24-
unless Datadog::DI.code_tracker.send(:registry).empty?
25-
raise "Code tracker registry is not empty"
26-
end
22+
unless Datadog::DI.code_tracker.send(:registry).empty?
23+
raise "Code tracker registry is not empty"
24+
end
2725
28-
# Test load something
29-
require 'open3'
26+
# Test load something
27+
require 'open3'
3028
31-
if Datadog::DI.code_tracker.send(:registry).empty?
32-
raise "Code tracker did not add loaded file to registry"
33-
end
29+
if Datadog::DI.code_tracker.send(:registry).empty?
30+
raise "Code tracker did not add loaded file to registry"
31+
end
32+
33+
unless Datadog::DI.code_tracker.send(:registry).detect { |key, value| key =~ /open3.rb\\z/ }
34+
raise "Loaded script not found in code tracker registry"
35+
end
36+
37+
if Datadog.constants.sort != %i(DI VERSION)
38+
raise "Too many datadog components loaded at the end of execution: \#{Datadog.constants}"
39+
end
40+
SCRIPT
41+
42+
context 'when loaded initially into a clean process' do
43+
it 'loads only DI code tracker' do
44+
out, status = Open3.capture2e('ruby', stdin_data: BOOTSTRAP_SCRIPT)
45+
unless status.exitstatus == 0
46+
fail("Test script failed with exit status #{status.exitstatus}:\n#{out}")
47+
end
48+
end
49+
end
50+
51+
context 'when entire library is loaded after di bootstrapper' do
52+
it 'keeps the mappings in code tracker prior to datadog load' do
53+
script = <<-SCRIPT
54+
#{BOOTSTRAP_SCRIPT}
55+
56+
require 'datadog'
3457
58+
# Should still have the open3 entry in code tracker
3559
unless Datadog::DI.code_tracker.send(:registry).detect { |key, value| key =~ /open3.rb\\z/ }
3660
raise "Loaded script not found in code tracker registry"
3761
end
3862
39-
if Datadog.constants.sort != %i(DI VERSION)
40-
raise "Too many datadog components loaded at the end of execution: \#{Datadog.constants}"
63+
unless defined?(Datadog::Tracing)
64+
raise "Expected Datadog::Tracing to be defined after datadog was loaded"
4165
end
4266
SCRIPT
4367
out, status = Open3.capture2e('ruby', stdin_data: script)

0 commit comments

Comments
 (0)