forked from newrelic/newrelic-ruby-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.rb
167 lines (142 loc) · 5.21 KB
/
test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module NewRelic; TEST = true; end unless defined? NewRelic::TEST
ENV['RAILS_ENV'] = 'test'
NEWRELIC_PLUGIN_DIR = File.expand_path(File.join(File.dirname(__FILE__),".."))
$LOAD_PATH << '.'
$LOAD_PATH << '../../..'
$LOAD_PATH << File.join(NEWRELIC_PLUGIN_DIR,"test")
$LOAD_PATH << File.join(NEWRELIC_PLUGIN_DIR,"ui/helpers")
$LOAD_PATH.uniq!
require 'rubygems'
# We can speed things up in tests that don't need to load rails.
# You can also run the tests in a mode without rails. Many tests
# will be skipped.
begin
require 'config/environment'
# require File.join(File.dirname(__FILE__),'..','..','rpm_test_app','config','environment')
begin
require 'test_help'
rescue LoadError
# ignore load problems on test help - it doesn't exist in rails 3
end
rescue LoadError
# To run the tests against a standalone agent build, you need to
# add a rails app to the load path. It can be 2.* to 3.*. It should
# referenc newrelic_rpm in the Gemfile with a :path option pointing
# to this work directory.
guess = File.expand_path("../../../rpm", __FILE__)
if $LOAD_PATH.include? guess
puts "Unable to load Rails for New Relic tests. See note in test_helper.rb"
raise
else
$LOAD_PATH << guess
retry
end
end
require 'newrelic_rpm'
require 'test/unit'
require 'shoulda'
require 'test_contexts'
require 'mocha'
require 'mocha/integration/test_unit'
require 'mocha/integration/test_unit/assertion_counter'
require 'new_relic/fake_service'
class Test::Unit::TestCase
include Mocha::API
# we can delete this trick when we stop supporting rails2.0.x
if ENV['BRANCH'] != 'rails20'
# a hack because rails2.0.2 does not like double teardowns
def teardown
mocha_teardown
end
end
end
def assert_between(floor, ceiling, value, message="expected #{floor} <= #{value} <= #{ceiling}")
assert((floor <= value && value <= ceiling), message)
end
def check_metric_time(metric, value, delta)
time = NewRelic::Agent.get_stats(metric).total_call_time
assert_between((value - delta), (value + delta), time)
end
def check_metric_count(metric, value)
count = NewRelic::Agent.get_stats(metric).call_count
assert_equal(value, count, "should have the correct number of calls")
end
def check_unscoped_metric_count(metric, value)
count = NewRelic::Agent.get_stats_unscoped(metric).call_count
assert_equal(value, count, "should have the correct number of calls")
end
def generate_unscoped_metric_counts(*metrics)
metrics.inject({}) do |sum, metric|
sum[metric] = NewRelic::Agent.get_stats_no_scope(metric).call_count
sum
end
end
def generate_metric_counts(*metrics)
metrics.inject({}) do |sum, metric|
sum[metric] = NewRelic::Agent.get_stats(metric).call_count
sum
end
end
def assert_does_not_call_metrics(*metrics)
first_metrics = generate_metric_counts(*metrics)
yield
last_metrics = generate_metric_counts(*metrics)
assert_equal first_metrics, last_metrics, "should not have changed these metrics"
end
def assert_calls_metrics(*metrics)
first_metrics = generate_metric_counts(*metrics)
yield
last_metrics = generate_metric_counts(*metrics)
assert_not_equal first_metrics, last_metrics, "should have changed these metrics"
end
def assert_calls_unscoped_metrics(*metrics)
first_metrics = generate_unscoped_metric_counts(*metrics)
yield
last_metrics = generate_unscoped_metric_counts(*metrics)
assert_not_equal first_metrics, last_metrics, "should have changed these metrics"
end
def compare_metrics(expected, actual)
actual.delete_if {|a| a.include?('GC/cumulative') } # in case we are in REE
assert_equal(expected.to_a.sort, actual.to_a.sort, "extra: #{(actual - expected).to_a.inspect}; missing: #{(expected - actual).to_a.inspect}")
end
def with_config(config_hash, level=0)
config = NewRelic::Agent::Configuration::DottedHash.new(config_hash)
NewRelic::Agent.config.apply_config(config, level)
begin
yield
ensure
NewRelic::Agent.config.remove_config(config)
end
end
module TransactionSampleTestHelper
module_function
def make_sql_transaction(*sql)
sampler = NewRelic::Agent::TransactionSampler.new
sampler.notice_first_scope_push Time.now.to_f
sampler.notice_transaction '/path', nil, :jim => "cool"
sampler.notice_push_scope "a"
sampler.notice_transaction '/path/2', nil, :jim => "cool"
sql.each {|sql_statement| sampler.notice_sql(sql_statement, {:adapter => "test"}, 0 ) }
sleep 0.02
yield if block_given?
sampler.notice_pop_scope "a"
sampler.notice_scope_empty
sampler.samples[0]
end
def run_sample_trace_on(sampler, path='/path')
sampler.notice_first_scope_push Time.now.to_f
sampler.notice_transaction path, path, {}
sampler.notice_push_scope "Controller/sandwiches/index"
sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
sampler.notice_push_scope "ab"
sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
yield sampler if block_given?
sampler.notice_pop_scope "ab"
sampler.notice_push_scope "lew"
sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
sampler.notice_pop_scope "lew"
sampler.notice_pop_scope "Controller/sandwiches/index"
sampler.notice_scope_empty
sampler.samples[0]
end
end