Skip to content

Commit

Permalink
RUBY-1668 initial test/ tree cleanup
Browse files Browse the repository at this point in the history
* tidy up test_helper.rb, refactor out to new helpers/ files
* fix up requires, after tidying of $:
* pull out unneeded travis scripts
  • Loading branch information
kenichi committed Feb 3, 2017
1 parent 30c3bef commit d6e7c7c
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 284 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Expand Up @@ -8,10 +8,8 @@ language: ruby
sudo: required

before_install:
- ./test/script/before_install/revert_rubygems.sh
- gem --version
- ./test/script/before_install/gemstash_mirror.sh
- ./test/script/before_install/update_bundler.sh
- bundle --version

install: bundle install
Expand Down
3 changes: 1 addition & 2 deletions lib/new_relic/agent.rb
Expand Up @@ -369,8 +369,7 @@ def add_instrumentation(file_pattern)
#
# @api public
def require_test_helper
path = File.join(__FILE__, '..', '..', '..', 'test', 'agent_helper')
require File.expand_path(path)
require File.expand_path('../../../test/agent_helper', __FILE__)
end

# This method sets the block sent to this method as a sql
Expand Down
37 changes: 37 additions & 0 deletions test/helpers/logging.rb
@@ -0,0 +1,37 @@
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

def with_verbose_logging
orig_logger = NewRelic::Agent.logger
$stderr.puts '', '---', ''
new_logger = NewRelic::Agent::AgentLogger.new('', Logger.new($stderr) )
NewRelic::Agent.logger = new_logger

with_config(:log_level => 'debug') do
yield
end
ensure
NewRelic::Agent.logger = orig_logger
end

# Need to be a bit sloppy when testing against the logging--let everything
# through, but check we (at least) get our particular message we care about
def expects_logging(level, *with_params)
::NewRelic::Agent.logger.stubs(level)
::NewRelic::Agent.logger.expects(level).with(*with_params).once
end

def expects_no_logging(level)
::NewRelic::Agent.logger.expects(level).never
end

# Sometimes need to test cases where we muddle with the global logger
# If so, use this method to ensure it gets restored after we're done
def without_logger
logger = ::NewRelic::Agent.logger
::NewRelic::Agent.logger = nil
yield
ensure
::NewRelic::Agent.logger = logger
end
50 changes: 50 additions & 0 deletions test/helpers/minitest.rb
@@ -0,0 +1,50 @@
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

unless defined?(Minitest::Test)
Minitest::Test = MiniTest::Unit::TestCase
end

# Set up a watcher for leaking agent threads out of tests. It'd be nice to
# disable the threads everywhere, but not all tests have newrelic.yml loaded to
# us to rely on, so instead we'll just watch for it.
class Minitest::Test

def before_setup
if self.respond_to?(:name)
test_method_name = self.name
else
test_method_name = self.__name__
end

NewRelic::Agent.logger.info("*** #{self.class}##{test_method_name} **")

@__thread_count = ruby_threads.count
super
end

def after_teardown
unfreeze_time

threads = ruby_threads
if @__thread_count != threads.count
backtraces = threads.map do |thread|
trace = Hometown.for(thread)
trace.backtrace.join("\n ")
end.join("\n\n")

fail "Thread count changed in this test from #{@__thread_count} to #{threads.count}\n#{backtraces}"
end

super
end

# We only want to count threads that were spun up from Ruby (i.e.
# Thread.new) JRuby has system threads we don't care to track.
def ruby_threads
Thread.list.select { |t| Hometown.for(t) }
end

end

87 changes: 87 additions & 0 deletions test/helpers/misc.rb
@@ -0,0 +1,87 @@
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

def default_service(stubbed_method_overrides = {})
service = stub
stubbed_method_defaults = {
:connect => {},
:shutdown => nil,
:agent_id= => nil,
:agent_id => nil,
:collector => stub_everything,
:request_timeout= => nil,
:metric_data => nil,
:error_data => nil,
:transaction_sample_data => nil,
:sql_trace_data => nil,
:get_agent_commands => [],
:agent_command_results => nil,
:analytic_event_data => nil,
:valid_to_marshal? => true
}

service.stubs(stubbed_method_defaults.merge(stubbed_method_overrides))

# When session gets called yield to the given block.
service.stubs(:session).yields
service
end

def fixture_tcp_socket( response )
# Don't actually talk to Google.
socket = stub("socket") do
stubs(:closed?).returns(false)
stubs(:close)
stubs(:setsockopt)

# Simulate a bunch of socket-ey stuff since Mocha doesn't really
# provide any other way to do it
class << self
attr_accessor :response, :write_checker
end

def self.check_write
self.write_checker = Proc.new
end

def self.write( buf )
self.write_checker.call( buf ) if self.write_checker
buf.length
end

def self.sysread( size, buf='' )
@data ||= response.to_s
raise EOFError if @data.empty?
buf.replace @data.slice!( 0, size )
buf
end
class << self
alias_method :read_nonblock, :sysread
end

end

socket.response = response
TCPSocket.stubs( :open ).returns( socket )

return socket
end

def dummy_mysql_explain_result(hash=nil)
hash ||= {
'Id' => '1',
'Select Type' => 'SIMPLE',
'Table' => 'sandwiches',
'Type' => 'range',
'Possible Keys' => 'PRIMARY',
'Key' => 'PRIMARY',
'Key Length' => '4',
'Ref' => '',
'Rows' => '1',
'Extra' => 'Using index'
}
explain_result = mock('explain result')
explain_result.stubs(:each_hash).yields(hash)
explain_result
end
44 changes: 44 additions & 0 deletions test/helpers/transaction_sample_test.rb
@@ -0,0 +1,44 @@
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

module TransactionSampleTestHelper
module_function
def make_sql_transaction(*sql)
sampler = nil
state = NewRelic::Agent::TransactionState.tl_get

in_transaction('/path') do
sampler = NewRelic::Agent.instance.transaction_sampler
sampler.notice_push_frame(state, "a")
explainer = NewRelic::Agent::Instrumentation::ActiveRecord::EXPLAINER
sql.each {|sql_statement| sampler.notice_sql(sql_statement, {:adapter => "mysql"}, 0, state, explainer) }
sleep 0.02
yield if block_given?
sampler.notice_pop_frame(state, "a")
end

return sampler.last_sample
end

def run_sample_trace(path='/path')
sampler = nil
state = NewRelic::Agent::TransactionState.tl_get

request = stub(:path => path)

in_transaction("Controller/sandwiches/index", :request => request) do
sampler = NewRelic::Agent.instance.transaction_sampler
sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", {}, 0, state)
sampler.notice_push_frame(state, "ab")
sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", {}, 0, state)
yield sampler if block_given?
sampler.notice_pop_frame(state, "ab")
sampler.notice_push_frame(state, "lew")
sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", {}, 0, state)
sampler.notice_pop_frame(state, "lew")
end

return sampler.last_sample
end
end
4 changes: 2 additions & 2 deletions test/new_relic/agent/sampled_buffer_test.rb
Expand Up @@ -2,8 +2,8 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
require 'test/new_relic/agent/event_buffer_test_cases'
require File.expand_path '../../../test_helper', __FILE__
require 'new_relic/agent/event_buffer_test_cases'

module NewRelic::Agent
class SampledBufferTest < Minitest::Test
Expand Down
2 changes: 1 addition & 1 deletion test/new_relic/agent/sized_buffer_test.rb
Expand Up @@ -3,7 +3,7 @@
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
require 'test/new_relic/agent/event_buffer_test_cases'
require 'new_relic/agent/event_buffer_test_cases'
require 'new_relic/agent/sized_buffer'

module NewRelic::Agent
Expand Down
2 changes: 1 addition & 1 deletion test/new_relic/agent/synthetics_event_buffer_test.rb
Expand Up @@ -3,7 +3,7 @@
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
require 'test/new_relic/agent/event_buffer_test_cases'
require 'new_relic/agent/event_buffer_test_cases'
require 'new_relic/agent/synthetics_event_buffer'

module NewRelic::Agent
Expand Down
33 changes: 0 additions & 33 deletions test/new_relic/rack/deferred_instrumentation_test.rb

This file was deleted.

15 changes: 0 additions & 15 deletions test/script/before_install/revert_rubygems.sh

This file was deleted.

12 changes: 0 additions & 12 deletions test/script/before_install/update_bundler.sh

This file was deleted.

0 comments on commit d6e7c7c

Please sign in to comment.