class Spork::Server
(This was originally based off of spec_server.rb from rspec-rails (David Chelimsky), which was based on Florian Weber’s TDDMate)
An abstract class that is implemented to create a server
def self.run(options = {})
def self.run(options = {}) new(options).listen end
def abort
def abort run_strategy.abort end
def initialize(options = {})
def initialize(options = {}) @run_strategy = options[:run_strategy] @port = options[:port] end
def listen
def listen @run_strategy.assert_ready! trap("SIGINT") { sig_int_received } trap("SIGTERM") { abort; exit!(0) } trap("USR2") { abort; restart } if Signal.list.has_key?("USR2") @drb_service = DRb.start_service("druby://127.0.0.1:#{port}", self) Spork.each_run { @drb_service.stop_service } if @run_strategy.class == Spork::RunStrategy::Forking stderr.puts "Spork is ready and listening on #{port}!" stderr.flush DRb.thread.join end
def restart
def restart stderr.puts "restarting" stderr.flush config = ::Config::CONFIG ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT'] command_line = [ruby, $0, ARGV].flatten.join(' ') exec(command_line) end
def run(argv, stderr, stdout)
spec_server.run(options.argv, $stderr, $stdout)
spec_server = DRbObject.new_with_uri("druby://127.0.0.1:8989")
# see http://redmine.ruby-lang.org/issues/show/496 to see why localhost:0 is used.
DRb.start_service("druby://localhost:0") # this allows Ruby to do some magical stuff so you can pass an output stream over DRb.
This is the public facing method that is served up by DRb. To use it from the client side (in a testing framework):
def run(argv, stderr, stdout) puts "Running tests with args #{argv.inspect}..." result = run_strategy.run(argv, stderr, stdout) puts "Done.\n\n" result end
def sig_int_received
def sig_int_received stdout.puts "\n" abort if run_strategy.running? stderr.puts "Running tests stopped. Press CTRL-C again to stop the server." stderr.flush else exit!(0) end end