Skip to content

Commit

Permalink
RUBY-1747 Re-enable, update, and move commented out tests
Browse files Browse the repository at this point in the history
Some tests were commented out while this refactor was in progress. Tests have
been preserved for the most part, but some have been moved or modified. Tests
that were covered elsewhere were deleted.
  • Loading branch information
mwear committed Oct 2, 2017
1 parent 609f061 commit 8f73394
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 136 deletions.
7 changes: 6 additions & 1 deletion lib/new_relic/agent/transaction/trace.rb
Expand Up @@ -29,7 +29,12 @@ def sample_id
end

def count_nodes
self.node_count
node_count = 0
each_node do |node|
next if node == root_node
node_count +=1
end
node_count
end

def duration
Expand Down
14 changes: 14 additions & 0 deletions test/new_relic/agent/transaction/datastore_segment_test.rb
Expand Up @@ -399,6 +399,20 @@ def test_backtrace_appended_when_over_duration
node = find_node_with_name_matching(sample, /^Datastore/)
refute_nil node.params[:backtrace]
end

def test_node_obfuscated
orig_sql = "SELECT * from Jim where id=66"

in_transaction do
s = NewRelic::Agent::Transaction.start_datastore_segment
s.notice_sql(orig_sql)
s.finish
end

node = find_last_transaction_node(last_transaction_trace)
assert_equal orig_sql, node[:sql].sql
assert_equal "SELECT * from Jim where id=?", node.obfuscated_sql
end
end
end
end
Expand Down
43 changes: 43 additions & 0 deletions test/new_relic/agent/transaction/tracing_test.rb
Expand Up @@ -303,6 +303,27 @@ def test_segments_over_limit_still_record_metrics
end
end

def test_should_not_collect_nodes_beyond_limit
with_config(:'transaction_tracer.limit_segments' => 3) do
in_transaction do
%w[ wheat challah semolina ].each do |bread|
s = NewRelic::Agent::Transaction.start_datastore_segment
s.notice_sql("SELECT * FROM sandwiches WHERE bread = '#{bread}'")
s.finish
end
end

last_sample = last_transaction_trace

assert_equal 3, last_sample.count_nodes

expected_sql = "SELECT * FROM sandwiches WHERE bread = 'challah'"
deepest_node = find_last_transaction_node(last_sample)
assert_equal([], deepest_node.called_nodes)
assert_equal(expected_sql, deepest_node[:sql].sql)
end
end

# The test below documents a failure case. When a transaction has
# completed, and a segment has not been finished, we will forcibly
# finish the segment at the end of the transaction. This will cause the
Expand Down Expand Up @@ -338,6 +359,28 @@ def test_unfinished_segment_is_truncated_at_transaction_end_exclusive_times_inco
assert_equal 4, segment_c.duration
end

def test_large_transaction_trace
config = {
:'transaction_tracer.enabled' => true,
:'transaction_tracer.transaction_threshold' => 0,
:'transaction_tracer.limit_segments' => 100
}
with_config(config) do

in_transaction 'test_txn' do
110.times do |i|
segment = NewRelic::Agent::Transaction.start_segment "segment_#{i}"
segment.finish
end
end

sample = last_transaction_trace

# Verify that the TT stopped recording after 100 nodes
assert_equal(100, sample.count_nodes)
end
end

def test_txn_not_recorded_when_tracing_is_disabled
with_config :'transaction_tracer.enabled' => false do
in_transaction 'dont_trace_this' do
Expand Down
159 changes: 24 additions & 135 deletions test/new_relic/agent/transaction_sampler_test.rb
Expand Up @@ -234,53 +234,30 @@ def test_harvest_has_hard_maximum
# unit tests per se - some overlap with the tests above, but
# generally usefully so

# def test_sample_tree
# with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
# @sampler.on_start_transaction(@state, Time.now)
# @sampler.notice_push_frame(@state)

# @sampler.notice_push_frame(@state)
# @sampler.notice_pop_frame(@state, "b")

# @sampler.notice_push_frame(@state)
# @sampler.notice_push_frame(@state)
# @sampler.notice_pop_frame(@state, "d")
# @sampler.notice_pop_frame(@state, "c")

# @sampler.notice_pop_frame(@state, "a")
# @sampler.on_finishing_transaction(@state, @txn)
# sample = @sampler.harvest!.first
# assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
# end
# end

# def test_sample__gc_stats
# GC.extend MockGCStats
# # These are effectively Garbage Collects, detected each time GC.time is
# # called by the transaction sampler. One time value in seconds for each call.
# MockGCStats.mock_values = [0,0,0,1,0,0,1,0,0,0,0,0,0,0,0]

# with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
# @sampler.on_start_transaction(@state, Time.now)
# @sampler.notice_push_frame(@state)

# @sampler.notice_push_frame(@state)
# @sampler.notice_pop_frame(@state, "b")

# @sampler.notice_push_frame(@state)
# @sampler.notice_push_frame(@state)
# @sampler.notice_pop_frame(@state, "d")
# @sampler.notice_pop_frame(@state, "c")

# @sampler.notice_pop_frame(@state, "a")
# @sampler.on_finishing_transaction(@state, @txn)

# sample = @sampler.harvest!.first
# assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
# end
# ensure
# MockGCStats.mock_values = []
# end
def test_sample__gc_stats
GC.extend MockGCStats
# These are effectively Garbage Collects, detected each time GC.time is
# called by the transaction sampler. One time value in seconds for each call.
MockGCStats.mock_values = [0,0,0,1,0,0,1,0,0,0,0,0,0,0,0]

with_config(:'transaction_tracer.transaction_threshold' => 0.0) do

in_transaction 'a' do
segment_b = NewRelic::Agent::Transaction.start_segment "b"
segment_b.finish

segment_c = NewRelic::Agent::Transaction.start_segment "c"
segment_d = NewRelic::Agent::Transaction.start_segment "d"
segment_d.finish
segment_c.finish
end

sample = last_transaction_trace
assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
end
ensure
MockGCStats.mock_values = []
end

# NB this test occasionally fails due to a GC during one of the
# sample traces, for example. It's unfortunate, but we can't
Expand Down Expand Up @@ -347,94 +324,6 @@ def test_harvest_slowest
unfreeze_time
end

# def test_sample_with_parallel_paths
# with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
# @sampler.on_start_transaction(@state, Time.now)
# @sampler.notice_push_frame(@state)

# assert_equal 1, @sampler.tl_builder.scope_depth

# @sampler.notice_pop_frame(@state, "a")
# @sampler.on_finishing_transaction(@state, @txn)

# assert_nil @sampler.tl_builder

# @sampler.on_start_transaction(@state, Time.now)
# @sampler.notice_push_frame(@state)
# @sampler.notice_pop_frame(@state, "a")
# @sampler.on_finishing_transaction(@state, @txn)

# assert_nil @sampler.tl_builder

# assert_equal "ROOT{a}", @sampler.last_sample.to_s_compact
# end
# end

# def test_double_traced_method_stack_empty
# with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
# @sampler.on_start_transaction(@state, Time.now)
# @sampler.notice_push_frame(@state)
# @sampler.notice_pop_frame(@state, "a")
# @sampler.on_finishing_transaction(@state, @txn)
# @sampler.on_finishing_transaction(@state, @txn)
# @sampler.on_finishing_transaction(@state, @txn)
# @sampler.on_finishing_transaction(@state, @txn)

# refute_nil @sampler.harvest![0]
# end
# end

# def test_node_obfuscated
# @sampler.on_start_transaction(@state, Time.now.to_f)
# orig_sql = "SELECT * from Jim where id=66"

# in_transaction do
# s = NewRelic::Agent::Transaction.start_datastore_segment
# s.notice_sql(orig_sql)
# s.finish
# end

# node = find_last_transaction_node(@sampler.last_sample)
# assert_equal orig_sql, node[:sql].sql
# assert_equal "SELECT * from Jim where id=?", node.obfuscated_sql
# end

# def test_should_not_collect_nodes_beyond_limit
# with_config(:'transaction_tracer.limit_segments' => 3) do
# in_transaction do
# %w[ wheat challah semolina ].each do |bread|
# s = NewRelic::Agent::Transaction.start_datastore_segment
# s.notice_sql("SELECT * FROM sandwiches WHERE bread = '#{bread}'")
# s.finish
# end
# end

# assert_equal 3, @sampler.last_sample.count_nodes

# expected_sql = "SELECT * FROM sandwiches WHERE bread = 'challah'"
# deepest_node = find_last_transaction_node(@sampler.last_sample)
# assert_equal([], deepest_node.called_nodes)
# assert_equal(expected_sql, deepest_node[:sql].sql)
# end
# end

# def test_large_transaction_trace_harvest
# config = {
# :'transaction_tracer.enabled' => true,
# :'transaction_tracer.transaction_threshold' => 0,
# :'transaction_tracer.limit_segments' => 100
# }
# with_config(config) do
# run_long_sample_trace(110)

# samples = @sampler.harvest!
# assert_equal(1, samples.size)

# # Verify that the TT stopped recording after 100 nodes
# assert_equal(100, samples.first.count_nodes)
# end
# end

def test_harvest_prepare_samples
samples = [mock('TT0'), mock('TT1')]
samples[0].expects(:prepare_to_send!)
Expand Down

0 comments on commit 8f73394

Please sign in to comment.