Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhb committed Jan 13, 2011
1 parent f924349 commit 2e84396
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/rack/perftools_profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def self.clear_data
end

def self.with_profiling_off(app, options = {})
clear_data
instance = ProfilerMiddleware.new(app, options)
instance.force_stop
instance
Expand Down
65 changes: 42 additions & 23 deletions test/multiple_request_profiling_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ def setup
@root_request_env = Rack::MockRequest.env_for("/")
end

def profile(profiled_app, options = {})
start = options.fetch(:start) { @start_env }
stop = options.fetch(:stop) { @stop_env }
data = options.fetch(:data) { @data_env }

profiled_app.call(start)
if block_given?
yield profiled_app
else
profiled_app.call(@root_request_env)
end
last_response = profiled_app.call(stop)
if data!=nil && data!=:none
last_response = profiled_app.call(data)
end
last_response
end

def profile_requests(profiled_app, requests, options = {})
get_data = options.fetch(:get_data) { true }
if requests == :default
Expand All @@ -29,7 +47,8 @@ def profile_requests(profiled_app, requests, options = {})
context "(common behavior)" do

should 'default to text printer' do
_, headers, _ = profile_requests(Rack::PerftoolsProfiler.new(@app), :default)
# TODO - It's weird that this passes if you pass in :data => :none to #profile
_, headers, _ = profile(Rack::PerftoolsProfiler.new(@app))
assert_equal "text/plain", headers['Content-Type']
end

Expand All @@ -41,7 +60,7 @@ def profile_requests(profiled_app, requests, options = {})
[200, {}, ["hi"]]
end
profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'walltime')
profile_requests(profiled_app, :default, :get_data => false)
profile(profiled_app, :data => :none)
assert_equal '1', realtime
end

Expand All @@ -53,19 +72,20 @@ def profile_requests(profiled_app, requests, options = {})
[200, {}, ["hi"]]
end
profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'objects')
profile_requests(profiled_app, :default, :get_data => false)
profile(profiled_app, :data => :none)
assert_equal '1', objects
end

should "not set CPUPROFILE_FREQUENCY by default" do
frequency = ENV['CPUPROFILE_FREQUENCY']
assert_nil frequency
frequency = '1'
app = lambda do |env|
frequency = ENV['CPUPROFILE_FREQUENCY']
[200, {}, ["hi"]]
end
profiled_app = Rack::PerftoolsProfiler.new(app)
profile_requests(profiled_app, :default, :get_data => false)
profile(profiled_app, :data => :none)
assert_nil frequency
end

Expand All @@ -77,49 +97,48 @@ def profile_requests(profiled_app, requests, options = {})
[200, {}, ["hi"]]
end
profiled_app = Rack::PerftoolsProfiler.new(app, :frequency => 250)
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
profiled_app.call(@stop_env)
profile(profiled_app, :data => :none)
assert_equal '250', frequency
end

should "allow 'printer' param to override :default_printer option'" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf')
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
profiled_app.call(@stop_env)
profile(profiled_app, :data => :none)
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'printer=gif')
_, headers, _ = profiled_app.call(custom_data_env)
assert_equal 'image/gif', headers['Content-Type']
end

should 'give 400 if printer is invalid' do
profiled_app = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf')
profile_requests(profiled_app, :default, :get_data => false)
profile(profiled_app, :data => :none)
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'printer=badprinter')
status, _, _ = Rack::PerftoolsProfiler.new(@app).call(custom_data_env)
status, _, body = Rack::PerftoolsProfiler.new(@app).call(custom_data_env)
assert_equal 400, status
assert_match /Invalid printer type/, body.join
end

should "accept 'focus' param" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
profiled_app.call(@start_env)
profiled_app.call(Rack::MockRequest.env_for('/method1'))
profiled_app.call(Rack::MockRequest.env_for('/method2'))
profiled_app.call(@stop_env)
profile(profiled_app, :data => :none) do |app|
app.call(Rack::MockRequest.env_for('/method1'))
app.call(Rack::MockRequest.env_for('/method2'))
end
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'focus=method1')
status, headers, body = profiled_app.call(custom_data_env)
assert_match(/method1/, RackResponseBody.new(body).to_s)
assert_no_match(/method2/, RackResponseBody.new(body).to_s)
end

should "accept 'ignore' param" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
profiled_app.call(@start_env)
profiled_app.call(Rack::MockRequest.env_for('/method1'))
profiled_app.call(Rack::MockRequest.env_for('/method2'))
profiled_app.call(@stop_env)
profile(profiled_app, :data => :none) do |app|
app.call(Rack::MockRequest.env_for('/method1'))
app.call(Rack::MockRequest.env_for('/method2'))
end
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'ignore=method1')
status, headers, body = profiled_app.call(custom_data_env)
assert_match(/method2/, RackResponseBody.new(body).to_s)
assert_no_match(/method1/, RackResponseBody.new(body).to_s)
end

Expand All @@ -129,19 +148,19 @@ def profile_requests(profiled_app, requests, options = {})
status = stub_everything(:exitstatus => 0)
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true)
Open4.expects(:popen4).with(regexp_matches(/^bundle exec pprof\.rb/)).returns(status)
profile_requests(profiled_app, :default)
profile(profiled_app)
end

should "change directory into the current directory if custom Gemfile dir is not provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true, :gemfile_dir => 'bundler')
Dir.expects(:chdir).with('bundler').returns(["","",0])
profile_requests(profiled_app, :default)
profile(profiled_app)
end

should "change directory into custom Gemfile dir if provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true)
Dir.expects(:chdir).with('.').returns(["","",0])
profile_requests(profiled_app, :default)
profile(profiled_app)
end

end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'rack/perftools_profiler'

class Test::Unit::TestCase

end

ITERATIONS = case RUBY_VERSION
Expand Down

0 comments on commit 2e84396

Please sign in to comment.