From c7532003f38a48a0f00ad31561c0007d53cca63c Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 23 Aug 2017 10:41:47 -0400 Subject: [PATCH] add more diagnostics for flaky spec --- spec/components/scheduler/manager_spec.rb | 25 ++++++++++++++++++++++- spec/support/helpers.rb | 3 ++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spec/components/scheduler/manager_spec.rb b/spec/components/scheduler/manager_spec.rb index ac5c0c82db4..0a229eed32c 100644 --- a/spec/components/scheduler/manager_spec.rb +++ b/spec/components/scheduler/manager_spec.rb @@ -61,6 +61,7 @@ describe Scheduler::Manager do before do expect(ActiveRecord::Base.connection_pool.connections.length).to eq(1) @thread_count = Thread.list.count + @thread_ids = Thread.list.map { |t| t.object_id } end after do @@ -77,7 +78,29 @@ describe Scheduler::Manager do ActiveRecord::Base.connection_pool.remove(c) end expect(ActiveRecord::Base.connection_pool.connections.length).to eq(1) - wait_for do + + on_thread_mismatch = lambda do + current = Thread.list.map { |t| t.object_id } + + extra = current - @thread_ids + missing = @thread_ids - current + + if missing.length > 0 + STDERR.puts "\nMissing Threads #{missing.length} thread/s" + end + + if extra.length > 0 + Thread.list.each do |thread| + if extra.include?(thread.object_id) + STDERR.puts "\nExtra Thread Backtrace:" + STDERR.puts thread.backtrace + STDERR.puts + end + end + end + end + + wait_for(on_fail: on_thread_mismatch) do @thread_count == Thread.list.count end end diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index 805a1202409..0a74e2df384 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -61,7 +61,7 @@ module Helpers Guardian.stubs(new: guardian).with(user) end - def wait_for(&blk) + def wait_for(on_fail: nil, &blk) i = 0 result = false while !result && i < 1000 @@ -70,6 +70,7 @@ module Helpers sleep 0.001 end + on_fail&.call expect(result).to eq(true) end