Skip to content

Commit

Permalink
use next_tick workaround when closing connections evereywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Bradbury committed Nov 2, 2010
1 parent 18c9e5b commit be35f18
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1.9
0.1.10
6 changes: 4 additions & 2 deletions lib/dkbrpc/connection.rb
Expand Up @@ -147,8 +147,10 @@ def method_missing(method, *args, &block)
def stop(& callback)
EventMachine::schedule do
if @remote_connection
@remote_connection.close_connection
callback.call(true) if callback
EventMachine::next_tick do
@remote_connection.close_connection
callback.call(true) if callback
end
else
callback.call(false) if callback
end
Expand Down
44 changes: 31 additions & 13 deletions lib/dkbrpc/server.rb
Expand Up @@ -12,13 +12,13 @@ def initialize(outgoing_connection)
@remote_connection = outgoing_connection
end

def method_missing(method, *args, &block)
def method_missing(method, * args, & block)
EventMachine::schedule do
@remote_connection.remote_call(method, args, &block)
@remote_connection.remote_call(method, args, & block)
end
end

def errback(&block)
def errback(& block)
@remote_connection.errbacks << block if block
end

Expand Down Expand Up @@ -48,7 +48,7 @@ def initialize(host, port, api, insecure_methods=Default::INSECURE_METHODS)
@insecure_methods = insecure_methods
end

def start(&callback)
def start(& callback)
EventMachine::schedule do
begin
@server_signature = EventMachine::start_server(@host, @port, Listener) do |connection|
Expand All @@ -68,26 +68,44 @@ def start(&callback)
end
end

def stop

def stop(& callback)
EventMachine::schedule do
@connections.each do |connection|
connection.close_connection
EventMachine::next_tick do
close_all_connections
stop_server(callback)
end
EventMachine::stop_server(@server_signature)
end if @server_signature
@server_signature = nil
end
end
def errback(&block)

def errback(& block)
@errback = block
end

private #################################################################################
def close_all_connections
@connections.each do |connection|
connection.close_connection
end
end

def stop_server(callback)
if @server_signature
EventMachine::stop_server(@server_signature)
@server_signature = nil
callback.call(true) if callback
else
callback.call(false) if callback
end
end

end

module Listener
INCOMING_CONNECTION = "4"[0]
OUTGOING_CONNECTION = "5"[0]
attr_accessor :conn_id_generator
attr_reader :conn_id
attr_reader :conn_id
attr_accessor :msg_id_generator
attr_accessor :api
attr_accessor :new_connection_callback
Expand Down
11 changes: 9 additions & 2 deletions spec/dkbrpc/server_failure_spec.rb
Expand Up @@ -194,8 +194,15 @@ def run_server_failure(blocks)
@server.start
@connection1.start {connected = true}
wait_for{connected}
@server.stop
@server.stop
stopped = []
@server.stop do |result|
stopped << result
end
@server.stop do |result|
stopped << result
end
wait_for{stopped.size == 2}
stopped.should == [true, false]
stop_reactor(thread)
end

Expand Down

0 comments on commit be35f18

Please sign in to comment.