mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									75bfc6440d
								
							
						
					
					
						commit
						1d15d5f080
					
				
					 4370 changed files with 0 additions and 0 deletions
				
			
		
							
								
								
									
										84
									
								
								spec/ruby/core/process/fixtures/common.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								spec/ruby/core/process/fixtures/common.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,84 @@
 | 
			
		|||
module ProcessSpecs
 | 
			
		||||
  def self.use_system_ruby(context)
 | 
			
		||||
    if defined?(MSpecScript::SYSTEM_RUBY)
 | 
			
		||||
      context.send(:before, :all) do
 | 
			
		||||
        @ruby = ::RUBY_EXE
 | 
			
		||||
        Object.const_set(:RUBY_EXE, MSpecScript::SYSTEM_RUBY)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      context.send(:after, :all) do
 | 
			
		||||
        Object.const_set(:RUBY_EXE, @ruby)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  class Daemonizer
 | 
			
		||||
    attr_reader :input, :data
 | 
			
		||||
 | 
			
		||||
    def initialize
 | 
			
		||||
      # Fast feedback for implementations without Process.daemon
 | 
			
		||||
      raise NotImplementedError, "Process.daemon is not implemented" unless Process.respond_to? :daemon
 | 
			
		||||
 | 
			
		||||
      @script = fixture __FILE__, "daemon.rb"
 | 
			
		||||
      @input = tmp("process_daemon_input_file")
 | 
			
		||||
      @data = tmp("process_daemon_data_file")
 | 
			
		||||
      @args = []
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def wait_for_daemon
 | 
			
		||||
      sleep 0.001 until File.exist?(@data) and File.size?(@data)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def invoke(behavior, arguments=[])
 | 
			
		||||
      args = Marshal.dump(arguments).unpack("H*")
 | 
			
		||||
      args << @input << @data << behavior
 | 
			
		||||
 | 
			
		||||
      ruby_exe @script, args: args
 | 
			
		||||
 | 
			
		||||
      wait_for_daemon
 | 
			
		||||
 | 
			
		||||
      return unless File.exist? @data
 | 
			
		||||
 | 
			
		||||
      File.open(@data, "rb") { |f| return f.read.chomp }
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  class Signalizer
 | 
			
		||||
    attr_reader :pid_file, :pid
 | 
			
		||||
 | 
			
		||||
    def initialize(scenario=nil)
 | 
			
		||||
      platform_is :windows do
 | 
			
		||||
        fail "not supported on windows"
 | 
			
		||||
      end
 | 
			
		||||
      @script = fixture __FILE__, "kill.rb"
 | 
			
		||||
      @pid = nil
 | 
			
		||||
      @pid_file = tmp("process_kill_signal_file")
 | 
			
		||||
      rm_r @pid_file
 | 
			
		||||
 | 
			
		||||
      @thread = Thread.new do
 | 
			
		||||
        Thread.current.abort_on_exception = true
 | 
			
		||||
        args = [@pid_file]
 | 
			
		||||
        args << scenario if scenario
 | 
			
		||||
        @result = ruby_exe @script, args: args
 | 
			
		||||
      end
 | 
			
		||||
      Thread.pass while @thread.status and !File.exist?(@pid_file)
 | 
			
		||||
      while @thread.status && (@pid.nil? || @pid == 0)
 | 
			
		||||
        @pid = IO.read(@pid_file).chomp.to_i
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def wait_on_result
 | 
			
		||||
      @thread.join
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def cleanup
 | 
			
		||||
      wait_on_result
 | 
			
		||||
      rm_r pid_file
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def result
 | 
			
		||||
      wait_on_result
 | 
			
		||||
      @result.chomp if @result
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										111
									
								
								spec/ruby/core/process/fixtures/daemon.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								spec/ruby/core/process/fixtures/daemon.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,111 @@
 | 
			
		|||
module ProcessSpecs
 | 
			
		||||
  class Daemon
 | 
			
		||||
    def initialize(argv)
 | 
			
		||||
      args, @input, @data, @behavior = argv
 | 
			
		||||
      @args = Marshal.load [args].pack("H*")
 | 
			
		||||
      @no_at_exit = false
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def run
 | 
			
		||||
      send @behavior
 | 
			
		||||
 | 
			
		||||
      # Exit without running any at_exit handlers
 | 
			
		||||
      exit!(0) if @no_at_exit
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def write(data)
 | 
			
		||||
      File.open(@data, "wb") { |f| f.puts data }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def daemonizing_at_exit
 | 
			
		||||
      at_exit do
 | 
			
		||||
        write "running at_exit"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      @no_at_exit = true
 | 
			
		||||
      Process.daemon
 | 
			
		||||
      write "not running at_exit"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def return_value
 | 
			
		||||
      write Process.daemon.to_s
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def pid
 | 
			
		||||
      parent = Process.pid
 | 
			
		||||
      Process.daemon
 | 
			
		||||
      daemon = Process.pid
 | 
			
		||||
      write "#{parent}:#{daemon}"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def process_group
 | 
			
		||||
      parent = Process.getpgrp
 | 
			
		||||
      Process.daemon
 | 
			
		||||
      daemon = Process.getpgrp
 | 
			
		||||
      write "#{parent}:#{daemon}"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def daemon_at_exit
 | 
			
		||||
      at_exit do
 | 
			
		||||
        write "running at_exit"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      Process.daemon
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def stay_in_dir
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      write Dir.pwd
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_false_stdout
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      $stdout.write "writing to stdout"
 | 
			
		||||
      write ""
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_false_stderr
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      $stderr.write "writing to stderr"
 | 
			
		||||
      write ""
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_false_stdin
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
 | 
			
		||||
      # Reading from /dev/null will return right away. If STDIN were not
 | 
			
		||||
      # /dev/null, reading would block and the spec would hang. This is not a
 | 
			
		||||
      # perfect way to spec the behavior but it works.
 | 
			
		||||
      write $stdin.read
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_true_stdout
 | 
			
		||||
      $stdout.reopen @data
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      $stdout.write "writing to stdout"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_true_stderr
 | 
			
		||||
      $stderr.reopen @data
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      $stderr.write "writing to stderr"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_true_stdin
 | 
			
		||||
      File.open(@input, "w") { |f| f.puts "reading from stdin" }
 | 
			
		||||
 | 
			
		||||
      $stdin.reopen @input, "r"
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      write $stdin.read
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def keep_stdio_open_files
 | 
			
		||||
      file = File.open @input, "w"
 | 
			
		||||
 | 
			
		||||
      Process.daemon(*@args)
 | 
			
		||||
      write file.closed?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
ProcessSpecs::Daemon.new(ARGV).run
 | 
			
		||||
							
								
								
									
										45
									
								
								spec/ruby/core/process/fixtures/kill.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								spec/ruby/core/process/fixtures/kill.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
require 'thread'
 | 
			
		||||
 | 
			
		||||
pid_file = ARGV.shift
 | 
			
		||||
scenario = ARGV.shift
 | 
			
		||||
 | 
			
		||||
# We must do this first otherwise there will be a race with the process that
 | 
			
		||||
# creates this process and the TERM signal below could go to that process
 | 
			
		||||
# instead, which will likely abort the specs process.
 | 
			
		||||
Process.setsid if scenario
 | 
			
		||||
 | 
			
		||||
mutex = Mutex.new
 | 
			
		||||
 | 
			
		||||
Signal.trap(:TERM) do
 | 
			
		||||
  if mutex.try_lock
 | 
			
		||||
    STDOUT.puts "signaled"
 | 
			
		||||
    STDOUT.flush
 | 
			
		||||
    $signaled = true
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
File.open(pid_file, "wb") { |f| f.puts Process.pid }
 | 
			
		||||
 | 
			
		||||
if scenario
 | 
			
		||||
  # We are sending a signal to the process group
 | 
			
		||||
  process = "Process.getpgrp"
 | 
			
		||||
 | 
			
		||||
  case scenario
 | 
			
		||||
  when "self"
 | 
			
		||||
    signal = %["SIGTERM"]
 | 
			
		||||
    process = "0"
 | 
			
		||||
  when "group_numeric"
 | 
			
		||||
    signal = %[-Signal.list["TERM"]]
 | 
			
		||||
  when "group_short_string"
 | 
			
		||||
    signal = %["-TERM"]
 | 
			
		||||
  when "group_full_string"
 | 
			
		||||
    signal = %["-SIGTERM"]
 | 
			
		||||
  else
 | 
			
		||||
    raise "unknown scenario: #{scenario.inspect}"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  code = "Process.kill(#{signal}, #{process})"
 | 
			
		||||
  system(ENV["RUBY_EXE"], *ENV["RUBY_FLAGS"].split(' '), "-e", code)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
sleep 0.001 until mutex.locked? and $signaled
 | 
			
		||||
							
								
								
									
										8
									
								
								spec/ruby/core/process/fixtures/map_fd.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								spec/ruby/core/process/fixtures/map_fd.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
fd = ARGV.shift.to_i
 | 
			
		||||
 | 
			
		||||
f = File.for_fd fd
 | 
			
		||||
begin
 | 
			
		||||
  f.write "writing to fd: #{fd}"
 | 
			
		||||
ensure
 | 
			
		||||
  f.close
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										12
									
								
								spec/ruby/core/process/fixtures/setpriority.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								spec/ruby/core/process/fixtures/setpriority.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
case ARGV[0]
 | 
			
		||||
when "process"
 | 
			
		||||
  which = Process::PRIO_PROCESS
 | 
			
		||||
when "group"
 | 
			
		||||
  Process.setpgrp
 | 
			
		||||
  which = Process::PRIO_PGRP
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
priority = Process.getpriority(which, 0)
 | 
			
		||||
p priority
 | 
			
		||||
p Process.setpriority(which, 0, priority + 1)
 | 
			
		||||
p Process.getpriority(which, 0)
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue