Fail before suite if more than 1 thread exists

MSP-11147

Detect thread leaks in a `before(:suite)` configured by
`Metasploit::Framework::Spec::Threads::Suite.configure!` and fail if any
leaks are found.
bug/bundler_fix
Luke Imhoff 2014-11-05 14:38:43 -06:00
parent cca30b536f
commit 96990fdc02
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8
5 changed files with 51 additions and 0 deletions

View File

@ -32,6 +32,10 @@ module Metasploit
# works in compatible manner with activerecord's rake tasks and other
# railties.
module Framework
extend ActiveSupport::Autoload
autoload :Spec
# Returns the root of the metasploit-framework project. Use in place of
# `Rails.root`.
#

View File

@ -0,0 +1,5 @@
module Metasploit::Framework::Spec
extend ActiveSupport::Autoload
autoload :Threads
end

View File

@ -0,0 +1,5 @@
module Metasploit::Framework::Spec::Threads
extend ActiveSupport::Autoload
autoload :Suite
end

View File

@ -0,0 +1,35 @@
module Metasploit::Framework::Spec::Threads::Suite
#
# CONSTANTS
#
EXPECTED_THREAD_COUNT_BEFORE_SUITE = 1
#
# Module Methods
#
# Configures `before(:suite)` and `after(:suite)` callback to detect thread leaks.
#
# @return [void]
def self.configure!
unless @configured
RSpec.configure do |config|
config.before(:suite) do
thread_count = Thread.list.count
expect(thread_count).to(
(be <= EXPECTED_THREAD_COUNT_BEFORE_SUITE),
"#{thread_count} #{'thread'.pluralize(thread_count)} exist(s) when " \
"only #{EXPECTED_THREAD_COUNT_BEFORE_SUITE} #{'thread'.pluralize(EXPECTED_THREAD_COUNT_BEFORE_SUITE)} " \
"expected before suite runs"
)
end
end
@configured = true
end
@configured
end
end

View File

@ -55,3 +55,5 @@ RSpec.configure do |config|
# instead of true.
config.use_transactional_fixtures = true
end
Metasploit::Framework::Spec::Threads::Suite.configure!