Last active
August 29, 2015 14:18
-
-
Save rockwood/5c5a2e31ddf3941422f0 to your computer and use it in GitHub Desktop.
TestServer
This file contains 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
class TestServer | |
def initialize(options) | |
@server_port = options.fetch(:server_port) | |
@client_port = options.fetch(:client_port) | |
end | |
def start | |
@pid = spawn(command) | |
end | |
def kill | |
Process.kill "INT", @pid | |
exit 1 | |
end | |
def ready? | |
begin | |
return TCPSocket.new("127.0.0.1", @client_port) | |
rescue | |
return false | |
end | |
end | |
def command | |
"cd client && ember serve --port #{@client_port} --live-reload=false --proxy=http://localhost:#{@server_port}" | |
end | |
end | |
# Somewhere in spec/support | |
client_port = 4100 | |
server_port = 4101 | |
Capybara.server_port = server_port | |
Capybara.app_host = "http://localhost:#{client_port}" | |
server = TestServer.new(client_port: client_port, server_port: server_port) | |
fork { server.start } | |
loop do | |
break if server.ready? | |
sleep 1 | |
end | |
trap "INT" do | |
server.kill | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment