Created
August 9, 2013 19:25
-
-
Save jnunemaker/6196418 to your computer and use it in GitHub Desktop.
Kestrel blocking client with signal checks for the kestrel-client gem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Kestrel | |
class Client | |
class BlockingWithCheck < Blocking | |
attr_accessor :return_check | |
def get(*args) | |
count = 0 | |
while count += 1 | |
if response = client.get(*args) | |
return response | |
end | |
sleep_for_count(count) | |
if @return_check | |
return if @return_check.call | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'kestrel/client/blocking_with_check' | |
describe Kestrel::Client::BlockingWithCheck do | |
let(:client) { Kestrel::Client.new } | |
subject { | |
described_class.new(client) | |
} | |
before do | |
client.stub(:get => nil) | |
end | |
it "blocks if return check is false" do | |
subject.return_check = proc { false } | |
client.should_receive(:get).with(:foo).exactly(3).times.and_return(nil, nil, 'woot') | |
Timeout.timeout(0.05) do | |
subject.get(:foo).should eq('woot') | |
end | |
end | |
it "returns if check is true" do | |
subject.return_check = proc { true } | |
Timeout.timeout(0.05) do | |
subject.get(:foo) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment