Skip to content

Commit

Permalink
make socket read timeout raise a BERTRPC::ReadTimeoutError and enhanc…
Browse files Browse the repository at this point in the history
…e readme
  • Loading branch information
mojombo authored and defunkt committed Nov 26, 2009
1 parent b692401 commit 51db9f7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -56,6 +56,18 @@ The underlying BERT-RPC transaction of the above cast is:
<- {noreply}


Documentation
-------------

Creating a service:

# No timeout
svc = BERTRPC::Service.new('localhost', 9999)

# 10s socket read timeout, raises BERTRPC::ReadTimeoutError
svc = BERTRPC::Service.new('localhost', 9999, 10)


Copyright
---------

Expand Down
4 changes: 3 additions & 1 deletion lib/bertrpc/action.rb
Expand Up @@ -42,8 +42,10 @@ def transaction(bert_request)
raise ProtocolError.new(ProtocolError::NO_DATA) unless bert_response
sock.close
bert_response
rescue Errno::ECONNREFUSED, Timeout::Error
rescue Errno::ECONNREFUSED
raise ConnectionError.new("Unable to connect to #{@svc.host}:#{@svc.port}")
rescue Timeout::Error
raise ReadTimeoutError.new("No response from #{@svc.host}:#{@svc.port} in #{@svc.timeout}s")
end

# Creates a socket object which does speedy, non-blocking reads
Expand Down
4 changes: 4 additions & 0 deletions lib/bertrpc/errors.rb
Expand Up @@ -28,6 +28,10 @@ class ConnectionError < BERTRPCError

end

class ReadTimeoutError < BERTRPCError

end

class ProtocolError < BERTRPCError
NO_HEADER = [0, "Unable to read length header from server."]
NO_DATA = [1, "Unable to read data from server."]
Expand Down
14 changes: 14 additions & 0 deletions test/action_test.rb
Expand Up @@ -89,6 +89,20 @@ class ActionTest < Test::Unit::TestCase
assert_equal 1, e.code
end
end

should "raise a ReadTimeoutError when the connection times out" do
io = stub()
io.expects(:write).with("\000\000\000\003")
io.expects(:write).with("foo")
io.expects(:read).with(4).raises(Timeout::Error)
@call.expects(:connect_to).returns(io)
begin
@call.transaction("foo")
fail "Should have thrown an error"
rescue BERTRPC::ReadTimeoutError => e
assert_equal 0, e.code
end
end
end
end
end
7 changes: 6 additions & 1 deletion test/service_test.rb
Expand Up @@ -2,10 +2,15 @@

class ServiceTest < Test::Unit::TestCase
context "A Service" do
should "be created with host and port" do
should "be creatable with host and port" do
svc = BERTRPC::Service.new('localhost', 9941)
assert svc.is_a?(BERTRPC::Service)
end

should "be creatable with host, port, and timeout" do
svc = BERTRPC::Service.new('localhost', 9941, 5)
assert svc.is_a?(BERTRPC::Service)
end
end

context "A Service Instance's" do
Expand Down

0 comments on commit 51db9f7

Please sign in to comment.