Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reorganized tests to make it easier to ensure that common tests appea…

…r in single-request and multi-request tests
  • Loading branch information...
commit a99582f615242680e717bc1d2af7000aad2cd880 1 parent 7934fe6
@bhb bhb authored
View
159 test/multiple_request_profiling_test.rb
@@ -27,61 +27,106 @@ def profile_requests(profiled_app, requests, options = {})
profiled_app.call(@data_env) if get_data
end
- should 'default to text printer' do
- _, headers, _ = profile_requests(Rack::PerftoolsProfiler.new(@app), :default)
- assert_equal "text/plain", headers['Content-Type']
- end
+ context "(common behavior)" do
+
+ should 'default to text printer' do
+ _, headers, _ = profile_requests(Rack::PerftoolsProfiler.new(@app), :default)
+ assert_equal "text/plain", headers['Content-Type']
+ end
- should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime' " do
- realtime = ENV['CPUPROFILE_REALTIME']
- assert_nil realtime
- app = lambda do |env|
+ should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime' " do
realtime = ENV['CPUPROFILE_REALTIME']
- [200, {}, ["hi"]]
+ assert_nil realtime
+ app = lambda do |env|
+ realtime = ENV['CPUPROFILE_REALTIME']
+ [200, {}, ["hi"]]
+ end
+ profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'walltime')
+ profile_requests(profiled_app, :default, :get_data => false)
+ assert_equal '1', realtime
end
- profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'walltime')
- profile_requests(profiled_app, :default, :get_data => false)
- assert_equal '1', realtime
- end
- should "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
- objects = ENV['CPUPROFILE_OBJECTS']
- assert_nil objects
- app = lambda do |env|
+ should "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
objects = ENV['CPUPROFILE_OBJECTS']
- [200, {}, ["hi"]]
+ assert_nil objects
+ app = lambda do |env|
+ objects = ENV['CPUPROFILE_OBJECTS']
+ [200, {}, ["hi"]]
+ end
+ profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'objects')
+ profile_requests(profiled_app, :default, :get_data => false)
+ assert_equal '1', objects
end
- profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'objects')
- profile_requests(profiled_app, :default, :get_data => false)
- assert_equal '1', objects
- end
- should "not set CPUPROFILE_FREQUENCY by default" do
- frequency = ENV['CPUPROFILE_FREQUENCY']
- assert_nil frequency
- app = lambda do |env|
+ should "not set CPUPROFILE_FREQUENCY by default" do
frequency = ENV['CPUPROFILE_FREQUENCY']
- [200, {}, ["hi"]]
+ assert_nil frequency
+ 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)
+ assert_nil frequency
end
- profiled_app = Rack::PerftoolsProfiler.new(app)
- profile_requests(profiled_app, :default, :get_data => false)
- assert_nil frequency
- end
- should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
- frequency = ENV['CPUPROFILE_FREQUENCY']
- assert_nil frequency
- app = lambda do |env|
+ should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
frequency = ENV['CPUPROFILE_FREQUENCY']
- [200, {}, ["hi"]]
+ 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)
+ profiled_app.call(@stop_env)
+ assert_equal '250', frequency
end
- profiled_app = Rack::PerftoolsProfiler.new(app, :frequency => 250)
- profiled_app.call(@start_env)
- profiled_app.call(@root_request_env)
- assert_equal '250', frequency
+
+ 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 'give 400 if printer is invalid' do
+ profiled_app = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf')
+ profile_requests(profiled_app, :default, :get_data => false)
+ custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'printer=badprinter')
+ status, _, _ = Rack::PerftoolsProfiler.new(@app).call(custom_data_env)
+ assert_equal 400, status
+ 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 profiling is on' do
+ context 'when profiling is enabled' do
should 'not provide profiling data when __data__ is called' do
Rack::PerftoolsProfiler.clear_data
@@ -157,36 +202,4 @@ def profile_requests(profiled_app, requests, options = {})
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
View
134 test/single_request_profiling_test.rb
@@ -14,56 +14,87 @@ def setup
@profiled_request_env_with_times = Rack::MockRequest.env_for("/", :params => "profile=true&times=2")
end
- should 'default to text printer' do
- _, headers, _ = Rack::PerftoolsProfiler.new(@app).call(@profiled_request_env)
- assert_equal "text/plain", headers['Content-Type']
- end
+ context "(common behavior)" do
+
+ should 'default to text printer' do
+ _, headers, _ = Rack::PerftoolsProfiler.new(@app).call(@profiled_request_env)
+ assert_equal "text/plain", headers['Content-Type']
+ end
- should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime'" do
- realtime = ENV['CPUPROFILE_REALTIME']
- assert_nil realtime
- app = lambda do |env|
+ should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime'" do
realtime = ENV['CPUPROFILE_REALTIME']
- [200, {}, ["hi"]]
+ assert_nil realtime
+ app = lambda do |env|
+ realtime = ENV['CPUPROFILE_REALTIME']
+ [200, {}, ["hi"]]
+ end
+ Rack::PerftoolsProfiler.new(app, :mode => 'walltime').call(@profiled_request_env)
+ assert_equal '1', realtime
end
- Rack::PerftoolsProfiler.new(app, :mode => 'walltime').call(@profiled_request_env)
- assert_equal '1', realtime
- end
- should "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
- objects = ENV['CPUPROFILE_OBJECTS']
- assert_nil objects
- app = lambda do |env|
+ should "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
objects = ENV['CPUPROFILE_OBJECTS']
- [200, {}, ["hi"]]
+ assert_nil objects
+ app = lambda do |env|
+ objects = ENV['CPUPROFILE_OBJECTS']
+ [200, {}, ["hi"]]
+ end
+ Rack::PerftoolsProfiler.new(app, :mode => 'objects').call(@profiled_request_env)
+ assert_equal '1', objects
end
- Rack::PerftoolsProfiler.new(app, :mode => 'objects').call(@profiled_request_env)
- assert_equal '1', objects
- end
- should "not set CPUPROFILE_FREQUENCY by default" do
- frequency = ENV['CPUPROFILE_FREQUENCY']
- assert_nil frequency
- app = lambda do |env|
+ should "not set CPUPROFILE_FREQUENCY by default" do
frequency = ENV['CPUPROFILE_FREQUENCY']
- [200, {}, ["hi"]]
+ assert_nil frequency
+ app = lambda do |env|
+ frequency = ENV['CPUPROFILE_FREQUENCY']
+ [200, {}, ["hi"]]
+ end
+ Rack::PerftoolsProfiler.new(app).call(@profiled_request_env)
+ assert_nil frequency
end
- Rack::PerftoolsProfiler.new(app).call(@profiled_request_env)
- assert_nil frequency
- end
- should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
- frequency = ENV['CPUPROFILE_FREQUENCY']
- assert_nil frequency
- app = lambda do |env|
+ should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
frequency = ENV['CPUPROFILE_FREQUENCY']
- [200, {}, ["hi"]]
+ assert_nil frequency
+ app = lambda do |env|
+ frequency = ENV['CPUPROFILE_FREQUENCY']
+ [200, {}, ["hi"]]
+ end
+ Rack::PerftoolsProfiler.new(app, :frequency => 500).call(@profiled_request_env)
+ assert_equal '500', frequency
+ end
+
+ 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)
+ assert_equal 'image/gif', headers['Content-Type']
+ end
+
+ should 'give 400 if printer is invalid' do
+ env = Rack::MockRequest.env_for('/', :params => 'profile=true&printer=badprinter')
+ status, _, _ = Rack::PerftoolsProfiler.new(@app).call(env)
+ assert_equal 400, status
+ end
+
+ should "accept 'focus' param" do
+ profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
+ custom_env = Rack::MockRequest.env_for('/method1', :params => 'profile=true&focus=method1')
+ status, headers, body = profiled_app.call(custom_env)
+ assert_no_match(/garbage/, 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')
+ custom_env = Rack::MockRequest.env_for('/method1', :params => 'profile=true&ignore=method1')
+ status, headers, body = profiled_app.call(custom_env)
+ assert_match(/garbage/, RackResponseBody.new(body).to_s)
+ assert_no_match(/method1/, RackResponseBody.new(body).to_s)
end
- Rack::PerftoolsProfiler.new(app, :frequency => 500).call(@profiled_request_env)
- assert_equal '500', frequency
+
end
- context 'text printer' do
+ context 'when using the text printer' do
should 'return profiling data' do
_, _, body = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'text').call(@profiled_request_env)
@@ -82,7 +113,7 @@ def setup
end
- context 'gif printer' do
+ 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)
@@ -101,7 +132,7 @@ def setup
end
- context 'pdf printer' do
+ context 'when using the pdf printer' do
should 'have default filename' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(@profiled_request_env)
@@ -117,18 +148,6 @@ def setup
Rack::PerftoolsProfiler.new(app, :default_printer => 'text').call(env)
end
- 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)
- assert_equal 'image/gif', headers['Content-Type']
- end
-
- should 'give 400 if printer is invalid' do
- env = Rack::MockRequest.env_for('/', :params => 'profile=true&printer=badprinter')
- status, _, _ = Rack::PerftoolsProfiler.new(@app).call(env)
- assert_equal 400, status
- end
-
should 'send Rack environment to underlying application (minus special profiling GET params)' do
env = Rack::MockRequest.env_for('/', :params => 'profile=true&times=1&param=value&printer=gif&focus=foo&ignore=bar')
old_env = env.clone
@@ -140,19 +159,4 @@ def setup
assert_equal env, old_env
end
- should "accept 'focus' param" do
- profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
- custom_env = Rack::MockRequest.env_for('/method1', :params => 'profile=true&focus=method1')
- status, headers, body = profiled_app.call(custom_env)
- assert_no_match(/garbage/, 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')
- custom_env = Rack::MockRequest.env_for('/method1', :params => 'profile=true&ignore=method1')
- status, headers, body = profiled_app.call(custom_env)
- assert_match(/garbage/, RackResponseBody.new(body).to_s)
- assert_no_match(/method1/, RackResponseBody.new(body).to_s)
- end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.