Move server_is_listening_on, wait_for_listener and kill_process_group to Jasmine module

This commit is contained in:
Aaron Peckham & Christian Williams 2009-08-10 13:49:59 -07:00
parent 6e9cd25150
commit 22226f3423
2 changed files with 31 additions and 31 deletions

View File

@ -19,6 +19,30 @@ module Jasmine
port
end
def self.server_is_listening_on(hostname, port)
require 'socket'
begin
socket = TCPSocket.open(hostname, port)
rescue Errno::ECONNREFUSED
return false
end
socket.close
true
end
def self.wait_for_listener(port, name = "required process", seconds_to_wait = 10)
time_out_at = Time.now + seconds_to_wait
until server_is_listening_on "localhost", port
sleep 0.1
puts "Waiting for #{name} on #{port}..."
raise "#{name} didn't show up on port #{port} after #{seconds_to_wait} seconds." if Time.now > time_out_at
end
end
def self.kill_process_group(process_group_id, signal="TERM")
Process.kill signal, -process_group_id # negative pid means kill process group. (see man 2 kill)
end
class RunAdapter
def initialize(spec_files_or_proc)
@spec_files_or_proc = spec_files_or_proc
@ -118,26 +142,6 @@ module Jasmine
stop_servers
end
def server_is_listening_on(hostname, port)
require 'socket'
begin
socket = TCPSocket.open(hostname, port)
rescue Errno::ECONNREFUSED
return false
end
socket.close
true
end
def wait_for_listener(port, name = "required process", seconds_to_wait = 10)
seconds_waited = 0
until server_is_listening_on "localhost", port
sleep 1
puts "Waiting for #{name} on #{port}..."
raise "#{name} didn't show up on port #{port} after #{seconds_to_wait} seconds." if (seconds_waited += 1) >= seconds_to_wait
end
end
def start_servers
@jasmine_server_port = Jasmine::find_unused_port
@selenium_server_port = Jasmine::find_unused_port
@ -155,18 +159,14 @@ module Jasmine
end
puts "jasmine server started. pid is #{@jasmine_server_pid}"
wait_for_listener(@selenium_server_port, "selenium server")
wait_for_listener(@jasmine_server_port, "jasmine server")
end
def kill_process_group(process_group_id, signal="TERM")
Process.kill signal, -process_group_id # negative pid means kill process group. (see man 2 kill)
Jasmine::wait_for_listener(@selenium_server_port, "selenium server")
Jasmine::wait_for_listener(@jasmine_server_port, "jasmine server")
end
def stop_servers
puts "shutting down the servers..."
kill_process_group(@selenium_pid) if @selenium_pid
kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
Jasmine::kill_process_group(@selenium_pid) if @selenium_pid
Jasmine::kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
end
def run

View File

@ -8,7 +8,7 @@ describe Jasmine::SimpleServer do
end
after do
Process.kill "TERM", -@jasmine_server_pid if @jasmine_server_pid
Jasmine::kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
end
it "should start and print script tags" do
@ -18,7 +18,7 @@ describe Jasmine::SimpleServer do
exit! 0
end
Jasmine::Runner.new(nil, nil, nil).wait_for_listener(@port)
Jasmine::wait_for_listener(@port)
run_html = open("http://localhost:#{@port}/run.html").read
run_html.should =~ /<script src="file1"/
@ -37,7 +37,7 @@ describe Jasmine::SimpleServer do
exit! 0
end
Jasmine::Runner.new(nil, nil, nil).wait_for_listener(@port)
Jasmine::wait_for_listener(@port)
run_html = open("http://localhost:#{@port}/run.html").read
run_html.should =~ /<script src="file1"/