spec/attempt_spec.rb in attempt_this-1.0.0 vs spec/attempt_spec.rb in attempt_this-1.0.1
- old
+ new
@@ -3,15 +3,15 @@
describe AttemptThis do
include AttemptThis
context 'attempt' do
it 'should reject nil enumerator' do
- ->{attempt(nil)}.should raise_error(ArgumentError)
+ lambda{attempt(nil)}.should raise_error(ArgumentError)
end
it 'should allow execution without a code block' do
- ->{attempt(3.times)}.should_not raise_error
+ lambda{attempt(3.times)}.should_not raise_error
end
it 'should execute code block' do
was_called = false
attempt(1.times) {was_called = true}
@@ -30,16 +30,16 @@
call_count.should eql(0)
end
it 'should re-throw the original exception' do
- ->{attempt(2.times){raise 'Test'}}.should raise_error('Test')
+ lambda{attempt(2.times){raise 'Test'}}.should raise_error('Test')
end
it 'should attempt 3 times' do
call_count = 0
- ->{attempt(3.times) { call_count += 1; raise 'Test'}}.should raise_error('Test')
+ lambda{attempt(3.times) { call_count += 1; raise 'Test'}}.should raise_error('Test')
call_count.should eql(3)
end
it 'should stop trying after a successful attempt' do
attempt_count = 0
@@ -50,38 +50,38 @@
attempt_count.should eql(2)
end
it 'should return block\'s value' do
- expected = UUID.generate.to_s
+ expected = "some string"
attempt(3.times) { expected }.should eql(expected)
end
end
context 'with_delay' do
it 'should reject nil delay' do
- ->{attempt(3.times).with_delay(nil)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay(nil)}.should raise_error(ArgumentError)
end
it 'should reject negative delay' do
- ->{attempt(3.times).with_delay(-1)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay(-1)}.should raise_error(ArgumentError)
end
it 'should reject non-number delay' do
- ->{attempt(3.times).with_delay('foo')}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay('foo')}.should raise_error(ArgumentError)
end
it 'should accept floating point delay' do
- ->{attempt(3.times).with_delay(1.5)}.should_not raise_error
+ lambda{attempt(3.times).with_delay(1.5)}.should_not raise_error
end
it 'should accept zero delay' do
- ->{attempt(3.times).with_delay(0)}.should_not raise_error
+ lambda{attempt(3.times).with_delay(0)}.should_not raise_error
end
it 'should accept calls without a code block' do
- ->{attempt(3.times).with_delay(3)}.should_not raise_error
+ lambda{attempt(3.times).with_delay(3)}.should_not raise_error
end
it 'should call the code block' do
was_called = false
attempt(3.times).with_delay(1) do
@@ -95,151 +95,151 @@
attempt(3.times).with_delay(3) {}
end
it 'should sleep for given number of seconds between failed attempts' do
Kernel.should_receive(:sleep).with(5).exactly(2).times
- ->{attempt(3.times).with_delay(5) {raise 'Test'}}.should raise_error('Test')
+ lambda{attempt(3.times).with_delay(5) {raise 'Test'}}.should raise_error('Test')
end
it 'should not fail on zero delay' do
- ->{attempt(3.times).with_delay(0) { raise 'Test' }}.should raise_error('Test')
+ lambda{attempt(3.times).with_delay(0) { raise 'Test' }}.should raise_error('Test')
end
it 'should reject negative start' do
- ->{attempt(3.times).with_delay(-1..1)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay(-1..1)}.should raise_error(ArgumentError)
end
it 'should reject negative end' do
- ->{attempt(3.times).with_delay(1..-1)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay(1..-1)}.should raise_error(ArgumentError)
end
it 'should reject non-number range' do
- ->{attempt(3.times).with_delay('x'..'y')}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay('x'..'y')}.should raise_error(ArgumentError)
end
it 'should accept floating point range' do
- ->{attempt(3.times).with_delay(1.5..3)}.should_not raise_error
+ lambda{attempt(3.times).with_delay(1.5..3)}.should_not raise_error
end
it 'should reject inverse range' do
- ->{attempt(2.times).with_delay(3..1)}.should raise_error(ArgumentError)
+ lambda{attempt(2.times).with_delay(3..1)}.should raise_error(ArgumentError)
end
it 'should accept zero seconds interval' do
- ->{attempt(3.times).with_delay(0..0)}.should_not raise_error
+ lambda{attempt(3.times).with_delay(0..0)}.should_not raise_error
end
it 'should wait for specified number of seconds' do
Kernel.should_receive(:sleep).with(5).exactly(2).times
- ->{attempt(3.times).with_delay(5..5){raise 'Test'}}.should raise_error('Test')
+ lambda{attempt(3.times).with_delay(5..5){raise 'Test'}}.should raise_error('Test')
end
it 'should reject multiple delay policies' do
- ->{attempt(3.times).with_delay(1).with_delay(1)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_delay(1).with_delay(1)}.should raise_error(ArgumentError)
end
end
context 'with_reset' do
it 'should reject nil reset proc' do
- ->{attempt(3.times).with_reset(nil)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_reset(nil)}.should raise_error(ArgumentError)
end
it 'should accept calls without a code block' do
- ->{attempt(3.times).with_reset(->{})}.should_not raise_error
+ lambda{attempt(3.times).with_reset(lambda{})}.should_not raise_error
end
it 'should call the code block' do
was_called = false
- attempt(1.times).with_reset(->{}) { was_called = true }
+ attempt(1.times).with_reset(lambda{}) { was_called = true }
was_called.should be_true
end
it 'should reject multiple reset procs' do
- ->{attempt(3.times).with_reset(->{}).with_reset(->{})}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_reset(lambda{}).with_reset(lambda{})}.should raise_error(ArgumentError)
end
it 'should not be called on successful calls' do
was_called = false
- attempt(1.times).with_reset(->{ was_called = true }) {}
+ attempt(1.times).with_reset(lambda{ was_called = true }) {}
was_called.should be_false
end
it 'should be called on each failure' do
reset_count = 0
- ->{attempt(3.times).with_reset(->{ reset_count += 1 }) { raise 'Test' }}.should raise_error('Test')
+ lambda{attempt(3.times).with_reset(lambda{ reset_count += 1 }) { raise 'Test' }}.should raise_error('Test')
reset_count.should eql(3)
end
end
context 'and_default_to' do
it 'should reject nil default method' do
- ->{attempt(3.times).and_default_to(nil)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).and_default_to(nil)}.should raise_error(ArgumentError)
end
it 'should reject duplicate default methods' do
- ->{attempt(3.times).and_default_to(->{}).and_default_to(->{})}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).and_default_to(lambda{}).and_default_to(lambda{})}.should raise_error(ArgumentError)
end
it 'should allow calls without a code block' do
- ->{attempt(3.times).and_default_to(->{})}.should_not raise_error
+ lambda{attempt(3.times).and_default_to(lambda{})}.should_not raise_error
end
it 'should call the code block' do
was_called = false
- attempt(3.times).and_default_to(->{}){ was_called = true }
+ attempt(3.times).and_default_to(lambda{}){ was_called = true }
was_called.should be_true
end
it 'should not be called on success' do
was_called = false
- attempt(3.times).and_default_to(->{ was_called = true }) {}
+ attempt(3.times).and_default_to(lambda{ was_called = true }) {}
was_called.should be_false
end
it 'should be called once on the failure' do
call_count = 0
- attempt(3.times).and_default_to(->{ call_count += 1 }){ raise 'Test'}
+ attempt(3.times).and_default_to(lambda{ call_count += 1 }){ raise 'Test'}
call_count.should eql(1)
end
it 'should not be called if code block stopped failing' do
call_count = 0
was_called = false
- attempt(3.times).and_default_to(->{ was_called = true }) { call_count += 1; raise 'Test' if call_count < 2 }
+ attempt(3.times).and_default_to(lambda{ was_called = true }) { call_count += 1; raise 'Test' if call_count < 2 }
was_called.should be_false
end
end
context 'with_binary_backoff' do
it 'should reject nil initial delay' do
- ->{attempt(3.times).with_binary_backoff(nil)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_binary_backoff(nil)}.should raise_error(ArgumentError)
end
it 'should reject non-integer initial delay' do
- ->{attempt(3.times).with_binary_backoff('foo')}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_binary_backoff('foo')}.should raise_error(ArgumentError)
end
it 'should reject zero initial delay' do
- ->{attempt(3.times).with_binary_backoff(0)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_binary_backoff(0)}.should raise_error(ArgumentError)
end
it 'should reject negative initial delay' do
- ->{attempt(3.times).with_binary_backoff(-1)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_binary_backoff(-1)}.should raise_error(ArgumentError)
end
it 'should reject multiple policies' do
- ->{attempt(3.times).with_binary_backoff(1).with_binary_backoff(2)}.should raise_error(ArgumentError)
+ lambda{attempt(3.times).with_binary_backoff(1).with_binary_backoff(2)}.should raise_error(ArgumentError)
end
it 'should accept calls without a code block' do
- ->{attempt(3.times).with_binary_backoff(1)}.should_not raise_error
+ lambda{attempt(3.times).with_binary_backoff(1)}.should_not raise_error
end
it 'should call the code block' do
was_called = false
@@ -250,47 +250,47 @@
it 'should double delay on each failure' do
Kernel.should_receive(:sleep).ordered.with(1)
Kernel.should_receive(:sleep).ordered.with(2)
Kernel.should_receive(:sleep).ordered.with(4)
- attempt(4.times).with_binary_backoff(1).and_default_to(->{}) { raise 'Test' }
+ attempt(4.times).with_binary_backoff(1).and_default_to(lambda{}) { raise 'Test' }
end
end
context 'with_filter' do
it 'should reject empty exceptions list' do
- ->{attempt.with_filter}.should raise_error(ArgumentError)
+ lambda{attempt.with_filter}.should raise_error(ArgumentError)
end
it 'should reject non-exceptions' do
- ->{attempt.with_filter(1)}.should raise_error(ArgumentError)
+ lambda{attempt.with_filter(1)}.should raise_error(ArgumentError)
end
it 'should accept calls without a block' do
- ->{attempt(2.times).with_filter(Exception)}.should_not raise_error
+ lambda{attempt(2.times).with_filter(Exception)}.should_not raise_error
end
it 'should call code within the block' do
was_called = false
attempt(2.times).with_filter(Exception){ was_called = true }
was_called.should be_true
end
it 'should ignore other exceptions' do
count = 0
- ->{attempt(3.times).with_filter(StandardError){ count += 1; raise(Exception, 'Test')}}.should raise_error(Exception)
+ lambda{attempt(3.times).with_filter(StandardError){ count += 1; raise(Exception, 'Test')}}.should raise_error(Exception)
count.should eql(1)
end
it 'should not ignore specified exceptions' do
count = 0
- ->{attempt(3.times).with_filter(RuntimeError){ count += 1; raise 'Test'}}.should raise_error(RuntimeError)
+ lambda{attempt(3.times).with_filter(RuntimeError){ count += 1; raise 'Test'}}.should raise_error(RuntimeError)
count.should eql(3)
end
it 'should not ignore derived exceptions' do
count = 0
- ->{attempt(3.times).with_filter(Exception){ count += 1; raise(StandardError, 'Test')}}.should raise_error(StandardError)
+ lambda{attempt(3.times).with_filter(Exception){ count += 1; raise(StandardError, 'Test')}}.should raise_error(StandardError)
count.should eql(3)
end
end
context 'enabled' do