Class: RSpec::Parallel::ExampleThreadRunner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(num_threads, used_threads) ⇒ ExampleThreadRunner

Creates a new instance of ExampleThreadRunner.

Parameters:

  • num_threads (Integer)

    the maximum limit of threads that can be used

  • used_threads (Integer)

    the current number of threads being used



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_threadsObject

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_arrayObject

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_threadsObject

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

Parameters:

  • example (Example)

    the example to be executed in a [Thread]

  • instance

    the instance of an ExampleGroup subclass

  • reporter (Reporter)

    the passed in reporting class used for



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_threadObject

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_completionObject

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