From 22226f3423a9becd6eb42ddfb8e22a7996555bdb Mon Sep 17 00:00:00 2001 From: Aaron Peckham & Christian Williams Date: Mon, 10 Aug 2009 13:49:59 -0700 Subject: [PATCH] Move server_is_listening_on, wait_for_listener and kill_process_group to Jasmine module --- contrib/ruby/jasmine_runner.rb | 56 ++++++++++++++--------------- contrib/ruby/jasmine_runner_spec.rb | 6 ++-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/contrib/ruby/jasmine_runner.rb b/contrib/ruby/jasmine_runner.rb index 857bec0..0f0eb4f 100644 --- a/contrib/ruby/jasmine_runner.rb +++ b/contrib/ruby/jasmine_runner.rb @@ -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 diff --git a/contrib/ruby/jasmine_runner_spec.rb b/contrib/ruby/jasmine_runner_spec.rb index 5a287ee..83999de 100644 --- a/contrib/ruby/jasmine_runner_spec.rb +++ b/contrib/ruby/jasmine_runner_spec.rb @@ -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 =~ /