Skip to content

Commit

Permalink
DEBUG-2334 test argument squashing into hashes on ruby 2 (#4157)
Browse files Browse the repository at this point in the history
  • Loading branch information
p-datadog authored Nov 25, 2024
1 parent 5779eb3 commit 0d88e2a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/datadog/di/hook_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ def recursive(depth)
def infinitely_recursive(depth = 0)
infinitely_recursive(depth + 1)
end

def squashed(options)
options
end

def positional_and_squashed(arg, options)
[arg, options]
end
end
90 changes: 90 additions & 0 deletions spec/datadog/di/instrumenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
allow(settings.dynamic_instrumentation.internal).to receive(:untargeted_trace_points).and_return(false)
allow(settings.dynamic_instrumentation).to receive(:max_capture_depth).and_return(2)
allow(settings.dynamic_instrumentation).to receive(:max_capture_attribute_count).and_return(2)
allow(settings.dynamic_instrumentation).to receive(:max_capture_string_length).and_return(100)
allow(settings.dynamic_instrumentation).to receive(:redacted_type_names).and_return([])
allow(settings.dynamic_instrumentation).to receive(:redacted_identifiers).and_return([])
end
Expand Down Expand Up @@ -250,6 +251,95 @@
end
end

context 'keyword arguments squashed into a hash' do
ruby_2_only

shared_examples 'invokes callback and captures parameters' do
it 'invokes callback and captures parameters' do
instrumenter.hook_method(probe) do |payload|
observed_calls << payload
end

target_call

expect(observed_calls.length).to eq 1
expect(observed_calls.first.keys.sort).to eq call_keys
expect(observed_calls.first[:rv]).to eq(kwarg: 42)
expect(observed_calls.first[:duration]).to be_a(Float)

expect(observed_calls.first[:serialized_entry_args]).to eq(
kwarg: {type: 'Integer', value: '42'}
)
end
end

let(:probe_args) do
{type_name: 'HookTestClass', method_name: 'squashed',
capture_snapshot: true}
end

context 'call with keyword arguments' do
let(:target_call) do
expect(HookTestClass.new.squashed(kwarg: 42)).to eq(kwarg: 42)
end

include_examples 'invokes callback and captures parameters'
end

context 'call with positional argument' do
let(:target_call) do
arg = {kwarg: 42}
expect(HookTestClass.new.squashed(arg)).to eq(kwarg: 42)
end

include_examples 'invokes callback and captures parameters'
end

context 'when there is also a positional argument' do
shared_examples 'invokes callback and captures parameters' do
it 'invokes callback and captures parameters' do
instrumenter.hook_method(probe) do |payload|
observed_calls << payload
end

target_call

expect(observed_calls.length).to eq 1
expect(observed_calls.first.keys.sort).to eq call_keys
expect(observed_calls.first[:rv]).to eq(['hello', {kwarg: 42}])
expect(observed_calls.first[:duration]).to be_a(Float)

expect(observed_calls.first[:serialized_entry_args]).to eq(
arg1: {type: 'String', value: 'hello'},
kwarg: {type: 'Integer', value: '42'},
)
end
end

let(:probe_args) do
{type_name: 'HookTestClass', method_name: 'positional_and_squashed',
capture_snapshot: true}
end

context 'call with positional and keyword arguments' do
let(:target_call) do
expect(HookTestClass.new.positional_and_squashed('hello', kwarg: 42)).to eq(['hello', {kwarg: 42}])
end

include_examples 'invokes callback and captures parameters'
end

context 'call with a splat' do
let(:target_call) do
args = ['hello', {kwarg: 42}]
expect(HookTestClass.new.positional_and_squashed(*args)).to eq(['hello', {kwarg: 42}])
end

include_examples 'invokes callback and captures parameters'
end
end
end

context 'when hooking two identical but different probes' do
let(:probe) do
Datadog::DI::Probe.new(**base_probe_args.merge(
Expand Down
8 changes: 8 additions & 0 deletions spec/datadog/di/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module DIHelpers
module ClassMethods
def ruby_2_only
if RUBY_VERSION >= '3'
before(:all) do
skip "Test is only for Ruby 2"
end
end
end

def di_test
if PlatformHelpers.jruby?
before(:all) do
Expand Down

0 comments on commit 0d88e2a

Please sign in to comment.