Class: Crspec::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concurrency: Etc.nprocessors, formatter: nil) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
15
16
17
18
19
# File 'lib/crspec/runner.rb', line 11

def initialize(concurrency: Etc.nprocessors, formatter: nil)
  @concurrency = concurrency
  @formatter = formatter || Formatters::ProgressFormatter.new
  @queue = Thread::Queue.new
  @passed_examples = []
  @failed_examples = []
  @mutex = Mutex.new
  @total_duration = 0
end

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



9
10
11
# File 'lib/crspec/runner.rb', line 9

def concurrency
  @concurrency
end

#failed_examplesObject (readonly)

Returns the value of attribute failed_examples.



9
10
11
# File 'lib/crspec/runner.rb', line 9

def failed_examples
  @failed_examples
end

#formatterObject (readonly)

Returns the value of attribute formatter.



9
10
11
# File 'lib/crspec/runner.rb', line 9

def formatter
  @formatter
end

#passed_examplesObject (readonly)

Returns the value of attribute passed_examples.



9
10
11
# File 'lib/crspec/runner.rb', line 9

def passed_examples
  @passed_examples
end

#total_durationObject (readonly)

Returns the value of attribute total_duration.



9
10
11
# File 'lib/crspec/runner.rb', line 9

def total_duration
  @total_duration
end

Instance Method Details

#run(example_groups) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/crspec/runner.rb', line 21

def run(example_groups)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @formatter.start
  example_groups.each { |group| enqueue_examples(group) }

  workers = Array.new(@concurrency) do |worker_idx|
    Thread.new do
      worker_number = worker_idx + 1
      Rails::Parallel.setup_worker(worker_number) if defined?(Rails::Parallel) && Rails::Parallel.enabled?

      Fiber.set_scheduler(Async::Scheduler.new) if defined?(Async::Scheduler)
      until @queue.empty?
        example = begin
          @queue.pop(true)
        rescue StandardError
          nil
        end
        break unless example

        execute_example(example)
      end
    ensure
      Rails::Parallel.teardown_worker(worker_number) if defined?(Rails::Parallel) && Rails::Parallel.enabled?
    end
  end

  workers.each(&:join)
  @total_duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  @formatter.finish
  self
end

#success?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/crspec/runner.rb', line 53

def success?
  @failed_examples.empty?
end