Skip to content

Commit

Permalink
Additional tests for unbalanced scope stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Kayser committed Jun 5, 2008
1 parent a6b9767 commit 386daf7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
45 changes: 40 additions & 5 deletions test/newrelic/agent/tc_method_tracer.rb
@@ -1,5 +1,6 @@
require File.join(File.dirname(__FILE__),'mock_agent')
require 'newrelic/agent/method_tracer'
require 'newrelic/agent/transaction_sampler'
require 'test/unit'

::RPM_TRACERS_ENABLED = true unless defined? ::RPM_TRACERS_ENABLED
Expand Down Expand Up @@ -51,6 +52,7 @@ def test_basic
METRIC = "metric"
def test_add_method_tracer
@metric_name = METRIC
assert ::RPM_TRACERS_ENABLED
self.class.add_method_tracer :method_to_be_traced, METRIC

t1 = Time.now
Expand All @@ -61,6 +63,21 @@ def test_add_method_tracer
check_time stats.total_call_time, elapsed
assert stats.call_count == 1
end
def test_nested_scope_tracer
Insider.add_method_tracer :catcher, "catcher", true
Insider.add_method_tracer :thrower, "thrower", true
sampler = TransactionSampler.new
@stats_engine.add_scope_stack_listener sampler
mock = Insider.new(@stats_engine)
mock.catcher(0)
mock.catcher(5)
stats = @stats_engine.get_stats("catcher")
assert_equal 2, stats.call_count
stats = @stats_engine.get_stats("thrower")
assert_equal 6, stats.call_count
sample = sampler.harvest_slowest_sample
assert_not_nil sample
end

def test_add_same_tracer_twice
@metric_name = METRIC
Expand Down Expand Up @@ -114,7 +131,7 @@ def test_trace_module_method
def test_remove
self.class.add_method_tracer :method_to_be_traced, METRIC
self.class.remove_method_tracer :method_to_be_traced, METRIC

t1 = Time.now
method_to_be_traced 1,2,3,false,METRIC
elapsed = Time.now - t1
Expand All @@ -127,14 +144,14 @@ def MethodTracerTests.static_method(x, testcase, is_traced)
testcase.assert x == "x"
testcase.assert((testcase.stats_engine.peek_scope.name == "x") == is_traced)
end

def trace_trace_static_method
self.add_method_tracer :static_method, '#{args[0]}'
self.class.static_method "x", self, true
self.remove_method_tracer :static_method, '#{args[0]}'
self.class.static_method "x", self, false
end

def test_execption
begin
metric = "hey there"
Expand Down Expand Up @@ -174,7 +191,7 @@ def trace_no_push_scope
def check_time (t1, t2)
assert((t2-t1).abs < 0.01)
end

# =======================================================
# test methods to be traced
def method_to_be_traced(x, y, z, is_traced, expected_metric)
Expand All @@ -199,4 +216,22 @@ def method_with_block(x, y, z, is_traced, expected_metric, &block)
end
end
end

class Insider
def initialize(stats_engine)
@stats_engine = stats_engine
end
def catcher(level=0)
thrower(level) if level>0
end
def thrower(level)
if level == 0
sampler = NewRelic::Agent::TransactionSampler.new
begin
@stats_engine.add_scope_stack_listener sampler
fail "This should not have worked."
rescue; end
else
thrower(level-1)
end
end
end
14 changes: 13 additions & 1 deletion test/newrelic/agent/tc_transaction_sample_builder.rb
Expand Up @@ -116,7 +116,19 @@ def test_omit_segments_with
assert_nil segment.metric_name =~ /Rails\/Application Code Loading/
end
end

def test_unbalanced_handling
assert_raise RuntimeError do
build_segment("a") do
begin
build_segment("aa") do
build_segment("aaa") do
raise "a problem"
end
end
rescue; end
end
end
end
def test_marshal
build_segment "a" do
build_segment "ab"
Expand Down
6 changes: 3 additions & 3 deletions test/newrelic/agent/tc_transaction_sampler.rb
Expand Up @@ -16,9 +16,9 @@ def test_multiple_samples
run_sample_trace

samples = @sampler.get_samples
assert samples.length == 4
assert samples.first.root_segment.called_segments[0].metric_name == "a"
assert samples.last.root_segment.called_segments[0].metric_name == "a"
assert_equal 4, samples.length
assert_equal "a", samples.first.root_segment.called_segments[0].metric_name
assert_equal "a", samples.last.root_segment.called_segments[0].metric_name
end


Expand Down

0 comments on commit 386daf7

Please sign in to comment.