Skip to content

Commit

Permalink
Time out after 10 seconds if starting the spring server doesn't work
Browse files Browse the repository at this point in the history
Maybe related to #480, #479
  • Loading branch information
jonleighton committed Apr 11, 2016
1 parent 487a326 commit bec1b8e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Specify absolute path to spring binfile when starting the server
(#478)
* Time out after 10 seconds if starting the spring server doesn't work
(maybe related to #480, #479)

## 1.7.0

Expand Down
19 changes: 15 additions & 4 deletions lib/spring/client/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Spring
module Client
class Run < Command
FORWARDED_SIGNALS = %w(INT QUIT USR1 USR2 INFO WINCH) & Signal.list.keys
TIMEOUT = 1
CONNECT_TIMEOUT = 1
BOOT_TIMEOUT = 10

attr_reader :server

Expand Down Expand Up @@ -69,11 +70,21 @@ def run

def boot_server
env.socket_path.unlink if env.socket_path.exist?
pid = Process.spawn(gem_env, env.server_command, out: File::NULL)

pid = Process.spawn(gem_env, env.server_command, out: File::NULL)
timeout = Time.now + BOOT_TIMEOUT

until env.socket_path.exist?
_, status = Process.waitpid2(pid, Process::WNOHANG)
exit status.exitstatus if status

if status
exit status.exitstatus
elsif Time.now > timeout
$stderr.puts "Starting Spring server with `#{env.server_command}` " \
"timed out after #{BOOT_TIMEOUT} seconds"
exit 1
end

sleep 0.1
end
end
Expand Down Expand Up @@ -111,7 +122,7 @@ def connect_to_application(client)
server.send_io client
send_json server, "args" => args, "default_rails_env" => default_rails_env

if IO.select([server], [], [], TIMEOUT)
if IO.select([server], [], [], CONNECT_TIMEOUT)
server.gets or raise CommandNotFound
else
raise "Error connecting to Spring server"
Expand Down
11 changes: 10 additions & 1 deletion lib/spring/test/acceptance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def exec_name
end

test "can define client tasks" do
File.write("#{app.spring_config.sub('.rb', '_client.rb')}", <<-RUBY)
File.write("#{app.spring_client_config}", <<-RUBY)
Spring::Client::COMMANDS["foo"] = lambda { |args| puts "bar -- \#{args.inspect}" }
RUBY
assert_success "bin/spring foo --baz", stdout: "bar -- [\"foo\", \"--baz\"]\n"
Expand Down Expand Up @@ -525,6 +525,15 @@ def exec_name
assert_success app.spring_test_command
end
end

test "server boot timeout" do
app.env["SPRING_SERVER_COMMAND"] = "sleep 1"
app.spring_client_config.write %(
Spring::Client::Run.const_set(:BOOT_TIMEOUT, 0.1)
)

assert_failure "bin/rails runner ''", stderr: "timed out"
end
end
end
end
4 changes: 4 additions & 0 deletions lib/spring/test/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def spring_config
path "config/spring.rb"
end

def spring_client_config
path "config/spring_client.rb"
end

def run(command, opts = {})
start_time = Time.now

Expand Down

0 comments on commit bec1b8e

Please sign in to comment.