test/test_helper.rb in newrelic_rpm-2.13.4 vs test/test_helper.rb in newrelic_rpm-2.13.5.beta1
- old
+ new
@@ -1,41 +1,104 @@
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.
-if ENV['SKIP_RAILS']
- dirs = File.dirname(__FILE__).split('/')
- while dirs.any? && !File.directory?((dirs+%w[log]).join('/'))
- dirs.pop
- end
- RAILS_ROOT = dirs.any? ? dirs.join("/") : "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
- $LOAD_PATH << File.join(NEWRELIC_PLUGIN_DIR, "lib")
- require File.join(NEWRELIC_PLUGIN_DIR, "lib/newrelic_rpm")
-else
+
+begin
+ require 'config/environment'
begin
- require File.expand_path("../../../config/environment", NEWRELIC_PLUGIN_DIR)
require 'test_help'
rescue LoadError
- puts "Unable to load Rails for New Relic tests: try setting the environment variable SKIP_RAILS=false"
- raise
+ # ignore load problems on test help - it doesn't exist in rails 3
end
+
+rescue LoadError
+ puts "Unable to load Rails for New Relic tests"
+ raise
end
+
require 'test/unit'
-require 'mocha'
require 'shoulda'
require 'test_contexts'
+require 'mocha'
+require 'mocha/integration/test_unit'
+require 'mocha/integration/test_unit/assertion_counter'
-def assert_between(floor, ceiling, value, message = nil)
- assert floor <= value && value <= ceiling,
- message || "expected #{floor} <= #{value} <= #{ceiling}"
+class Test::Unit::TestCase
+ include Mocha::API
+
+ # FIXME 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_list, actual_list
actual = Set.new actual_list
actual.delete('GC/cumulative') # in case we are in REE
expected = Set.new expected_list