From 47b8d10d5659102b30b74cdf2dae3b80bd54e020 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 17 Jan 2012 13:32:57 -0500 Subject: [PATCH] server runner updated --- config/cucumber.yml | 2 + ext/jasmine-webkit-specrunner/Runner.h | 2 + .../jasmine-webkit-specrunner.pro | 19 --- lib/jasmine/headless/runner.rb | 123 +++++++++++------- spec/lib/jasmine/headless/runner_spec.rb | 22 +++- 5 files changed, 103 insertions(+), 65 deletions(-) create mode 100644 config/cucumber.yml delete mode 100644 ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro diff --git a/config/cucumber.yml b/config/cucumber.yml new file mode 100644 index 0000000..a061551 --- /dev/null +++ b/config/cucumber.yml @@ -0,0 +1,2 @@ +default: -r features + diff --git a/ext/jasmine-webkit-specrunner/Runner.h b/ext/jasmine-webkit-specrunner/Runner.h index 8d7269e..3f1f01b 100644 --- a/ext/jasmine-webkit-specrunner/Runner.h +++ b/ext/jasmine-webkit-specrunner/Runner.h @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include "Page.h" diff --git a/ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro b/ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro deleted file mode 100644 index e6710d1..0000000 --- a/ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro +++ /dev/null @@ -1,19 +0,0 @@ -###################################################################### -# Automatically generated by qmake (2.01a) Tue Aug 2 10:37:48 2011 -###################################################################### - -TEMPLATE = app -TARGET = -DEPENDPATH += . HeadlessSpecRunner Test -INCLUDEPATH += . HeadlessSpecRunner Test - -# Input -HEADERS += HeadlessSpecRunner/ConsoleOutput.h \ - HeadlessSpecRunner/Page.h \ - HeadlessSpecRunner/Runner.h \ - Test/Page_test.h -SOURCES += specrunner.cpp \ - HeadlessSpecRunner/ConsoleOutput.cpp \ - HeadlessSpecRunner/Page.cpp \ - HeadlessSpecRunner/Runner.cpp \ - Test/Page_test.cpp diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index b8413e0..964aef6 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -25,9 +25,81 @@ module Jasmine attr_reader :options - class << self - def run(options = {}) - new(options).run + def self.run(options = {}) + new(options).run + end + + def self.server_port + return @server_port if @server_port + + count = 100 + begin + port = select_server_port + + socket = TCPSocket.new(server_interface, port) + socket.close + + count -= 1 + + raise "Could not create server port after 100 attempts!" if count == 0 + rescue Errno::ECONNREFUSED + @server_port = port + + break + ensure + begin + socket.close if socket + rescue IOError + end + end while true + + @server_port + end + + def self.select_server_port + 21000 + rand(10000) + end + + def self.server_interface + '127.0.0.1' + end + + def self.server_uri + "http://#{server_interface}:#{server_port}" + end + + def self.ensure_server + return if @server + + require 'webrick' + require 'thread' + require 'rack' + require 'net/http' + + port = server_port + + @server = Thread.new do + Jasmine::Headless.warn "Powering up!" + + Rack::Handler::WEBrick.run( + Rack::File.new('/'), + :Port => port, + :Logger => Logger.new(StringIO.new), + :AccessLog => [ + [ StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT ], + [ StringIO.new, WEBrick::AccessLog::REFERER_LOG_FORMAT ] + ] + ) + end + + while true do + begin + Net::HTTP.get(URI(server_uri)) + break + rescue Errno::ECONNREFUSED => e + end + + sleep 0.1 end end @@ -65,7 +137,7 @@ module Jasmine command += targets.flatten.collect do |target| target = File.expand_path(target) if options[:use_server] - target = server_uri + target + target = self.class.server_uri + target else target = "file://" + target end @@ -125,53 +197,14 @@ module Jasmine end def wrap_in_server - require 'webrick' - require 'thread' - require 'rack' - require 'net/http' + self.class.ensure_server - server = Thread.new do - Rack::Handler::WEBrick.run( - Rack::File.new('/'), - :Port => server_port, - :Logger => Logger.new(StringIO.new), - :AccessLog => [ - [ StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT ], - [ StringIO.new, WEBrick::AccessLog::REFERER_LOG_FORMAT ] - ] - ) - end - - while true do - begin - Net::HTTP.get(URI(server_uri)) - break - rescue Errno::ECONNREFUSED => e - end - - sleep 0.1 - end - - Jasmine::Headless.warn "HTTP powered specs!" + Jasmine::Headless.warn "HTTP powered specs! Located at #{self.class.server_uri}" yield - - Thread.kill(server) end private - def server_port - @server_port ||= 21000 + rand(1000) - end - - def server_interface - '127.0.0.1' - end - - def server_uri - "http://#{server_interface}:#{server_port}" - end - def jasmine_config_data raise JasmineConfigNotFound.new("Jasmine config not found. I tried #{@options[:jasmine_config]}.") if !File.file?(@options[:jasmine_config]) diff --git a/spec/lib/jasmine/headless/runner_spec.rb b/spec/lib/jasmine/headless/runner_spec.rb index 1def973..f42c4d7 100644 --- a/spec/lib/jasmine/headless/runner_spec.rb +++ b/spec/lib/jasmine/headless/runner_spec.rb @@ -107,7 +107,7 @@ describe Jasmine::Headless::Runner do before do options[:use_server] = true - runner.stubs(:server_uri).returns(server_uri) + described_class.stubs(:server_uri).returns(server_uri) end it { should include(server_uri + File.expand_path(target)) } @@ -206,4 +206,24 @@ describe Jasmine::Headless::Runner do subject.options[:reporters].should == reporters end end + + describe '.server_port' do + before do + described_class.instance_variable_set(:@server_port, nil) + end + + context 'port in use' do + require 'socket' + + before do + described_class.stubs(:select_server_port).returns(5000, 5001) + end + + it 'should try another port' do + server = TCPServer.new(described_class.server_interface, 5000) + + described_class.server_port.should == 5001 + end + end + end end