Skip to content

Commit

Permalink
Merge pull request #3185 from DataDog/ivoanjo/maybe-fix-flaky-profile…
Browse files Browse the repository at this point in the history
…r-spec

Try to fix flaky profiler spec
  • Loading branch information
ivoanjo authored Oct 5, 2023
2 parents 1ee7bcf + 75ee6e2 commit 3248f24
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/datadog/profiling/collectors/stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ def call_sleep
end

describe 'approximate thread state categorization based on current stack' do
before do
wait_for { background_thread.backtrace_locations.first.base_label }.to eq(expected_method_name)
end

describe 'state label validation' do
let(:expected_method_name) { 'sleep' }
let(:do_in_background_thread) do
proc do |ready_queue|
ready_queue << true
Expand All @@ -248,6 +253,7 @@ def call_sleep
end

context 'when sampling a thread with cpu-time' do
let(:expected_method_name) { 'sleep' }
let(:do_in_background_thread) do
proc do |ready_queue|
ready_queue << true
Expand All @@ -262,6 +268,7 @@ def call_sleep
end

context 'when sampling a sleeping thread with no cpu-time' do
let(:expected_method_name) { 'sleep' }
let(:do_in_background_thread) do
proc do |ready_queue|
ready_queue << true
Expand All @@ -276,6 +283,7 @@ def call_sleep
end

context 'when sampling a thread waiting on a select' do
let(:expected_method_name) { 'select' }
let(:server_socket) { TCPServer.new(6006) }
let(:background_thread) { Thread.new(ready_queue, server_socket, &do_in_background_thread) }
let(:do_in_background_thread) do
Expand All @@ -298,6 +306,7 @@ def call_sleep
end

context 'when sampling a thread blocked on Thread#join' do
let(:expected_method_name) { 'join' }
let(:another_thread) { Thread.new { sleep } }
let(:background_thread) { Thread.new(ready_queue, another_thread, &do_in_background_thread) }
let(:do_in_background_thread) do
Expand All @@ -324,6 +333,7 @@ def call_sleep
end

context 'when sampling a thread blocked on Mutex#synchronize' do
let(:expected_method_name) { 'synchronize' }
let(:locked_mutex) { Mutex.new.tap(&:lock) }
let(:background_thread) { Thread.new(ready_queue, locked_mutex, &do_in_background_thread) }
let(:do_in_background_thread) do
Expand All @@ -340,6 +350,7 @@ def call_sleep
end

context 'when sampling a thread blocked on Mutex#lock' do
let(:expected_method_name) { 'lock' }
let(:locked_mutex) { Mutex.new.tap(&:lock) }
let(:background_thread) { Thread.new(ready_queue, locked_mutex, &do_in_background_thread) }
let(:do_in_background_thread) do
Expand All @@ -356,6 +367,14 @@ def call_sleep
end

context 'when sampling a thread blocked on Monitor#synchronize' do
let(:expected_method_name) do
# On older Rubies Monitor is implemented using Mutex instead of natively
if RUBY_VERSION.start_with?('2.3', '2.4', '2.5', '2.6')
'lock'
else
'synchronize'
end
end
let(:locked_monitor) { Monitor.new.tap(&:enter) }
let(:background_thread) { Thread.new(ready_queue, locked_monitor, &do_in_background_thread) }
let(:do_in_background_thread) do
Expand All @@ -372,6 +391,7 @@ def call_sleep
end

context 'when sampling a thread waiting on a IO object' do
let(:expected_method_name) { 'wait_readable' }
let(:server_socket) { TCPServer.new(6006) }
let(:background_thread) { Thread.new(ready_queue, server_socket, &do_in_background_thread) }
let(:do_in_background_thread) do
Expand All @@ -394,6 +414,7 @@ def call_sleep
end

context 'when sampling a thread waiting on a Queue object' do
let(:expected_method_name) { 'pop' }
let(:do_in_background_thread) do
proc do |ready_queue|
ready_queue << true
Expand All @@ -408,6 +429,7 @@ def call_sleep
end

context 'when sampling a thread in an unknown state' do
let(:expected_method_name) { 'stop' }
let(:do_in_background_thread) do
proc do |ready_queue|
ready_queue << true
Expand Down

0 comments on commit 3248f24

Please sign in to comment.