Class: RSpec::Parallel::ExampleGroupThreadRunner

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

Instance Method Summary collapse

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]

Parameters:

  • max_threads (Integer) (defaults to: 1)

    the maximum limit of threads that can be used

  • mutex (Mutex) (defaults to: Mutex.new)

    a semaphore used to prevent access to shared variables in

  • used_threads (Integer) (defaults to: 0)

    the current number of threads being used



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_threadsObject

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

#mutexObject

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_arrayObject

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_threadsObject

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

Parameters:

  • example_group (ExampleGroup)

    the group to be run inside a [Thread]

  • reporter (Reporter)

    the passed in reporting class used for



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_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



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