Skip to content

Commit

Permalink
Merge pull request oesmith#239 from AlanFoster/add-after-suite-stop-h…
Browse files Browse the repository at this point in the history
…ook-for-event-machine

Add after suite stop hook for event machine
  • Loading branch information
ronwsmith authored Apr 30, 2018
2 parents 369302c + 79fc22c commit f9740f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/billy/init/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def proxy
RSpec.configure do |config|
config.include(Billy::RspecHelper)

config.prepend_after(:each) do
config.append_after(:each) do
proxy.reset
end

config.after(:suite) do
Billy.proxy.stop
end
end
26 changes: 26 additions & 0 deletions lib/billy/proxy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'cgi'
require 'eventmachine'
require 'timeout'

module Billy
class Proxy
Expand All @@ -22,6 +23,14 @@ def start(threaded = true)
end
end

def stop
return if @signature.nil?

server_port = port
EM.stop
wait_for_server_shutdown! server_port
end

def url
"http://#{host}:#{port}"
end
Expand All @@ -40,6 +49,23 @@ def cache

protected

def wait_for_server_shutdown!(server_port)
Timeout::timeout(60) do
sleep(0.01) while port_in_use? server_port
end
rescue Timeout::Error
Billy.log(:error, "puffing-billy: Event machine not shutdown correctly on port #{port}")
end

def port_in_use?(port)
s = TCPSocket.new(host, port)
s.close
Billy.log(:info, "puffing-billy: Waiting for event machine to shutdown on port #{port}")
s
rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL
false
end

def main_loop
EM.run do
EM.error_handler do |e|
Expand Down

0 comments on commit f9740f0

Please sign in to comment.