Skip to content

Commit

Permalink
Add max_retry_count & wait options to Command::Base#step
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr committed Jul 22, 2024
1 parent 594b706 commit 9c921c1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ RSpec/ExampleLength:

RSpec/MultipleExpectations:
Enabled: false

RSpec/NestedGroups:
Enabled: true
Max: 5
11 changes: 8 additions & 3 deletions lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,22 @@ def step_finish(success)
end
end

def step(message, abort_on_error: true, retry_on_failure: false) # rubocop:disable Metrics/MethodLength
def step(message, abort_on_error: true, retry_on_failure: false, max_retry_count: 5, wait: 1) # rubocop:disable Metrics/MethodLength
progress.print("#{message}...")

Shell.use_tmp_stderr do
success = false
run_count = 0

begin
if retry_on_failure
until (success = yield)
while run_count < max_retry_count
success = yield

progress.print(".")
Kernel.sleep(1)
Kernel.sleep(wait)

run_count += 1
end
else
success = yield
Expand Down
62 changes: 62 additions & 0 deletions spec/command/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require "spec_helper"

describe Command::Base do
subject(:command) { described_class.new(config) }

let(:config) { instance_double(Command::Config) }

around do |example|
suppress_output { example.run }
end

describe "#step" do
let(:message) { "test message" }

context "with retry_on_failure: true" do
let(:options) { { retry_on_failure: true, wait: 0 } }

it "retries block 5 times by default" do
run_count = 0

command.step(message, **options) do
run_count += 1
false
end

expect(run_count).to eq 5
end

context "with max_retry_count option" do
let(:options) { super().merge(max_retry_count: 1) }

it "retries block specified times" do
run_count = 0

command.step(message, **options) do
run_count += 1
false
end

expect(run_count).to eq 1
end
end
end

context "with retry_on_failure: false" do
let(:options) { { retry_on_failure: false } }

it "does not retry block" do
run_count = 0

command.step(message, **options) do
run_count += 1
false
end

expect(run_count).to eq 1
end
end
end
end
10 changes: 10 additions & 0 deletions spec/support/command_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ def spawn_cpflow_command(*args, stty_rows: nil, stty_cols: nil, wait_for_process
end
end

def suppress_output
original_stderr = replace_stderr
original_stdout = replace_stdout

yield
ensure
restore_stderr(original_stderr)
restore_stdout(original_stdout)
end

def replace_stderr
original_stderr = $stderr
$stderr = Tempfile.create
Expand Down

0 comments on commit 9c921c1

Please sign in to comment.