2010-04-26 11:25:07 -04:00
|
|
|
require 'rbconfig'
|
2013-12-06 04:26:59 -05:00
|
|
|
require 'minitest/parallel'
|
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:
|
2012-06-19 20:08:15 -04:00
|
|
|
klass.extend(Module.new {
|
|
|
|
def test_methods
|
|
|
|
ParallelEach.new super
|
|
|
|
end
|
|
|
|
})
|
|
|
|
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
|
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|
|
2013-08-01 23:35:27 -04:00
|
|
|
ENV["ISOLATION_TEST"] = self.class.name
|
2009-07-06 15:25:34 -04:00
|
|
|
ENV["ISOLATION_OUTPUT"] = tmpfile.path
|
|
|
|
|
|
|
|
load_paths = $-I.map {|p| "-I\"#{File.expand_path(p)}\"" }.join(" ")
|
2013-08-01 23:35:27 -04:00
|
|
|
`#{Gem.ruby} #{load_paths} #{$0} #{ORIG_ARGV.join(" ")}`
|
2009-07-06 15:25:34 -04:00
|
|
|
|
|
|
|
ENV.delete("ISOLATION_TEST")
|
|
|
|
ENV.delete("ISOLATION_OUTPUT")
|
|
|
|
|
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
|