Class: ParallelRSpec::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_rspec/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reporter) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/parallel_rspec/server.rb', line 5

def initialize(reporter)
  @remaining_examples_by_group = RSpec.world.filtered_examples.each_with_object({}) do |(example_group, examples), results|
    results[example_group] = examples unless example_group.any_context_hooks? || examples.size.zero?
  end
  @reporter = reporter
  @success = true
  @running_examples = {}
  @top_level_groups = @remaining_examples_by_group.each_with_object({}) do |(group, examples), results|
    results[top_level(group)] ||= { started: false, count: 0 }
    results[top_level(group)][:count] += examples.size
  end
end

Instance Attribute Details

#remaining_examples_by_groupObject (readonly)

Returns the value of attribute remaining_examples_by_group.



3
4
5
# File 'lib/parallel_rspec/server.rb', line 3

def remaining_examples_by_group
  @remaining_examples_by_group
end

#reporterObject (readonly)

Returns the value of attribute reporter.



3
4
5
# File 'lib/parallel_rspec/server.rb', line 3

def reporter
  @reporter
end

#running_examplesObject (readonly)

Returns the value of attribute running_examples.



3
4
5
# File 'lib/parallel_rspec/server.rb', line 3

def running_examples
  @running_examples
end

#top_level_groupsObject (readonly)

Returns the value of attribute top_level_groups.



3
4
5
# File 'lib/parallel_rspec/server.rb', line 3

def top_level_groups
  @top_level_groups
end

Instance Method Details

#deprecation(hash, channel_to_client) ⇒ Object



54
55
56
# File 'lib/parallel_rspec/server.rb', line 54

def deprecation(hash, channel_to_client)
  reporter.deprecation(hash)
end

#example_failed(example_id, example_updates, channel_to_client) ⇒ Object



46
47
48
# File 'lib/parallel_rspec/server.rb', line 46

def example_failed(example_id, example_updates, channel_to_client)
  reporter.example_failed(update_example(running_examples.delete(example_id), example_updates))
end

#example_finished(example_id, example_updates, channel_to_client) ⇒ Object



37
38
39
40
# File 'lib/parallel_rspec/server.rb', line 37

def example_finished(example_id, example_updates, channel_to_client)
  reporter.example_finished(update_example(running_examples[example_id], example_updates))
  report_example_group_finished(running_examples[example_id].example_group)
end

#example_passed(example_id, example_updates, channel_to_client) ⇒ Object



42
43
44
# File 'lib/parallel_rspec/server.rb', line 42

def example_passed(example_id, example_updates, channel_to_client)
  reporter.example_passed(update_example(running_examples.delete(example_id), example_updates))
end

#example_pending(example_id, example_updates, channel_to_client) ⇒ Object



50
51
52
# File 'lib/parallel_rspec/server.rb', line 50

def example_pending(example_id, example_updates, channel_to_client)
  reporter.example_pending(update_example(running_examples.delete(example_id), example_updates))
end

#example_started(example_id, example_updates, channel_to_client) ⇒ Object



32
33
34
35
# File 'lib/parallel_rspec/server.rb', line 32

def example_started(example_id, example_updates, channel_to_client)
  reporter.example_started(update_example(running_examples[example_id], example_updates))
  report_example_group_started(running_examples[example_id].example_group)
end

#next_example_to_run(channel_to_client) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/parallel_rspec/server.rb', line 58

def next_example_to_run(channel_to_client)
  if remaining_examples_by_group.empty?
    channel_to_client.write(nil)
  else
    example_group = remaining_examples_by_group.keys.first
    example = remaining_examples_by_group[example_group].pop
    running_examples[example.id] = example # cache so we don't need to look through all the examples for each message
    channel_to_client.write([example_group, remaining_examples_by_group[example_group].size])
    remaining_examples_by_group.delete(example_group) if remaining_examples_by_group[example_group].empty?
  end
end

#report_example_group_finished(group) ⇒ Object



26
27
28
29
30
# File 'lib/parallel_rspec/server.rb', line 26

def report_example_group_finished(group)
  top_level_group = top_level(group)
  top_level_groups[top_level_group][:count] -= 1
  reporter.example_group_finished(top_level_group) if top_level_groups[top_level_group][:count].zero?
end

#report_example_group_started(group) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/parallel_rspec/server.rb', line 18

def report_example_group_started(group)
  top_level_group = top_level(group)
  return if top_level_groups[top_level_group][:started] # already reported as started

  top_level_groups[top_level_group][:started] = true
  reporter.example_group_started(top_level_group)
end

#result(success, channel_to_client) ⇒ Object



70
71
72
# File 'lib/parallel_rspec/server.rb', line 70

def result(success, channel_to_client)
  @success &&= success
end

#success?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/parallel_rspec/server.rb', line 74

def success?
  @success
end

#update_example(example, data) ⇒ Object



78
79
80
81
82
# File 'lib/parallel_rspec/server.rb', line 78

def update_example(example, data)
  example.set_exception(data[:exception])
  example..merge!(data[:metadata])
  example
end