Class: RSpec::Parallel::ExampleThreadRunner
- Inherits:
-
Object
- Object
- RSpec::Parallel::ExampleThreadRunner
- Defined in:
- lib/rspec/parallel/example_thread_runner.rb
Overview
ExampleThreadRunner is a class used to execute [Example] classes in parallel as part of rspec-core. When running in parallel the order of examples 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
Instance Attribute Summary collapse
-
#num_threads ⇒ Object
Returns the value of attribute num_threads.
-
#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(num_threads, used_threads) ⇒ ExampleThreadRunner
constructor
Creates a new instance of ExampleThreadRunner.
-
#run(example, instance, reporter) ⇒ Object
Method will run the specified example within an available thread or will wait for a thread to become available if none currently are tracking.
-
#wait_for_available_thread ⇒ Object
Method will check global utilization of threads and if that number is at or over the allocated maximum it will wait until a thread is available.
-
#wait_for_completion ⇒ Object
Method will wait for all threads to complete.
Constructor Details
#initialize(num_threads, used_threads) ⇒ ExampleThreadRunner
Creates a new instance of ExampleThreadRunner.
15 16 17 18 19 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 15 def initialize(num_threads, used_threads) @num_threads = num_threads @thread_array = [] @used_threads = used_threads end |
Instance Attribute Details
#num_threads ⇒ Object
Returns the value of attribute num_threads.
10 11 12 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 10 def num_threads @num_threads end |
#thread_array ⇒ Object
Returns the value of attribute thread_array.
10 11 12 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 10 def thread_array @thread_array end |
#used_threads ⇒ Object
Returns the value of attribute used_threads.
10 11 12 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 10 def used_threads @used_threads end |
Instance Method Details
#run(example, instance, reporter) ⇒ Object
Method will run the specified example within an available thread or will wait for a thread to become available if none currently are tracking
35 36 37 38 39 40 41 42 43 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 35 def run(example, instance, reporter) wait_for_available_thread @thread_array.push Thread.start { example.run(instance, reporter) @thread_array.delete Thread.current # remove from local scope @used_threads -= 1 } @used_threads += 1 end |
#wait_for_available_thread ⇒ Object
Method will check global utilization of threads and if that number is at or over the allocated maximum it will wait until a thread is available
23 24 25 26 27 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 23 def wait_for_available_thread while @used_threads.to_i >= @num_threads.to_i sleep 0.1 end 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
48 49 50 51 52 |
# File 'lib/rspec/parallel/example_thread_runner.rb', line 48 def wait_for_completion @thread_array.each do |t| t.join end end |