Permalink
Browse files

Moved tests to a new file

  • Loading branch information...
1 parent ad8dc07 commit 39ebc32060f5ed1f442155f25b48bc471bcbef7d @bhb bhb committed Jan 6, 2011
Showing with 157 additions and 141 deletions.
  1. +156 −0 test/multiple_request_profiling_test.rb
  2. +1 −141 test/rack-perftools-profiler_test.rb
@@ -0,0 +1,156 @@
+require 'test_helper'
+
+class MultipleRequestProfilingTest < Test::Unit::TestCase
+ include Rack::PerftoolsProfiler
+
+ 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']] }
+ @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("/")
+ @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
+
+ context 'start/stop profiling' do
+
+ should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime' " do
+ realtime = ENV['CPUPROFILE_REALTIME']
+ assert_nil realtime
+ app = lambda do |env|
+ realtime = ENV['CPUPROFILE_REALTIME']
+ [200, {}, ["hi"]]
+ end
+ profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'walltime')
+ profiled_app.call(@start_env)
+ profiled_app.call(@root_request_env)
+ profiled_app.call(@stop_env)
+ assert_equal '1', realtime
+ end
+
+ should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
+ frequency = ENV['CPUPROFILE_FREQUENCY']
+ assert_nil frequency
+ app = lambda do |env|
+ frequency = ENV['CPUPROFILE_FREQUENCY']
+ [200, {}, ["hi"]]
+ end
+ profiled_app = Rack::PerftoolsProfiler.new(app, :frequency => 250)
+ profiled_app.call(@start_env)
+ profiled_app.call(@root_request_env)
+ assert_equal '250', frequency
+ end
+
+ context 'when profiling is on' do
+
+ should 'not provide profiling data when __data__ is called' do
+ Rack::PerftoolsProfiler.clear_data
+ profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'text')
+ profiled_app.call(@start_env)
+ profiled_app.call(@root_request_env)
+ status, _, body = profiled_app.call(@data_env)
+ assert_equal 400, status
+ assert_match(/No profiling data available./, RackResponseBody.new(body).to_s)
+ end
+
+ should 'pass on profiling params in environment' do
+ env = Rack::MockRequest.env_for('/', :params => 'times=2')
+ old_env = env.clone
+ app = @app.clone
+ expected_env = env.clone
+ expected_env['rack.request.query_string'] = 'times=2'
+ expected_env['rack.request.query_hash'] = {'times' => '2'}
+ app.expects(:call).with(expected_env)
+ profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
+ profiled_app.call(@start_env)
+ profiled_app.call(env)
+ assert_equal env, old_env
+ end
+
+ should 'pass on non-profiling params in environment' do
+ env = Rack::MockRequest.env_for('/', :params => 'param=value')
+ old_env = env.clone
+ app = @app.clone
+ expected_env = env.clone
+ expected_env['rack.request.query_string'] = 'param=value'
+ expected_env['rack.request.query_hash'] = {'param' => 'value'}
+ app.expects(:call).with(expected_env)
+ profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
+ profiled_app.call(@start_env)
+ profiled_app.call(env)
+ assert_equal env, old_env
+ end
+
+ should 'not alter regular calls' do
+ profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'gif')
+ profiled_app.call(@start_env)
+ status, headers, body = profiled_app.call(@root_request_env)
+ assert_equal 200, status
+ assert_equal 'text/plain', headers['Content-Type']
+ assert_equal 'Oh hai der', RackResponseBody.new(body).to_s
+ end
+
+ end
+
+ context 'after profiling is finished' do
+
+ should 'return profiling data when __data__ is called' do
+ profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'gif')
+ profiled_app.call(@start_env)
+ profiled_app.call(@root_request_env)
+ profiled_app.call(@stop_env)
+ status, headers, body = profiled_app.call(@data_env)
+ assert_equal 200, status
+ assert_equal "image/gif", headers['Content-Type']
+ end
+
+ end
+
+ should 'keeps data from multiple calls' 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)
+ status, headers, body = profiled_app.call(@data_env)
+ assert_match(/method1/, RackResponseBody.new(body).to_s)
+ assert_match(/method2/, RackResponseBody.new(body).to_s)
+ 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)
+ 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 "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)
+ custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'focus=method1')
+ status, headers, body = profiled_app.call(custom_data_env)
+ 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)
+ custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'ignore=method1')
+ status, headers, body = profiled_app.call(custom_data_env)
+ assert_no_match(/method1/, RackResponseBody.new(body).to_s)
+ end
+
+ end
+
+end
@@ -61,7 +61,7 @@ class RackPerftoolsProfilerTest < Test::Unit::TestCase
assert_equal old_options, options
end
- context 'without profiling' do
+ context 'when not profiling' do
should 'call app directly' do
status, headers, body = Rack::PerftoolsProfiler.with_profiling_off(@app).call(@root_request_env)
@@ -80,146 +80,6 @@ class RackPerftoolsProfilerTest < Test::Unit::TestCase
end
-
- context 'start/stop profiling' do
-
- should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime' " do
- realtime = ENV['CPUPROFILE_REALTIME']
- assert_nil realtime
- app = lambda do |env|
- realtime = ENV['CPUPROFILE_REALTIME']
- [200, {}, ["hi"]]
- end
- profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'walltime')
- profiled_app.call(@start_env)
- profiled_app.call(@root_request_env)
- profiled_app.call(@stop_env)
- assert_equal '1', realtime
- end
-
- should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
- frequency = ENV['CPUPROFILE_FREQUENCY']
- assert_nil frequency
- app = lambda do |env|
- frequency = ENV['CPUPROFILE_FREQUENCY']
- [200, {}, ["hi"]]
- end
- profiled_app = Rack::PerftoolsProfiler.new(app, :frequency => 250)
- profiled_app.call(@start_env)
- profiled_app.call(@root_request_env)
- assert_equal '250', frequency
- end
-
- context 'when profiling is on' do
-
- should 'not provide profiling data when __data__ is called' do
- Rack::PerftoolsProfiler.clear_data
- profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'text')
- profiled_app.call(@start_env)
- profiled_app.call(@root_request_env)
- status, _, body = profiled_app.call(@data_env)
- assert_equal 400, status
- assert_match(/No profiling data available./, RackResponseBody.new(body).to_s)
- end
-
- should 'pass on profiling params in environment' do
- env = Rack::MockRequest.env_for('/', :params => 'times=2')
- old_env = env.clone
- app = @app.clone
- expected_env = env.clone
- expected_env['rack.request.query_string'] = 'times=2'
- expected_env['rack.request.query_hash'] = {'times' => '2'}
- app.expects(:call).with(expected_env)
- profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
- profiled_app.call(@start_env)
- profiled_app.call(env)
- assert_equal env, old_env
- end
-
- should 'pass on non-profiling params in environment' do
- env = Rack::MockRequest.env_for('/', :params => 'param=value')
- old_env = env.clone
- app = @app.clone
- expected_env = env.clone
- expected_env['rack.request.query_string'] = 'param=value'
- expected_env['rack.request.query_hash'] = {'param' => 'value'}
- app.expects(:call).with(expected_env)
- profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
- profiled_app.call(@start_env)
- profiled_app.call(env)
- assert_equal env, old_env
- end
-
- should 'not alter regular calls' do
- profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'gif')
- profiled_app.call(@start_env)
- status, headers, body = profiled_app.call(@root_request_env)
- assert_equal 200, status
- assert_equal 'text/plain', headers['Content-Type']
- assert_equal 'Oh hai der', RackResponseBody.new(body).to_s
- end
-
- end
-
- context 'after profiling is finished' do
-
- should 'return profiling data when __data__ is called' do
- profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'gif')
- profiled_app.call(@start_env)
- profiled_app.call(@root_request_env)
- profiled_app.call(@stop_env)
- status, headers, body = profiled_app.call(@data_env)
- assert_equal 200, status
- assert_equal "image/gif", headers['Content-Type']
- end
-
- end
-
- should 'keeps data from multiple calls' 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)
- status, headers, body = profiled_app.call(@data_env)
- assert_match(/method1/, RackResponseBody.new(body).to_s)
- assert_match(/method2/, RackResponseBody.new(body).to_s)
- 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)
- 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 "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)
- custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'focus=method1')
- status, headers, body = profiled_app.call(custom_data_env)
- 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)
- custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'ignore=method1')
- status, headers, body = profiled_app.call(custom_data_env)
- assert_no_match(/method1/, RackResponseBody.new(body).to_s)
- end
-
- end
-
context "when in bundler mode" do
should "call pprof.rb using 'bundle' command if bundler is set" do

0 comments on commit 39ebc32

Please sign in to comment.