diff --git a/README.md b/README.md index 16ad973..114626f 100644 --- a/README.md +++ b/README.md @@ -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 --------- diff --git a/lib/bertrpc/action.rb b/lib/bertrpc/action.rb index 8efd5d6..9762cef 100644 --- a/lib/bertrpc/action.rb +++ b/lib/bertrpc/action.rb @@ -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 diff --git a/lib/bertrpc/errors.rb b/lib/bertrpc/errors.rb index 3ed437e..a074b6c 100644 --- a/lib/bertrpc/errors.rb +++ b/lib/bertrpc/errors.rb @@ -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."] diff --git a/test/action_test.rb b/test/action_test.rb index 8706648..4aabe11 100644 --- a/test/action_test.rb +++ b/test/action_test.rb @@ -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 diff --git a/test/service_test.rb b/test/service_test.rb index ea5e994..614e306 100644 --- a/test/service_test.rb +++ b/test/service_test.rb @@ -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