Class: ParallelSpecs::Grouper

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_specs/grouper.rb

Class Method Summary collapse

Class Method Details

.in_even_groups_by_size(items, num_groups, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/parallel_specs/grouper.rb', line 6

def in_even_groups_by_size(items, num_groups, options = {})
  groups = Array.new(num_groups) { {items: [], size: 0} }
  single_process_patterns = options[:single_process] || []
  single_items, items = items.partition do |item, _size|
    single_process_patterns.any? { |pattern| item.match?(pattern) }
  end
  isolate_count = isolated_process_count(options)

  if options.key?(:isolate_count) && !isolate_count.positive?
    raise ParallelSpecs::ConfigurationError, "Isolated process count must be greater than 0"
  end
  if isolate_count >= num_groups
    raise ParallelSpecs::ConfigurationError, "Isolated process count must be less than total process count"
  end

  if isolate_count.positive?
    group_by_size(single_items, groups.first(isolate_count))
    group_by_size(items, groups.drop(isolate_count))
  else
    single_items.each { |item, size| add_to_group(groups.first, item, size) }
    group_by_size(items, groups)
  end

  groups.map { |group| group[:items].sort }
end