Skip to content

Commit

Permalink
raise exception when retry limit is exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
Ines Sombra committed Jan 13, 2012
1 parent 56e7a98 commit 8ab974d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
12 changes: 10 additions & 2 deletions lib/rack-idempotent.rb
Expand Up @@ -2,6 +2,10 @@

module Rack
class Idempotent
RETRY_LIMIT = 5

class RetryLimitExceeded < Exception; end

def initialize(app)
@app= app
end
Expand All @@ -11,8 +15,12 @@ def call(env)
begin
@app.call(env)
rescue Errno::ETIMEDOUT
env['client.retries'] += 1
retry
if env['client.retries'] > RETRY_LIMIT - 1
raise(RetryLimitExceeded)
else
env['client.retries'] += 1
retry
end
end
end
end
Expand Down
31 changes: 25 additions & 6 deletions spec/rack-idempotent_spec.rb
Expand Up @@ -5,21 +5,40 @@ class CaptureEnv
class << self; attr_accessor :env; end
def initialize(app); @app=app; end
def call(env)
tuple = @app.call(env)
@app.call(env)
ensure
self.class.env = env
tuple
end
end
class RaiseUp
class << self; attr_accessor :errors; end
def self.call(env); self.errors.shift.tap{|e| raise(e) if e}; [200, {}, []]; end
end
before(:each){ CaptureEnv.env = nil }
it "should retry Errno::ETIMEDOUT" do
$to_raise = [Errno::ETIMEDOUT, Errno::ETIMEDOUT]

RaiseUp.errors = [Errno::ETIMEDOUT, Errno::ETIMEDOUT]
client = Rack::Client.new do
use CaptureEnv
use Rack::Idempotent
run lambda{|env| $to_raise.shift.tap{|e| raise(e) if e}; [200, {}, []]}
run RaiseUp
end
response = client.get("/doesntmatter")

client.get("/doesntmatter")

env = CaptureEnv.env
env['client.retries'].should == 2
end
it "should raise Rack::Idempotent::RetryLimitExceeded when retry limit is reached" do
RaiseUp.errors = (Rack::Idempotent::RETRY_LIMIT + 1).times.map{|i| Errno::ETIMEDOUT}
client = Rack::Client.new do
use CaptureEnv
use Rack::Idempotent
run RaiseUp
end

lambda { client.get("/doesntmatter") }.should raise_exception(Rack::Idempotent::RetryLimitExceeded)

env = CaptureEnv.env
env['client.retries'].should == Rack::Idempotent::RETRY_LIMIT
end
end

0 comments on commit 8ab974d

Please sign in to comment.