Skip to content

Commit

Permalink
catch connection exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Bradbury committed Nov 4, 2010
1 parent 2b467f9 commit 245a8f6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.11
0.1.12
36 changes: 21 additions & 15 deletions lib/dkbrpc/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def handshake(buffer)
end

class Connection
attr_reader :remote_connection
attr_reader :msg_id_generator
attr_reader :remote_connection
attr_reader :msg_id_generator

def initialize(host, port, api, insecure_methods=Default::INSECURE_METHODS)
@host = host
Expand All @@ -119,29 +119,35 @@ def initialize(host, port, api, insecure_methods=Default::INSECURE_METHODS)
@insecure_methods = insecure_methods
end

def errback &block
def errback & block
@errbacks << block if block
end

def start &block
def start & block
EventMachine::schedule do
EventMachine::connect(@host, @port, OutgoingHandler) do |connection|
connection.host = @host
connection.port = @port
connection.on_connection = block
connection.api = @api
connection.errbacks = @errbacks
connection.msg_id_generator = @msg_id_generator
connection.insecure_methods = @insecure_methods
@remote_connection = connection
begin
EventMachine::connect(@host, @port, OutgoingHandler) do |connection|
connection.host = @host
connection.port = @port
connection.on_connection = block
connection.api = @api
connection.errbacks = @errbacks
connection.msg_id_generator = @msg_id_generator
connection.insecure_methods = @insecure_methods
@remote_connection = connection
end
rescue Exception => e
@errbacks.each do |errback|
errback.call(e)
end
end
end
end

def method_missing(method, *args, &block)
def method_missing(method, * args, & block)
EventMachine::schedule do
begin
@remote_connection.remote_call(method, args, &block)
@remote_connection.remote_call(method, args, & block)
rescue Exception => e
@remote_connection.call_errbacks(e)
end
Expand Down
14 changes: 14 additions & 0 deletions spec/dkbrpc/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ def connect(connection)
EM.reactor_running?.should == true
end

it "should catch exceptions from connect" do
connection = Dkbrpc::Connection.new("127.0.0.1", 9441, mock("client"))
EventMachine.stub!(:connect).and_raise("Internal Java error")
errback_called = false
connection.errback do |e|
e.message.should == "Internal Java error"
errback_called = true
end
connection.start

wait_for{errback_called}

end

end

end
Expand Down
44 changes: 31 additions & 13 deletions spec/dkbrpc/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def set_id(id)
class OtherProtocol

attr_reader :buffer, :connection

def handle_connection(buffer, connection)
@buffer = buffer
@connection = connection
Expand All @@ -64,7 +65,7 @@ def handle_connection(buffer, connection)
before(:each) do
@reactor_thread = nil
end

after(:each) do
stop_reactor(@reactor_thread) if @reactor_thread
end
Expand Down Expand Up @@ -93,37 +94,34 @@ def handle_connection(buffer, connection)
server = Server.new("host", "port", "api", [:==, :===, :=~])
server.insecure_methods.should == [:==, :===, :=~]
end

def connect
@reactor_thread = start_reactor
connected = false
@server = Dkbrpc::Server.new("127.0.0.1", 9441, mock("server"))
@connection = Dkbrpc::Connection.new("127.0.0.1", 9441, mock("client"))
EM.schedule do
@server.start { |client| @client = client }
@connection.start {connected = true}
@server.start { |client| @client = client }
@connection.start { connected = true }
end
wait_for{connected}
wait_for { connected }
end

it "sets instance of Dkbrpc::Id to each connection for connection ids" do
connect
generator_class = @server.connections.first.conn_id_generator.class
# stop_reactor(@reactor_thread)
generator_class.should == Dkbrpc::Id
end

it "sets instance of Dkbrpc::Id to each connection for message ids" do
connect
generator_class = @server.connections.first.msg_id_generator.class
# stop_reactor(@reactor_thread)
generator_class.should == Dkbrpc::Id
end

it "sets instance of insecure_methods on each connection" do
connect
insecure_methods = @server.connections.first.insecure_methods
# stop_reactor(@reactor_thread)
insecure_methods.should == Dkbrpc::Default::INSECURE_METHODS
end

Expand Down Expand Up @@ -156,27 +154,47 @@ def connect
connected = true
end
end
wait_for{connected}
wait_for { connected }
end

it "catches exceptions that occur during a remote call to client" do
connect
error = nil
@client.remote_connection.errbacks << lambda { |e| error = e }
@client.remote_connection.errbacks << proc { |e| error = e }
@client.remote_connection.should_receive(:remote_call).and_raise("Blah")

@client.foo
wait_for { error != nil }

error.to_s.should == "Blah"
EM.reactor_running?.should == true
end

it "should catch exceptions in starting server" do
EventMachine.stub!(:start_server).and_raise("EMReactor Exception")

@reactor_thread = start_reactor
done = false

@server = Dkbrpc::Server.new("127.0.0.1", 9441, TestApi.new)
@server.errback do |error|
error.message.should == "EMReactor Exception"
done = true
end

@server.start

wait_for { done }
done.should == true

end
end

class TestApi
def get_one(responder)
EM.add_timer(0.5) { responder.reply(1) }
end

def get_two(responder)
responder.reply(2)
end
Expand Down

0 comments on commit 245a8f6

Please sign in to comment.