Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TEST_ENV_NUMBER to support duplicate file groups #943

Merged
merged 6 commits into from Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/parallel_tests/cli.rb
Expand Up @@ -82,7 +82,8 @@ def run_tests_in_parallel(num_processes, options)

report_number_of_tests(groups) unless options[:quiet]
test_results = execute_in_parallel(groups, groups.size, options) do |group|
run_tests(group, groups.index(group), num_processes, options)
test_env = env_index(options).call(groups, group)
run_tests(group, test_env, num_processes, options)
end
report_results(test_results, options) unless options[:quiet]
end
Expand All @@ -107,6 +108,19 @@ def run_tests_in_parallel(num_processes, options)
end
end

def env_index(options)
jaydorsey marked this conversation as resolved.
Show resolved Hide resolved
return @env_index if defined?(@env_index)

start = options[:first_is_1] ? 0 : -1

@env_index =
if options[:allow_duplicates]
proc { start += 1 }
else
->(groups, group) { groups.index(group) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the original behavior. As I was writing this, I was wondering:

  1. Could we use the proc/counter as a default for everything?
  2. Is there something else I'm missing like "spec order randomization" where it's not as simple as just using an incrementing counter (e.g. maybe we don't even need the proc; we could just increment)

If it's not as simple as this proc (or a basic counter) I'm open to other suggestions; the only other idea I had was to track the index in a hash with a counter so I could see when I've re-used a group already.

If none of these ideas would work, then maybe --allow-duplicates should only work with a single file? This would be OK for my primary use case (and still useful I think)

end
end

def run_tests(group, process_number, num_processes, options)
if group.empty?
{ stdout: '', exit_status: 0, command: nil, seed: nil }
Expand Down
39 changes: 39 additions & 0 deletions spec/parallel_tests/cli_spec.rb
Expand Up @@ -361,6 +361,45 @@ def self.it_prints_nothing_about_rerun_commands(options)
subject.run(['test', '-n', '3', '--only-group', '2,3', '-t', 'my_test_runner'])
end
end

context 'when --allow-duplicates' do
jaydorsey marked this conversation as resolved.
Show resolved Hide resolved
let(:results) { { stdout: "", exit_status: 0 } }
let(:processes) { 2 }
let(:common_options) do
{ files: ['test'], allow_duplicates: true, first_is_1: false }
end
before do
allow(subject).to receive(:puts)
expect(subject).to receive(:load_runner).with("my_test_runner").and_return(ParallelTests::MyTestRunner::Runner)
allow(ParallelTests::MyTestRunner::Runner).to receive(:test_file_name).and_return("test")
expect(subject).to receive(:report_results).and_return(nil)
end

before do
expect(ParallelTests::MyTestRunner::Runner).to receive(:tests_in_groups).and_return(
[
['foo'],
['foo'],
['bar']
]
)
end

it "calls run_tests with --only-group" do
options = common_options.merge(count: processes, only_group: [2, 3], group_by: :filesize)
expect(subject).to receive(:run_tests).once.with(['foo'], 0, 1, options).and_return(results)
expect(subject).to receive(:run_tests).once.with(['bar'], 1, 1, options).and_return(results)
subject.run(['test', '-n', processes.to_s, '--allow-duplicates', '--only-group', '2,3', '-t', 'my_test_runner'])
end

it "calls run_tests with --first-is-1" do
options = common_options.merge(count: processes, first_is_1: true)
expect(subject).to receive(:run_tests).once.with(['foo'], 1, processes, options).and_return(results)
expect(subject).to receive(:run_tests).once.with(['foo'], 2, processes, options).and_return(results)
expect(subject).to receive(:run_tests).once.with(['bar'], 3, processes, options).and_return(results)
subject.run(['test', '-n', processes.to_s, '--first-is-1', '--allow-duplicates', '-t', 'my_test_runner'])
end
end
end

describe "#display_duration" do
Expand Down