Class: IsolatedServer::Base

Inherits:
Object
  • Object
show all
Includes:
Socket::Constants
Defined in:
lib/isolated_server/base.rb

Direct Known Subclasses

Mongodb, Mysql

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
# File 'lib/isolated_server/base.rb', line 8

def initialize(options)
  @base         = options[:base] || Dir.mktmpdir("isolated", "/tmp")
  @params       = options[:params]
  @port         = options[:port]
  @allow_output = options[:allow_output]
  @parent_pid   = options[:pid]
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/isolated_server/base.rb', line 5

def base
  @base
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/isolated_server/base.rb', line 6

def params
  @params
end

#pidObject (readonly)

Returns the value of attribute pid.



5
6
7
# File 'lib/isolated_server/base.rb', line 5

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/isolated_server/base.rb', line 5

def port
  @port
end

Class Method Details

.exec_wait(cmd, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/isolated_server/base.rb', line 57

def self.exec_wait(cmd, options = {})
  allow_output = options[:allow_output] # default false
  parent_pid = options[:parent_pid] || $$

  fork do
    exec_pid = fork do
      [[$stdin, :stdin], [$stdout, :stdout], [$stderr, :stderr]].each do |file, symbol|
        if options[symbol]
          file.reopen(options[symbol])
        end
      end

      if !allow_output
        devnull = File.open("/dev/null", "w")
        STDOUT.reopen(devnull)
        STDERR.reopen(devnull)
      end

      exec(cmd)
    end

    # begin waiting for the parent (or mysql) to die; at_exit is hard to control when interacting with test/unit
    # we can also be killed by our parent with down! and up!
    #
    ["TERM", "INT"].each do |sig|
      trap(sig) do
        if block_given?
          yield(exec_pid)
        else
          Process.kill("KILL", exec_pid)
        end

        exit!
      end
    end

    # HUP == down, but don't cleanup.
    trap("HUP") do
      Process.kill("KILL", exec_pid)
      exit!
    end

    while true
      begin
        Process.kill(0, parent_pid)
        Process.kill(0, exec_pid)
      rescue Exception
        if block_given?
          yield(exec_pid)
        else
          Process.kill("KILL", exec_pid)
        end

        exit!
      end

      sleep 1
    end
  end
end

.get_free_portObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/isolated_server/base.rb', line 43

def self.get_free_port
  while true
    candidate=9000 + rand(50_000)

    begin
      socket = Socket.new(AF_INET, SOCK_STREAM, 0)
      socket.bind(Socket.pack_sockaddr_in(candidate, '127.0.0.1'))
      socket.close
      return candidate
    rescue Exception
    end
  end
end

Instance Method Details

#cleanup!Object



33
34
35
# File 'lib/isolated_server/base.rb', line 33

def cleanup!
  system("rm -Rf #{base.shellescape}")
end

#down!Object



22
23
24
25
26
# File 'lib/isolated_server/base.rb', line 22

def down!
  Process.kill("HUP", @pid)
  Process.wait
  @cx = nil
end

#exec_server(cmd) ⇒ Object



118
119
120
121
122
123
# File 'lib/isolated_server/base.rb', line 118

def exec_server(cmd)
  @pid = self.class.exec_wait(cmd, allow_output: @allow_output, parent_pid: @parent_pid) do |child_pid|
    Process.kill("KILL", child_pid)
    cleanup!
  end
end

#grab_free_portObject



39
40
41
# File 'lib/isolated_server/base.rb', line 39

def grab_free_port
  self.class.get_free_port
end

#kill!Object



28
29
30
31
# File 'lib/isolated_server/base.rb', line 28

def kill!
  return unless @pid
  Process.kill("TERM", @pid)
end

#locate_executable(*candidates) ⇒ Object



16
17
18
19
20
# File 'lib/isolated_server/base.rb', line 16

def locate_executable(*candidates)
  output = `which #{candidates.shelljoin}`
  raise "I couldn't find any of these: #{candidates.join(',')} in $PATH" if output.chomp.empty?
  output.split("\n").first
end