Skip to content

Commit 3fe0ce0

Browse files
committed
Remove default seed value
Changes * Remove the default `seed` value (ref: #33) * Use exit status from RSpec calls (ref: #20)
1 parent b9e812e commit 3fe0ce0

File tree

4 files changed

+157
-95
lines changed

4 files changed

+157
-95
lines changed

lib/turbo_tests/cli.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def run
9898
end
9999
end
100100

101-
success = TurboTests::Runner.run(
101+
exitstatus = TurboTests::Runner.run(
102102
formatters: formatters,
103103
tags: tags,
104104
files: @argv.empty? ? ["spec"] : @argv,
@@ -109,11 +109,8 @@ def run
109109
seed: seed
110110
)
111111

112-
if success
113-
exit 0
114-
else
115-
exit 1
116-
end
112+
# From https://github.com/serpapi/turbo_tests/pull/20/
113+
exit exitstatus
117114
end
118115
end
119116
end

lib/turbo_tests/reporter.rb

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module TurboTests
44
class Reporter
55
attr_writer :load_time
66

7-
def self.from_config(formatter_config, start_time)
8-
reporter = new(start_time)
7+
def self.from_config(formatter_config, start_time, seed, seed_used)
8+
reporter = new(start_time, seed, seed_used)
99

1010
formatter_config.each do |config|
1111
name, outputs = config.values_at(:name, :outputs)
@@ -23,13 +23,15 @@ def self.from_config(formatter_config, start_time)
2323
attr_reader :pending_examples
2424
attr_reader :failed_examples
2525

26-
def initialize(start_time)
26+
def initialize(start_time, seed, seed_used)
2727
@formatters = []
2828
@pending_examples = []
2929
@failed_examples = []
3030
@all_examples = []
3131
@messages = []
3232
@start_time = start_time
33+
@seed = seed
34+
@seed_used = seed_used
3335
@load_time = 0
3436
@errors_outside_of_examples_count = 0
3537
end
@@ -50,6 +52,33 @@ def add(name, outputs)
5052
end
5153
end
5254

55+
# Borrowed from RSpec::Core::Reporter
56+
# https://github.com/rspec/rspec-core/blob/1eeadce5aa7137ead054783c31ff35cbfe9d07cc/lib/rspec/core/reporter.rb#L206
57+
def report(expected_example_count)
58+
start(expected_example_count)
59+
begin
60+
yield self
61+
ensure
62+
finish
63+
end
64+
end
65+
66+
def start(example_groups)
67+
delegate_to_formatters(:seed, RSpec::Core::Notifications::SeedNotification.new(@seed, @seed_used))
68+
69+
report_number_of_tests(example_groups)
70+
end
71+
72+
def report_number_of_tests(groups)
73+
name = ParallelTests::RSpec::Runner.test_file_name
74+
75+
num_processes = groups.size
76+
num_tests = groups.map(&:size).sum
77+
tests_per_process = (num_processes == 0 ? 0 : num_tests.to_f / num_processes).round
78+
79+
puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{tests_per_process} #{name}s per process"
80+
end
81+
5382
def group_started(notification)
5483
delegate_to_formatters(:example_group_started, notification)
5584
end
@@ -88,8 +117,7 @@ def error_outside_of_examples
88117
end
89118

90119
def finish
91-
# SEE: https://bit.ly/2NP87Cz
92-
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
120+
end_time = RSpec::Core::Time.now
93121

94122
delegate_to_formatters(:start_dump,
95123
RSpec::Core::Notifications::NullNotification)
@@ -112,11 +140,11 @@ def finish
112140
))
113141
delegate_to_formatters(:close,
114142
RSpec::Core::Notifications::NullNotification)
115-
end
116-
117-
def seed_notification(seed, seed_used)
118-
puts RSpec::Core::Notifications::SeedNotification.new(seed, seed_used).fully_formatted
119-
puts
143+
delegate_to_formatters(:seed,
144+
RSpec::Core::Notifications::SeedNotification.new(
145+
@seed,
146+
@seed_used,
147+
))
120148
end
121149

122150
protected

lib/turbo_tests/runner.rb

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ def self.run(opts = {})
1414
formatters = opts[:formatters]
1515
tags = opts[:tags]
1616

17-
# SEE: https://bit.ly/2NP87Cz
18-
start_time = opts.fetch(:start_time) { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
17+
start_time = opts.fetch(:start_time) { RSpec::Core::Time.now }
1918
runtime_log = opts.fetch(:runtime_log, nil)
2019
verbose = opts.fetch(:verbose, false)
2120
fail_fast = opts.fetch(:fail_fast, nil)
2221
count = opts.fetch(:count, nil)
23-
seed = opts.fetch(:seed) || rand(0xFFFF).to_s
24-
seed_used = !opts[:seed].nil?
22+
seed = opts.fetch(:seed)
23+
seed_used = !seed.nil?
2524

2625
if verbose
2726
STDERR.puts "VERBOSE"
2827
end
2928

30-
reporter = Reporter.from_config(formatters, start_time)
29+
reporter = Reporter.from_config(formatters, start_time, seed, seed_used)
3130

3231
new(
3332
reporter: reporter,
@@ -38,7 +37,7 @@ def self.run(opts = {})
3837
fail_fast: fail_fast,
3938
count: count,
4039
seed: seed,
41-
seed_used: seed_used
40+
seed_used: seed_used,
4241
).run
4342
end
4443

@@ -50,11 +49,12 @@ def initialize(opts)
5049
@verbose = opts[:verbose]
5150
@fail_fast = opts[:fail_fast]
5251
@count = opts[:count]
52+
@seed = opts[:seed]
53+
@seed_used = opts[:seed_used]
54+
5355
@load_time = 0
5456
@load_count = 0
5557
@failure_count = 0
56-
@seed = opts[:seed]
57-
@seed_used = opts[:seed_used]
5858

5959
@messages = Thread::Queue.new
6060
@threads = []
@@ -87,26 +87,25 @@ def run
8787
setup_tmp_dir
8888

8989
subprocess_opts = {
90-
record_runtime: use_runtime_info
90+
record_runtime: use_runtime_info,
9191
}
9292

93-
report_number_of_tests(tests_in_groups)
94-
95-
@reporter.seed_notification(@seed, @seed_used)
96-
97-
wait_threads = tests_in_groups.map.with_index do |tests, process_id|
98-
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
99-
end
100-
101-
handle_messages
102-
103-
@reporter.finish
93+
@reporter.report(tests_in_groups) do |reporter|
94+
wait_threads = tests_in_groups.map.with_index do |tests, process_id|
95+
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
96+
end
10497

105-
@reporter.seed_notification(@seed, @seed_used)
98+
handle_messages
10699

107-
@threads.each(&:join)
100+
@threads.each(&:join)
108101

109-
@reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
102+
if @reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
103+
0
104+
else
105+
# From https://github.com/serpapi/turbo_tests/pull/20/
106+
wait_threads.map { |thread| thread.value.exitstatus }.max
107+
end
108+
end
110109
end
111110

112111
private
@@ -157,10 +156,18 @@ def start_subprocess(env, extra_args, tests, process_id, record_runtime:)
157156
[]
158157
end
159158

159+
seed_option = if @seed_used
160+
[
161+
"--seed", @seed,
162+
]
163+
else
164+
[]
165+
end
166+
160167
command = [
161168
"rspec",
162169
*extra_args,
163-
"--seed", @seed,
170+
*seed_option,
164171
"--format", "TurboTests::JsonRowsFormatter",
165172
"--out", tmp_filename,
166173
*record_runtime_options,
@@ -273,15 +280,5 @@ def handle_messages
273280
def fail_fast_met
274281
!@fail_fast.nil? && @failure_count >= @fail_fast
275282
end
276-
277-
def report_number_of_tests(groups)
278-
name = ParallelTests::RSpec::Runner.test_file_name
279-
280-
num_processes = groups.size
281-
num_tests = groups.map(&:size).sum
282-
tests_per_process = (num_processes == 0 ? 0 : num_tests.to_f / num_processes).round
283-
284-
puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{tests_per_process} #{name}s per process"
285-
end
286283
end
287284
end

0 commit comments

Comments
 (0)