Retryable test
This commit is contained in:
parent
15a625275e
commit
43fcca3dcb
|
@ -0,0 +1,106 @@
|
|||
require File.expand_path("../../../base", __FILE__)
|
||||
|
||||
require "vagrant/util/retryable"
|
||||
|
||||
describe Vagrant::Util::Retryable do
|
||||
let(:klass) do
|
||||
Class.new do
|
||||
extend Vagrant::Util::Retryable
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't retry by default" do
|
||||
tries = 0
|
||||
|
||||
block = lambda do
|
||||
tries += 1
|
||||
raise RuntimeError, "Try"
|
||||
end
|
||||
|
||||
# It should re-raise the error
|
||||
expect { klass.retryable(&block) }.
|
||||
to raise_error(RuntimeError)
|
||||
|
||||
# It should've tried once
|
||||
tries.should == 1
|
||||
end
|
||||
|
||||
it "retries the set number of times" do
|
||||
tries = 0
|
||||
|
||||
block = lambda do
|
||||
tries += 1
|
||||
raise RuntimeError, "Try"
|
||||
end
|
||||
|
||||
# It should re-raise the error
|
||||
expect { klass.retryable(:tries => 5, &block) }.
|
||||
to raise_error(RuntimeError)
|
||||
|
||||
# It should've tried all specified times
|
||||
tries.should == 5
|
||||
end
|
||||
|
||||
it "only retries on the given exception" do
|
||||
tries = 0
|
||||
|
||||
block = lambda do
|
||||
tries += 1
|
||||
raise StandardError, "Try"
|
||||
end
|
||||
|
||||
# It should re-raise the error
|
||||
expect { klass.retryable(:tries => 5, :on => RuntimeError, &block) }.
|
||||
to raise_error(StandardError)
|
||||
|
||||
# It should've never tried since it was a different kind of error
|
||||
tries.should == 1
|
||||
end
|
||||
|
||||
it "can retry on multiple types of errors" do
|
||||
tries = 0
|
||||
|
||||
foo_error = Class.new(StandardError)
|
||||
bar_error = Class.new(StandardError)
|
||||
|
||||
block = lambda do
|
||||
tries += 1
|
||||
raise foo_error, "Try" if tries == 1
|
||||
raise bar_error, "Try" if tries == 2
|
||||
raise RuntimeError, "YAY"
|
||||
end
|
||||
|
||||
# It should re-raise the error
|
||||
expect { klass.retryable(:tries => 5, :on => [foo_error, bar_error], &block) }.
|
||||
to raise_error(RuntimeError)
|
||||
|
||||
# It should've never tried since it was a different kind of error
|
||||
tries.should == 3
|
||||
end
|
||||
|
||||
it "doesn't sleep between tries by default" do
|
||||
block = lambda do
|
||||
raise RuntimeError, "Try"
|
||||
end
|
||||
|
||||
# Sleep should never be called
|
||||
klass.should_not_receive(:sleep)
|
||||
|
||||
# Run it.
|
||||
expect { klass.retryable(:tries => 5, &block) }.
|
||||
to raise_error(RuntimeError)
|
||||
end
|
||||
|
||||
it "sleeps specified amount between retries" do
|
||||
block = lambda do
|
||||
raise RuntimeError, "Try"
|
||||
end
|
||||
|
||||
# Sleep should be called between each retry
|
||||
klass.should_receive(:sleep).with(10).exactly(4).times
|
||||
|
||||
# Run it.
|
||||
expect { klass.retryable(:tries => 5, :sleep => 10, &block) }.
|
||||
to raise_error(RuntimeError)
|
||||
end
|
||||
end
|
|
@ -1,50 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class RetryableUtilTest < Test::Unit::TestCase
|
||||
setup do
|
||||
@klass = Class.new do
|
||||
extend Vagrant::Util::Retryable
|
||||
end
|
||||
end
|
||||
|
||||
should "retry specified number of times if exception is raised" do
|
||||
proc = mock("proc")
|
||||
proc.expects(:call).twice
|
||||
|
||||
assert_raises(RuntimeError) {
|
||||
@klass.retryable(:tries => 2, :on => RuntimeError) do
|
||||
proc.call
|
||||
raise "An error"
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
should "only retry on specified exception" do
|
||||
proc = mock("proc")
|
||||
proc.expects(:call).once
|
||||
|
||||
assert_raises(StandardError) {
|
||||
@klass.retryable(:tries => 5, :on => RuntimeError) do
|
||||
proc.call
|
||||
raise StandardError.new
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
should "retry on multiple exceptions given" do
|
||||
proc = mock("proc")
|
||||
proc.expects(:call).twice
|
||||
|
||||
assert_raises(StandardError) {
|
||||
@klass.retryable(:tries => 2, :on => [StandardError, RuntimeError]) do
|
||||
proc.call
|
||||
raise StandardError
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
should "return the value of the block" do
|
||||
result = @klass.retryable { 7 }
|
||||
assert_equal 7, result
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue