Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/app_profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def profiler
def backend=(new_backend)
return if (new_profiler_backend = backend_for(new_backend)) == profiler_backend

if running?
if profiler_backend.locked?
raise BackendError,
"cannot change backend to #{new_backend} while #{backend} backend is running"
end
Expand Down Expand Up @@ -269,7 +269,7 @@ def profiler_backend
end

def clear
profiler.stop if running?
profiler.stop if profiler_backend.locked?
@profiler = nil
@profiler_backend = nil
end
Expand Down
4 changes: 4 additions & 0 deletions lib/app_profiler/backend/base_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def run_lock
@run_lock ||= Mutex.new
end

def locked?
run_lock.locked?
end

def name
raise NotImplementedError
end
Expand Down
18 changes: 18 additions & 0 deletions test/app_profiler/backend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ class BackendTest < TestCase
AppProfiler.backend = orig_backend
end

test ".backend= updates the backend while a foreign StackProf session is active" do
orig_backend = AppProfiler.backend
skip("Vernier not supported") unless AppProfiler.vernier_supported?
AppProfiler.backend = AppProfiler::Backend::StackprofBackend.name
AppProfiler.profiler # force @profiler memoization so AppProfiler.running? actually delegates

StackProf.start(mode: :wall, interval: 1000)
begin
AppProfiler.backend = AppProfiler::VernierProfile::BACKEND_NAME
assert_equal(AppProfiler::VernierProfile::BACKEND_NAME, AppProfiler.backend)
ensure
StackProf.stop
StackProf.results
end
ensure
AppProfiler.backend = orig_backend
end

test ".backend= accepts a symbol with the backend name" do
orig_backend = AppProfiler.backend
skip("Vernier not supported") unless AppProfiler.vernier_supported?
Expand Down
22 changes: 22 additions & 0 deletions test/app_profiler/run_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,27 @@ class RunTest < TestCase
ensure
AppProfiler.backend = orig_backend
end

test ".run swaps backend even while a foreign StackProf session is active" do
orig_backend = AppProfiler.backend
skip("Vernier not supported") unless AppProfiler.vernier_supported?
AppProfiler.backend = AppProfiler::Backend::StackprofBackend.name
AppProfiler.profiler # force @profiler memoization so AppProfiler.running? actually delegates

StackProf.start(mode: :wall, interval: 1000)
begin
profile = AppProfiler.run(backend: AppProfiler::VernierProfile::BACKEND_NAME) do
sleep(0.01)
end

assert_instance_of(AppProfiler::VernierProfile, profile)
assert_equal(AppProfiler::Backend::StackprofBackend.name, AppProfiler.backend)
ensure
StackProf.stop
StackProf.results
end
ensure
AppProfiler.backend = orig_backend
end
end
end
Loading