From 9c1d32a7ada794ecd0356d56f7be3cdf3982d8ac Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 27 Dec 2021 12:52:04 -0800 Subject: Make TracePoint#enable with block target current thread by default If TracePoint#enable is passed a block, it previously started the trace on all threads. This changes it to trace only the current thread by default. To limit the scope of the change, the current thread is only used by default if target and target_line are both nil. You can pass target_thread: nil to enable tracing on all threads, to get the previous default behavior. Fixes [Bug #16889] --- spec/ruby/core/tracepoint/enable_spec.rb | 55 ++++++++++++++++++++++--------- spec/ruby/core/tracepoint/inspect_spec.rb | 4 +-- 2 files changed, 42 insertions(+), 17 deletions(-) (limited to 'spec') diff --git a/spec/ruby/core/tracepoint/enable_spec.rb b/spec/ruby/core/tracepoint/enable_spec.rb index ab392c7583..4d9cfa1448 100644 --- a/spec/ruby/core/tracepoint/enable_spec.rb +++ b/spec/ruby/core/tracepoint/enable_spec.rb @@ -57,25 +57,50 @@ describe 'TracePoint#enable' do end.enable { event_name.should equal(:line) } end - it 'enables the trace object for any thread' do - threads = [] - trace = TracePoint.new(:line) do |tp| - # Runs on purpose on any Thread - threads << Thread.current - end + ruby_version_is '3.2' do + it 'enables the trace object for any thread' do + threads = [] + trace = TracePoint.new(:line) do |tp| + # Runs on purpose on any Thread + threads << Thread.current + end - thread = nil - trace.enable do - line_event = true - thread = Thread.new do - event_in_other_thread = true + thread = nil + trace.enable do + line_event = true + thread = Thread.new do + event_in_other_thread = true + end + thread.join end - thread.join + + threads = threads.uniq + threads.should.include?(Thread.current) + threads.should_not.include?(thread) end + end - threads = threads.uniq - threads.should.include?(Thread.current) - threads.should.include?(thread) + ruby_version_is ''...'3.2' do + it 'enables the trace object for any thread' do + threads = [] + trace = TracePoint.new(:line) do |tp| + # Runs on purpose on any Thread + threads << Thread.current + end + + thread = nil + trace.enable do + line_event = true + thread = Thread.new do + event_in_other_thread = true + end + thread.join + end + + threads = threads.uniq + threads.should.include?(Thread.current) + threads.should.include?(thread) + end end it 'can accept arguments within a block but it should not yield arguments' do diff --git a/spec/ruby/core/tracepoint/inspect_spec.rb b/spec/ruby/core/tracepoint/inspect_spec.rb index b9d7f35c32..a07b626212 100644 --- a/spec/ruby/core/tracepoint/inspect_spec.rb +++ b/spec/ruby/core/tracepoint/inspect_spec.rb @@ -98,7 +98,7 @@ describe 'TracePoint#inspect' do TracePoint.new(:thread_begin) { |tp| next unless Thread.current == thread inspect ||= tp.inspect - }.enable do + }.enable(target_thread: nil) do thread = Thread.new {} thread_inspection = thread.inspect thread.join @@ -114,7 +114,7 @@ describe 'TracePoint#inspect' do TracePoint.new(:thread_end) { |tp| next unless Thread.current == thread inspect ||= tp.inspect - }.enable do + }.enable(target_thread: nil) do thread = Thread.new {} thread_inspection = thread.inspect thread.join -- cgit v1.2.3