Created
February 13, 2018 17:06
-
-
Save dentarg/17528a08172e954061857c71db9be926 to your computer and use it in GitHub Desktop.
Problematic spec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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` |
Author
dentarg
commented
Feb 13, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment