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 committed Oct 27, 2009
1 parent 93edc15 commit 53562b9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ BERTRPC

By Tom Preston-Werner (tom@mojombo.com)

BERT-RPC client library for Ruby. Makes it ridiculously simple to interface with BERT-RPC servers.
BERT-RPC client library for Ruby. Makes it ridiculously simple to interface
with BERT-RPC servers.

See the full BERT-RPC specification at [bert-rpc.org](http://bert-rpc.org).

Expand All @@ -12,7 +13,9 @@ This library currently only supports the following BERT-RPC features:
* `call` requests
* `cast` requests

BERTRPC was developed for GitHub and is currently in production use performing millions of RPC requests every day. The stability and performance have been exemplary.
BERTRPC was developed for GitHub and is currently in production use performing
millions of RPC requests every day. The stability and performance have been
exemplary.


Installation
Expand Down Expand Up @@ -50,6 +53,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 53562b9

Please sign in to comment.