Skip to content

Commit

Permalink
updated the specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Salazar committed Jul 3, 2014
1 parent eeacd4c commit 06ccb84
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions spec/retrier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
DefRetry::Retrier.new({
on: Exception,
sleep: :invalid
}, -> {})
})
}.to raise_error ArgumentError
end
end
Expand All @@ -32,46 +32,56 @@
the_block = -> { :ran }
retrier = DefRetry::Retrier.new({
on: Exception
}, the_block)
})

expect(retrier.run).to be :ran
expect(retrier.run(&the_block)).to be :ran
end

it 'retries on exception :limit times' do
it 'retries on exception :tries times' do
retrier = DefRetry::Retrier.new({
on: Exception,
tries: 2
}, block_exception)
tries: 2,
re_raise: false
})

retrier.run
retrier.run &block_exception
expect(@retries).to be 2
end

it 'reraises the exception after done retrying' do
retrier = DefRetry::Retrier.new({
on: Exception
})

expect { retrier.run &block_exception }.to raise_error Exception
end

it 'raises unspecified exceptions' do
retrier = DefRetry::Retrier.new({
on: ArgumentError
}, block_exception)
})

expect { retrier.run }.to raise_error Exception
expect { retrier.run &block_exception }.to raise_error Exception
end

it 'runs an on_retry callback' do
retrier = DefRetry::Retrier.new({
on: Exception,
on_retry: ->(e, n) { @did_retry = :yes }
}, block_exception)
on_retry: ->(e, n) { @did_retry = :yes },
re_raise: false
})

retrier.run
retrier.run &block_exception
expect(@did_retry).to be :yes
end

it 'runs an on_ensure callback' do
retrier = DefRetry::Retrier.new({
on: Exception,
on_ensure: ->(r, n) { @did_ensure = :yes }
}, -> {})
})

retrier.run
retrier.run {}
expect(@did_ensure).to be :yes
end

Expand All @@ -81,10 +91,11 @@
on_retry: ->(e, r) {
@exception = e
@retry_count = r
}
}, block_exception)
},
re_raise: false
})

retrier.run
retrier.run &block_exception
expect(@exception).to be_kind_of Exception
expect(@retry_count).to be 3 # default limit
end
Expand All @@ -96,9 +107,9 @@
@value = v
@retry_count = r
}
}, -> { :ran })
})

retrier.run
retrier.run { :ran }
expect(@value).to be :ran
expect(@retry_count).to be 0 # default limit
end
Expand All @@ -111,11 +122,12 @@
sleep: :constant,
# the 1st retry will sleep for 1 second
# the 2nd retry will sleep for 1 second
tries: 2
}, block_exception)
tries: 2,
re_raise: false
})

start_time = Time.now.to_i
retrier.run
retrier.run &block_exception
end_time = Time.now.to_i

expect(end_time - start_time).to be 2
Expand All @@ -129,11 +141,12 @@
sleep: :linear,
# the 1st retry will sleep for 1 second
# the 2nd retry will sleep for 2 seconds, and so on
tries: 2
}, block_exception)
tries: 2,
re_raise: false
})

start_time = Time.now.to_i
retrier.run
retrier.run &block_exception
end_time = Time.now.to_i

expect(end_time - start_time).to be 3
Expand All @@ -147,11 +160,12 @@
sleep: :exponential,
# the 1st retry will sleep for 1**2 == 1 second
# the 2nd retry will sleep for 2**2 == 4 seconds
tries: 2
}, block_exception)
tries: 2,
re_raise: false
})

start_time = Time.now.to_i
retrier.run
retrier.run &block_exception
end_time = Time.now.to_i

expect(end_time - start_time).to be 5
Expand Down

0 comments on commit 06ccb84

Please sign in to comment.