Navigation Menu

Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhb committed Dec 12, 2011
1 parent 8cdb958 commit fc274b7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 46 deletions.
33 changes: 17 additions & 16 deletions test/multiple_request_profiling_test.rb
Expand Up @@ -8,14 +8,14 @@ def setup
@start_env = Rack::MockRequest.env_for('/__start__')
@stop_env = Rack::MockRequest.env_for('/__stop__')
@data_env = Rack::MockRequest.env_for('/__data__')
@root_request_env = Rack::MockRequest.env_for("/")
@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 start != :none
if block_given?
yield profiled_app
Expand Down Expand Up @@ -116,7 +116,8 @@ def profile_requests(profiled_app, requests, options = {})
should "allow 'printer' param to override :default_printer option'" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf')
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'printer=gif')
_, headers, _ = profile(profiled_app, :data => custom_data_env)
status, headers, body = profile(profiled_app, :data => custom_data_env)
assert_ok status, body
assert_equal 'image/gif', headers['Content-Type']
end

Expand Down Expand Up @@ -154,7 +155,7 @@ def profile_requests(profiled_app, requests, options = {})
end

context "when in bundler mode" do

should "call pprof.rb using 'bundle' command if bundler is set" do
status = stub_everything(:exitstatus => 0)
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true)
Expand All @@ -173,7 +174,7 @@ def profile_requests(profiled_app, requests, options = {})
Dir.expects(:chdir).with('.').returns(["","",0])
profile(profiled_app)
end

end

context "when the nodefraction parameter is specified" do
Expand Down Expand Up @@ -239,11 +240,11 @@ def profile_requests(profiled_app, requests, options = {})
profiled_app = Rack::PerftoolsProfiler.new(app, :mode => :cputime)
modified_start_env = Rack::MockRequest.env_for('/__start__', :params => 'mode=objects')
profile(profiled_app, :start => modified_start_env, :data => :none)

assert_equal '1', objects

profile(profiled_app, :data => :none)

assert_nil objects
end

Expand All @@ -256,7 +257,7 @@ def profile_requests(profiled_app, requests, options = {})
status, _, body = profiled_app.call(modified_start_env)

assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

Expand All @@ -269,7 +270,7 @@ def profile_requests(profiled_app, requests, options = {})
status, _, body = profiled_app.call(modified_start_env)

assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

Expand All @@ -282,12 +283,12 @@ def profile_requests(profiled_app, requests, options = {})
status, _, body = profiled_app.call(modified_start_env)

assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

end

end

context 'when profiling is enabled' do
Expand All @@ -311,8 +312,8 @@ def profile_requests(profiled_app, requests, options = {})
profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
profiled_app.call(@start_env)
profiled_app.call(env)
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# assert_equal env, old_env
end

Expand All @@ -327,8 +328,8 @@ def profile_requests(profiled_app, requests, options = {})
profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
profiled_app.call(@start_env)
profiled_app.call(env)
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# assert_equal env, old_env
end

Expand All @@ -353,7 +354,7 @@ def profile_requests(profiled_app, requests, options = {})
end

end

should 'keeps data from multiple calls' do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
status, headers, body = profile(profiled_app) do |app|
Expand Down
54 changes: 30 additions & 24 deletions test/single_request_profiling_test.rb
Expand Up @@ -6,7 +6,7 @@ class SingleRequestProfilingTest < Test::Unit::TestCase
def setup
@app = lambda { |env| ITERATIONS.times {1+2+3+4+5}; [200, {'Content-Type' => 'text/plain'}, ['Oh hai der']] }
@slow_app = lambda { |env| ITERATIONS.times {1+2+3+4+5}; [200, {'Content-Type' => 'text/plain'}, ['slow app']] }
@root_request_env = Rack::MockRequest.env_for("/")
@root_request_env = Rack::MockRequest.env_for("/")
@profiled_request_env = Rack::MockRequest.env_for("/", :params => "profile=true")
@profiled_request_env_with_times = Rack::MockRequest.env_for("/", :params => "profile=true&times=2")
end
Expand Down Expand Up @@ -75,7 +75,8 @@ def setup

should "allow 'printer' param override :default_printer option'" do
env = Rack::MockRequest.env_for('/', :params => 'profile=true&printer=gif')
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(env)
status, headers, body = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(env)
assert_ok status, body
assert_equal 'image/gif', headers['Content-Type']
end

Expand All @@ -101,7 +102,7 @@ def setup
end

context "when in bundler mode" do

should "call pprof.rb using 'bundle' command if bundler is set" do
status = stub_everything(:exitstatus => 0)
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true)
Expand All @@ -120,7 +121,7 @@ def setup
Dir.expects(:chdir).with('.').returns(["","",0])
profiled_app.call(@profiled_request_env)
end

end

context "when the nodecount parameter is specified" do
Expand Down Expand Up @@ -178,7 +179,7 @@ def setup
objects = ENV['CPUPROFILE_OBJECTS']
[200, {}, ["hi"]]
end

request = Rack::MockRequest.env_for("/", :params => 'profile=true&mode=objects')
rack_profiler = Rack::PerftoolsProfiler.new(app, :mode => :cputime)
rack_profiler.call(request)
Expand All @@ -192,7 +193,7 @@ def setup
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

Expand All @@ -202,8 +203,8 @@ def setup
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

should "return error message if mode is 'cputime'" do
Expand All @@ -212,8 +213,8 @@ def setup
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

end
Expand All @@ -231,7 +232,7 @@ def setup
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'text').call(@profiled_request_env)
assert_equal "text/plain", headers['Content-Type']
end

should 'have Content-Length' do
_, headers, _ = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'text').call(@profiled_request_env)
assert (headers.fetch('Content-Length').to_i > 500)
Expand All @@ -242,26 +243,30 @@ def setup
context 'when using the gif printer' do

should 'gif printer has Content-Type image/gif' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'gif').call(@profiled_request_env)
status, headers, body = Rack::PerftoolsProfiler.new(@app, :default_printer => 'gif').call(@profiled_request_env)
assert_ok status, body
assert_equal "image/gif", headers['Content-Type']
end

should 'gif printer has Content-Length' do
_, headers, _ = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'gif').call(@profiled_request_env)
status, headers, body = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'gif').call(@profiled_request_env)
assert_ok status, body
assert headers.fetch('Content-Length').to_i > 25_000
end

should 'pdf printer has Content-Type application/pdf' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(@profiled_request_env)
status, headers, body = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(@profiled_request_env)
assert_ok status, body
assert_equal "application/pdf", headers['Content-Type']
end

end

context 'when using the raw printer' do

should 'have default filename' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'raw').call(@profiled_request_env)
status, headers, body = Rack::PerftoolsProfiler.new(@app, :default_printer => 'raw').call(@profiled_request_env)
assert_ok status, body
assert_equal %q{attachment; filename="profile_data.raw"}, headers['Content-Disposition']
end

Expand All @@ -282,17 +287,17 @@ def setup
app = @app.clone
app.expects(:call).with(expected_env)
Rack::PerftoolsProfiler.new(app, :default_printer => 'gif').call(env)
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# assert_equal env, old_env
end

context "when request is not GET" do

should "not return profiling data" do
app = @app.clone
env = Rack::MockRequest.env_for('/',
:method => 'post',
env = Rack::MockRequest.env_for('/',
:method => 'post',
:params => {'profile' => 'true'})
status, headers, body = Rack::PerftoolsProfiler.new(app, :default_printer => 'gif').call(env)
assert_equal 200, status
Expand All @@ -301,12 +306,12 @@ def setup
end

should "call underlying app unchanged POST data" do
env = Rack::MockRequest.env_for('/',
env = Rack::MockRequest.env_for('/',
:method => 'post',
:params => 'profile=true&times=1&param=value&printer=gif&focus=foo&ignore=bar')
app = lambda do |env|
request = Rack::Request.new(env)
expected =
expected =
{
'profile' => 'true',
'times' => '1',
Expand All @@ -318,7 +323,7 @@ def setup
assert_equal expected, request.POST
[200, {}, ["hi"]]
end

Rack::PerftoolsProfiler.new(app, :default_printer => 'gif').call(env)
end

Expand All @@ -336,7 +341,8 @@ def setup

should "profile if the parameter matches" do
env = Rack::MockRequest.env_for('/', :params => 'profile=secret_password&printer=gif')
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf', :password => 'secret_password').call(env)
status, headers, body = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf', :password => 'secret_password').call(env)
assert_ok status, body
assert_equal 'image/gif', headers['Content-Type']
end
end
Expand Down
18 changes: 12 additions & 6 deletions test/test_helper.rb
Expand Up @@ -9,20 +9,26 @@

class Test::Unit::TestCase

def assert_ok status, body
unless status == 200
raise RuntimeError, body
end
end

end

ITERATIONS = case RUBY_VERSION
# Ruby 1.9.x is so fast that we need to add extra iterations
# Ruby 1.9.x is so fast that we need to add extra iterations
# to get profiling data
when /1\.9\../
350_000
else
350_000
else
35_000
end

# From the Rack spec (http://rack.rubyforge.org/doc/files/SPEC.html) :
# The Body must respond to each and must only yield String values. The Body should not be an instance of String.
# ... The Body commonly is an Array of Strings, the application instance itself, or a File-like object.
# ... The Body commonly is an Array of Strings, the application instance itself, or a File-like object.

class RackResponseBody
include Test::Unit::Assertions
Expand Down Expand Up @@ -52,7 +58,7 @@ def call(env)
end
GC.start
when /method2/
ITERATIONS.times do
ITERATIONS.times do
self.class.new.method2
end
GC.start
Expand All @@ -61,7 +67,7 @@ def call(env)
end

def method1
100.times do
100.times do
1+2+3+4+5
end
end
Expand Down

0 comments on commit fc274b7

Please sign in to comment.