Skip to content

Commit

Permalink
Merge pull request #48 from serpapi/remove-default-seed
Browse files Browse the repository at this point in the history
Remove default `seed` value
  • Loading branch information
ilyazub committed Jun 18, 2024
2 parents bb814e1 + 4115412 commit aeae4ff
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 141 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/snyk_ruby-analysis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
turbo_tests (2.2.3)
turbo_tests (2.2.4)
parallel_tests (>= 3.3.0, < 5)
rspec (>= 3.10)

Expand Down
3 changes: 3 additions & 0 deletions fixtures/rspec/no_method_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.describe "NoMethodError spec" do
it("fails") { expect(nil[:key]).to eql("value") }
end
9 changes: 3 additions & 6 deletions lib/turbo_tests/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run
end
end

success = TurboTests::Runner.run(
exitstatus = TurboTests::Runner.run(
formatters: formatters,
tags: tags,
files: @argv.empty? ? ["spec"] : @argv,
Expand All @@ -109,11 +109,8 @@ def run
seed: seed
)

if success
exit 0
else
exit 1
end
# From https://github.com/serpapi/turbo_tests/pull/20/
exit exitstatus
end
end
end
66 changes: 51 additions & 15 deletions lib/turbo_tests/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module TurboTests
class Reporter
attr_writer :load_time

def self.from_config(formatter_config, start_time)
reporter = new(start_time)
def self.from_config(formatter_config, start_time, seed, seed_used)
reporter = new(start_time, seed, seed_used)

formatter_config.each do |config|
name, outputs = config.values_at(:name, :outputs)
Expand All @@ -23,13 +23,15 @@ def self.from_config(formatter_config, start_time)
attr_reader :pending_examples
attr_reader :failed_examples

def initialize(start_time)
def initialize(start_time, seed, seed_used)
@formatters = []
@pending_examples = []
@failed_examples = []
@all_examples = []
@messages = []
@start_time = start_time
@seed = seed
@seed_used = seed_used
@load_time = 0
@errors_outside_of_examples_count = 0
end
Expand All @@ -50,6 +52,38 @@ def add(name, outputs)
end
end

# Borrowed from RSpec::Core::Reporter
# https://github.com/rspec/rspec-core/blob/5699fcdc4723087ff6139af55bd155ad9ad61a7b/lib/rspec/core/reporter.rb#L71
def report(example_groups)
start(example_groups)
begin
yield self
ensure
finish
end
end

def start(example_groups, time=RSpec::Core::Time.now)
@start = time
@load_time = (@start - @start_time).to_f

report_number_of_tests(example_groups)
expected_example_count = example_groups.flatten(1).count

delegate_to_formatters(:seed, RSpec::Core::Notifications::SeedNotification.new(@seed, @seed_used))
delegate_to_formatters(:start, RSpec::Core::Notifications::StartNotification.new(expected_example_count, @load_time))
end

def report_number_of_tests(groups)
name = ParallelTests::RSpec::Runner.test_file_name

num_processes = groups.size
num_tests = groups.map(&:size).sum
tests_per_process = (num_processes == 0 ? 0 : num_tests.to_f / num_processes).round

puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{tests_per_process} #{name}s per process"
end

def group_started(notification)
delegate_to_formatters(:example_group_started, notification)
end
Expand Down Expand Up @@ -83,16 +117,18 @@ def message(message)
@messages << message
end

def error_outside_of_examples
def error_outside_of_examples(error_message)
@errors_outside_of_examples_count += 1
message error_message
end

def finish
# SEE: https://bit.ly/2NP87Cz
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end_time = RSpec::Core::Time.now

@duration = end_time - @start_time
delegate_to_formatters :stop, RSpec::Core::Notifications::ExamplesNotification.new(self)

delegate_to_formatters(:start_dump,
RSpec::Core::Notifications::NullNotification)
delegate_to_formatters :start_dump, RSpec::Core::Notifications::NullNotification
delegate_to_formatters(:dump_pending,
RSpec::Core::Notifications::ExamplesNotification.new(
self
Expand All @@ -110,13 +146,13 @@ def finish
@load_time,
@errors_outside_of_examples_count
))
delegate_to_formatters(:close,
RSpec::Core::Notifications::NullNotification)
end

def seed_notification(seed, seed_used)
puts RSpec::Core::Notifications::SeedNotification.new(seed, seed_used).fully_formatted
puts
delegate_to_formatters(:seed,
RSpec::Core::Notifications::SeedNotification.new(
@seed,
@seed_used,
))
ensure
delegate_to_formatters :close, RSpec::Core::Notifications::NullNotification
end

protected
Expand Down
78 changes: 39 additions & 39 deletions lib/turbo_tests/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ def self.run(opts = {})
formatters = opts[:formatters]
tags = opts[:tags]

# SEE: https://bit.ly/2NP87Cz
start_time = opts.fetch(:start_time) { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
start_time = opts.fetch(:start_time) { RSpec::Core::Time.now }
runtime_log = opts.fetch(:runtime_log, nil)
verbose = opts.fetch(:verbose, false)
fail_fast = opts.fetch(:fail_fast, nil)
count = opts.fetch(:count, nil)
seed = opts.fetch(:seed) || rand(0xFFFF).to_s
seed_used = !opts[:seed].nil?
seed = opts.fetch(:seed)
seed_used = !seed.nil?

if verbose
warn "VERBOSE"
end

reporter = Reporter.from_config(formatters, start_time)
reporter = Reporter.from_config(formatters, start_time, seed, seed_used)

new(
reporter: reporter,
Expand All @@ -38,7 +37,7 @@ def self.run(opts = {})
fail_fast: fail_fast,
count: count,
seed: seed,
seed_used: seed_used
seed_used: seed_used,
).run
end

Expand All @@ -50,11 +49,12 @@ def initialize(opts)
@verbose = opts[:verbose]
@fail_fast = opts[:fail_fast]
@count = opts[:count]
@seed = opts[:seed]
@seed_used = opts[:seed_used]

@load_time = 0
@load_count = 0
@failure_count = 0
@seed = opts[:seed]
@seed_used = opts[:seed_used]

@messages = Thread::Queue.new
@threads = []
Expand Down Expand Up @@ -87,26 +87,25 @@ def run
setup_tmp_dir

subprocess_opts = {
record_runtime: use_runtime_info
record_runtime: use_runtime_info,
}

report_number_of_tests(tests_in_groups)

@reporter.seed_notification(@seed, @seed_used)

wait_threads = tests_in_groups.map.with_index do |tests, process_id|
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
end

handle_messages

@reporter.finish
@reporter.report(tests_in_groups) do |reporter|
wait_threads = tests_in_groups.map.with_index do |tests, process_id|
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
end

@reporter.seed_notification(@seed, @seed_used)
handle_messages

@threads.each(&:join)
@threads.each(&:join)

@reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
if @reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
0
else
# From https://github.com/serpapi/turbo_tests/pull/20/
wait_threads.map { |thread| thread.value.exitstatus }.max
end
end
end

private
Expand Down Expand Up @@ -157,12 +156,18 @@ def start_subprocess(env, extra_args, tests, process_id, record_runtime:)
[]
end

seed_option = if @seed_used
[
"--seed", @seed,
]
else
[]
end

command = [
*command_name,
*extra_args,
"--seed", rand(0xFFFF).to_s,
"--format", "ParallelTests::RSpec::RuntimeLogger",
"--out", @runtime_log,
*seed_option,
"--format", "TurboTests::JsonRowsFormatter",
*record_runtime_options,
*tests,
Expand Down Expand Up @@ -254,12 +259,17 @@ def handle_messages
break
end
when "message"
@reporter.message(message[:message])
if message[:message].include?("An error occurred") || message[:message].include?("occurred outside of examples")
@reporter.error_outside_of_examples(message[:message])
@error = true
else
@reporter.message(message[:message])
end
when "seed"
when "close"
when "error"
@reporter.error_outside_of_examples
@error = true
# Do nothing
nil
when "exit"
exited += 1
if exited == @num_processes
Expand All @@ -277,15 +287,5 @@ def handle_messages
def fail_fast_met
!@fail_fast.nil? && @failure_count >= @fail_fast
end

def report_number_of_tests(groups)
name = ParallelTests::RSpec::Runner.test_file_name

num_processes = groups.size
num_tests = groups.map(&:size).sum
tests_per_process = (num_processes == 0 ? 0 : num_tests.to_f / num_processes).round

puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{tests_per_process} #{name}s per process"
end
end
end
2 changes: 1 addition & 1 deletion lib/turbo_tests/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TurboTests
VERSION = "2.2.3"
VERSION = "2.2.4"
end
Loading

0 comments on commit aeae4ff

Please sign in to comment.