Skip to content

Instantly share code, notes, and snippets.

@dentarg
Created February 13, 2018 17:06
Show Gist options
  • Select an option

  • Save dentarg/17528a08172e954061857c71db9be926 to your computer and use it in GitHub Desktop.

Select an option

Save dentarg/17528a08172e954061857c71db9be926 to your computer and use it in GitHub Desktop.
Problematic spec
$stdout.sync = true
$stderr.sync = true
require "socket"
require "timeout"
module PortProber
module_function
def random(host)
server = TCPServer.new(host, 0)
port = server.addr[1]
port
ensure
server&.close
end
def port_open?(ip, port)
TCPSocket.new(ip, port).close
true
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
false
end
def localhost
info = Socket.getaddrinfo("localhost",
80,
Socket::AF_INET,
Socket::SOCK_STREAM)
raise "unable to translate 'localhost' for TCP + IPv4" if info.empty?
info[0][3]
end
end
# puts $PID
# puts %x`ps -p #{$PID}`
ip = PortProber.localhost
port = PortProber.random(ip)
puts "#{ip}:#{port}"
env = { "PORT" => port.to_s }
command = "bundle exec puma -C config/puma.rb"
r_out, w_out = IO.pipe
pid = Process.spawn(env, command, out: w_out, err: "/dev/null")
Process.detach(pid)
def running?(pid)
Process.getpgid(pid)
true
rescue Errno::ESRCH # No such process
false
end
def not_running_or_port_open?(pid, ip, port)
not_running = !running?(pid)
port_open = PortProber.port_open?(ip, port)
not_running || port_open
end
sleep 0.01 until not_running_or_port_open?(pid, ip, port)
puts running?(pid)
# puts pid
# puts %x`ps -p #{pid}`
begin
Process.kill(:INT, pid)
Process.wait(pid)
rescue Errno::ESRCH, Errno::ECHILD
end
[r_out, w_out].each(&:close)
puts
puts %x`ps aux | grep puma ; ps aux | grep ruby`
@dentarg
Copy link
Author

dentarg commented Feb 13, 2018

~/twingly/ruby/seikan fail-if-test-db-not-removed*
$ ruby test2.rb
127.0.0.1:52069
true

dentarg          72470   0.0  0.0  2442020   1988 s006  S+    6:04PM   0:00.00 grep puma
dentarg          72468   0.0  0.0  2442612   2308 s006  S+    6:04PM   0:00.00 sh -c ps aux | grep puma ; ps aux | grep ruby
dentarg          72465   0.0  0.1  2554616  25544 s006  S+    6:04PM   0:00.03 puma: cluster worker 0: 72459 [seikan]
dentarg          72458   1.2  0.0  2466608   9868 s006  S+    6:04PM   0:00.07 ruby test2.rb
dentarg          72468   1.0  0.0  2443636   2320 s006  S+    6:04PM   0:00.00 sh -c ps aux | grep puma ; ps aux | grep ruby
dentarg          72472   0.0  0.0  2432804   1964 s006  S+    6:04PM   0:00.00 grep ruby

~/twingly/ruby/seikan fail-if-test-db-not-removed*
$ kill 72465

~/twingly/ruby/seikan fail-if-test-db-not-removed*
$ ruby test2.rb
127.0.0.1:52169
true

dentarg          72654   0.0  0.0  2442020   1984 s006  S+    6:04PM   0:00.00 grep puma
dentarg          72652   0.0  0.0  2443636   2328 s006  S+    6:04PM   0:00.00 sh -c ps aux | grep puma ; ps aux | grep ruby
dentarg          72649   0.0  0.1  2551544  25320 s006  S+    6:04PM   0:00.03 puma: cluster worker 1: 72642 [seikan]
dentarg          72648   0.0  0.1  2551544  25272 s006  S+    6:04PM   0:00.03 puma: cluster worker 0: 72642 [seikan]
dentarg          72641   1.5  0.0  2466608   9836 s006  S+    6:04PM   0:00.07 ruby test2.rb
dentarg          72652   1.1  0.0  2444660   2340 s006  S+    6:04PM   0:00.00 sh -c ps aux | grep puma ; ps aux | grep ruby
dentarg          72658   0.0  0.0  2432804   1964 s006  S+    6:04PM   0:00.00 grep ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment