-
Notifications
You must be signed in to change notification settings - Fork 4
Delegate save to saviour proc #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
183c3b7
af0efa0
7200e93
4351976
9cb4aba
fbfcbc5
8940788
e415702
303e3dc
97ead0d
bec11d3
5d67d60
26d2db1
435df56
a905756
bc32d61
c07f502
4b54caa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,35 @@ def test_save_custom | |
| StackProf::Middleware.save | ||
| end | ||
|
|
||
| def test_save_should_use_a_proc_if_passed | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also needs tests for the |
||
| StackProf.stubs(:results).returns({ mode: 'foo' }) | ||
| FileUtils.expects(:mkdir_p).with('/foo').never | ||
| File.expects(:open).with(regexp_matches(/^\/foo\/stackprof-foo/), 'wb').never | ||
|
|
||
| proc_called = false | ||
| StackProf::Middleware.new(Object.new, saviour: Proc.new{ proc_called = true }) | ||
| StackProf::Middleware.save | ||
| assert proc_called | ||
| end | ||
|
|
||
| def test_save_proc_should_receive_env_in_proc_if_passed | ||
| StackProf.stubs(:results).returns({ mode: 'foo' }) | ||
|
|
||
| env_set = nil | ||
| StackProf::Middleware.new(Object.new, saviour: Proc.new{ |env, results| env_set = env['FOO'] }) | ||
| StackProf::Middleware.save({ 'FOO' => 'bar' }) | ||
| assert_equal env_set, 'bar' | ||
| end | ||
|
|
||
| def test_save_proc_should_receive_results_in_proc_if_passed | ||
| StackProf.stubs(:results).returns({ mode: 'foo' }) | ||
|
|
||
| results_received = nil | ||
| StackProf::Middleware.new(Object.new, saviour: Proc.new{ |env, results| results_received = results[:mode] }) | ||
| StackProf::Middleware.save({}) | ||
| assert_equal results_received, 'foo' | ||
| end | ||
|
|
||
| def test_enabled_should_use_a_proc_if_passed | ||
| env = {} | ||
|
|
||
|
|
@@ -64,4 +93,36 @@ def test_raw | |
| StackProf::Middleware.new(Object.new, raw: true) | ||
| assert StackProf::Middleware.raw | ||
| end | ||
|
|
||
| def test_enabled_should_override_mode_if_a_proc | ||
| proc_called = false | ||
| middleware = StackProf::Middleware.new(proc {|env| proc_called = true}, enabled: Proc.new{ [true, 'foo'] }) | ||
| env = Hash.new { true } | ||
| enabled, mode = StackProf::Middleware.enabled?(env) | ||
| assert enabled | ||
| assert_equal 'foo', mode | ||
|
|
||
| StackProf.expects(:start).with({mode: 'foo', interval: StackProf::Middleware.interval, raw: false}) | ||
| StackProf.expects(:stop) | ||
|
|
||
| middleware.call(env) | ||
| assert proc_called | ||
| end | ||
|
|
||
| def test_saviour_should_be_called_when_enabled_with_env | ||
| proc_called = false | ||
| env_set = nil | ||
| results_received = nil | ||
| enable_proc = Proc.new{ [true, 'foo'] } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realize I want to just replace all
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or better: use |
||
| saviour_proc = Proc.new{ |env, results| env_set = env['FOO'] ; results_received = results[:mode] } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. saviour_proc = proc do |env, results|
env_set = env['FOO']
results_received = results[:mode]
endmaybe? |
||
| middleware = StackProf::Middleware.new(proc {|env| proc_called = true}, enabled: enable_proc, saviour: saviour_proc, save_every: 1) | ||
| StackProf.expects(:start).with({mode: 'foo', interval: StackProf::Middleware.interval, raw: false}) | ||
| StackProf.expects(:stop) | ||
| StackProf.stubs(:results).returns({ mode: 'foo' }) | ||
|
|
||
| middleware.call({ 'FOO' => 'bar' }) | ||
| assert proc_called | ||
| assert_equal env_set, 'bar' | ||
| assert_equal results_received, 'foo' | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should just assume
saviouris callable if non-nil, soif saviourhere.