Class: RSpec::Parallel::ExampleGroupThreadRunner
- Inherits:
-
Object
- Object
- RSpec::Parallel::ExampleGroupThreadRunner
- Defined in:
- lib/rspec/parallel/example_group_thread_runner.rb
Overview
ExampleGroupThreadRunner is a class used to execute [ExampleGroup] classes in parallel as part of rspec-core. When running in parallel the order of example groups will not be honoured.
This class is used to ensure that we have a way of keeping track of the number of threads being created and preventing utilization of more than the specified number Additionally, this class will contain a mutex used to prevent access to shared variables within sub-threads
Instance Attribute Summary collapse
-
#max_threads ⇒ Object
Returns the value of attribute max_threads.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#thread_array ⇒ Object
Returns the value of attribute thread_array.
-
#used_threads ⇒ Object
Returns the value of attribute used_threads.
Instance Method Summary collapse
-
#initialize(max_threads = 1, mutex = Mutex.new, used_threads = 0) ⇒ ExampleGroupThreadRunner
constructor
Creates a new instance of ExampleGroupThreadRunner.
-
#run(example_group, reporter) ⇒ Object
Method will run an [ExampleGroup] inside a [Thread] to prevent blocking execution.
-
#wait_for_completion ⇒ Object
Method will wait for all threads to complete.
Constructor Details
#initialize(max_threads = 1, mutex = Mutex.new, used_threads = 0) ⇒ ExampleGroupThreadRunner
Creates a new instance of ExampleGroupThreadRunner. sub-threads such as those used by [ExampleThreadRunner]
19 20 21 22 23 24 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 19 def initialize(max_threads = 1, mutex = Mutex.new, used_threads = 0) @max_threads = max_threads @mutex = mutex @used_threads = used_threads @thread_array = [] end |
Instance Attribute Details
#max_threads ⇒ Object
Returns the value of attribute max_threads.
12 13 14 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 12 def max_threads @max_threads end |
#mutex ⇒ Object
Returns the value of attribute mutex.
12 13 14 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 12 def mutex @mutex end |
#thread_array ⇒ Object
Returns the value of attribute thread_array.
12 13 14 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 12 def thread_array @thread_array end |
#used_threads ⇒ Object
Returns the value of attribute used_threads.
12 13 14 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 12 def used_threads @used_threads end |
Instance Method Details
#run(example_group, reporter) ⇒ Object
Method will run an [ExampleGroup] inside a [Thread] to prevent blocking execution. The new [Thread] is added to an array for tracking and will automatically remove itself when done tracking
32 33 34 35 36 37 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 32 def run(example_group, reporter) @thread_array.push Thread.start { example_group.run_parallel(reporter, @max_threads, @mutex, @used_threads) @thread_array.delete Thread.current } end |
#wait_for_completion ⇒ Object
Method will wait for all threads to complete. On completion threads remove themselves from the @thread_array so an empty array means they completed
42 43 44 45 46 |
# File 'lib/rspec/parallel/example_group_thread_runner.rb', line 42 def wait_for_completion @thread_array.each do |t| t.join end end |