2010-04-26 11:25:07 -04:00
|
|
|
require 'rbconfig'
|
2013-01-29 05:44:23 -05:00
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
module ActiveSupport
|
|
|
|
module Testing
|
|
|
|
module Isolation
|
2012-06-19 20:08:15 -04:00
|
|
|
require 'thread'
|
|
|
|
|
2012-06-22 17:29:59 -04:00
|
|
|
def self.included(klass) #:nodoc:
|
2013-12-10 16:09:17 -05:00
|
|
|
klass.class_eval do
|
|
|
|
parallelize_me!
|
|
|
|
end
|
2012-06-19 20:08:15 -04:00
|
|
|
end
|
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
def self.forking_env?
|
2010-08-23 22:09:35 -04:00
|
|
|
!ENV["NO_FORK"] && ((RbConfig::CONFIG['host_os'] !~ /mswin|mingw/) && (RUBY_PLATFORM !~ /java/))
|
2009-06-30 15:00:50 -04:00
|
|
|
end
|
|
|
|
|
2013-01-18 11:07:07 -05:00
|
|
|
@@class_setup_mutex = Mutex.new
|
|
|
|
|
2010-06-15 16:28:19 -04:00
|
|
|
def _run_class_setup # class setup method should only happen in parent
|
2013-01-18 11:07:07 -05:00
|
|
|
@@class_setup_mutex.synchronize do
|
|
|
|
unless defined?(@@ran_class_setup) || ENV['ISOLATION_TEST']
|
|
|
|
self.class.setup if self.class.respond_to?(:setup)
|
|
|
|
@@ran_class_setup = true
|
|
|
|
end
|
2010-06-15 16:28:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-06 20:38:45 -04:00
|
|
|
def run
|
|
|
|
serialized = run_in_isolation do
|
|
|
|
super
|
2009-12-30 23:48:15 -05:00
|
|
|
end
|
2012-01-24 15:25:51 -05:00
|
|
|
|
2013-05-06 20:38:45 -04:00
|
|
|
Marshal.load(serialized)
|
2009-06-30 20:26:46 -04:00
|
|
|
end
|
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
module Forking
|
|
|
|
def run_in_isolation(&blk)
|
|
|
|
read, write = IO.pipe
|
2014-01-17 16:49:10 -05:00
|
|
|
read.binmode
|
|
|
|
write.binmode
|
2009-06-30 20:26:46 -04:00
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
pid = fork do
|
|
|
|
read.close
|
2013-05-06 20:38:45 -04:00
|
|
|
yield
|
|
|
|
write.puts [Marshal.dump(self.dup)].pack("m")
|
2009-07-06 15:25:34 -04:00
|
|
|
exit!
|
|
|
|
end
|
2009-06-30 20:26:46 -04:00
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
write.close
|
|
|
|
result = read.read
|
|
|
|
Process.wait2(pid)
|
2009-09-29 19:07:29 -04:00
|
|
|
return result.unpack("m")[0]
|
2009-07-06 15:25:34 -04:00
|
|
|
end
|
|
|
|
end
|
2009-06-30 20:26:46 -04:00
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
module Subprocess
|
2010-06-15 16:28:19 -04:00
|
|
|
ORIG_ARGV = ARGV.dup unless defined?(ORIG_ARGV)
|
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
# Crazy H4X to get this working in windows / jruby with
|
|
|
|
# no forking.
|
|
|
|
def run_in_isolation(&blk)
|
|
|
|
require "tempfile"
|
|
|
|
|
|
|
|
if ENV["ISOLATION_TEST"]
|
2013-08-01 23:35:27 -04:00
|
|
|
yield
|
2009-07-06 15:25:34 -04:00
|
|
|
File.open(ENV["ISOLATION_OUTPUT"], "w") do |file|
|
2013-08-01 23:35:27 -04:00
|
|
|
file.puts [Marshal.dump(self.dup)].pack("m")
|
2009-07-06 15:25:34 -04:00
|
|
|
end
|
|
|
|
exit!
|
|
|
|
else
|
|
|
|
Tempfile.open("isolation") do |tmpfile|
|
2014-06-09 12:13:47 -04:00
|
|
|
env = {
|
|
|
|
ISOLATION_TEST: self.class.name,
|
|
|
|
ISOLATION_OUTPUT: tmpfile.path
|
|
|
|
}
|
2009-07-06 15:25:34 -04:00
|
|
|
|
|
|
|
load_paths = $-I.map {|p| "-I\"#{File.expand_path(p)}\"" }.join(" ")
|
2014-06-09 12:13:47 -04:00
|
|
|
orig_args = ORIG_ARGV.join(" ")
|
|
|
|
test_opts = "-n#{self.class.name}##{self.name}"
|
|
|
|
command = "#{Gem.ruby} #{load_paths} #{$0} #{orig_args} #{test_opts}"
|
|
|
|
|
|
|
|
# IO.popen lets us pass env in a cross-platform way
|
|
|
|
child = IO.popen([env, command])
|
|
|
|
|
|
|
|
begin
|
|
|
|
Process.wait(child.pid)
|
|
|
|
rescue Errno::ECHILD # The child process may exit before we wait
|
|
|
|
nil
|
|
|
|
end
|
2009-07-06 15:25:34 -04:00
|
|
|
|
2009-09-29 19:07:29 -04:00
|
|
|
return tmpfile.read.unpack("m")[0]
|
2009-07-06 15:25:34 -04:00
|
|
|
end
|
2009-06-30 20:26:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-06 15:25:34 -04:00
|
|
|
include forking_env? ? Forking : Subprocess
|
|
|
|
end
|
2009-06-30 20:26:46 -04:00
|
|
|
end
|
|
|
|
end
|