Move server_is_listening_on, wait_for_listener and kill_process_group to Jasmine module
This commit is contained in:
parent
6e9cd25150
commit
22226f3423
@ -19,6 +19,30 @@ module Jasmine
|
|||||||
port
|
port
|
||||||
end
|
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
|
class RunAdapter
|
||||||
def initialize(spec_files_or_proc)
|
def initialize(spec_files_or_proc)
|
||||||
@spec_files_or_proc = spec_files_or_proc
|
@spec_files_or_proc = spec_files_or_proc
|
||||||
@ -118,26 +142,6 @@ module Jasmine
|
|||||||
stop_servers
|
stop_servers
|
||||||
end
|
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
|
def start_servers
|
||||||
@jasmine_server_port = Jasmine::find_unused_port
|
@jasmine_server_port = Jasmine::find_unused_port
|
||||||
@selenium_server_port = Jasmine::find_unused_port
|
@selenium_server_port = Jasmine::find_unused_port
|
||||||
@ -155,18 +159,14 @@ module Jasmine
|
|||||||
end
|
end
|
||||||
puts "jasmine server started. pid is #{@jasmine_server_pid}"
|
puts "jasmine server started. pid is #{@jasmine_server_pid}"
|
||||||
|
|
||||||
wait_for_listener(@selenium_server_port, "selenium server")
|
Jasmine::wait_for_listener(@selenium_server_port, "selenium server")
|
||||||
wait_for_listener(@jasmine_server_port, "jasmine server")
|
Jasmine::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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop_servers
|
def stop_servers
|
||||||
puts "shutting down the servers..."
|
puts "shutting down the servers..."
|
||||||
kill_process_group(@selenium_pid) if @selenium_pid
|
Jasmine::kill_process_group(@selenium_pid) if @selenium_pid
|
||||||
kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
|
Jasmine::kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
@ -8,7 +8,7 @@ describe Jasmine::SimpleServer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
Process.kill "TERM", -@jasmine_server_pid if @jasmine_server_pid
|
Jasmine::kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should start and print script tags" do
|
it "should start and print script tags" do
|
||||||
@ -18,7 +18,7 @@ describe Jasmine::SimpleServer do
|
|||||||
exit! 0
|
exit! 0
|
||||||
end
|
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 = open("http://localhost:#{@port}/run.html").read
|
||||||
run_html.should =~ /<script src="file1"/
|
run_html.should =~ /<script src="file1"/
|
||||||
@ -37,7 +37,7 @@ describe Jasmine::SimpleServer do
|
|||||||
exit! 0
|
exit! 0
|
||||||
end
|
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 = open("http://localhost:#{@port}/run.html").read
|
||||||
run_html.should =~ /<script src="file1"/
|
run_html.should =~ /<script src="file1"/
|
||||||
|
Loading…
Reference in New Issue
Block a user