diff --git a/spec/lib/ember_cli/dev_server_spec.rb b/spec/lib/ember_cli/dev_server_spec.rb index c83318da..d6e7172a 100644 --- a/spec/lib/ember_cli/dev_server_spec.rb +++ b/spec/lib/ember_cli/dev_server_spec.rb @@ -237,6 +237,7 @@ def listen(host, port, status: 200, body: "") end @thread = Thread.new { @server.start } + wait_until_running @port end @@ -245,6 +246,25 @@ def shutdown @server&.shutdown @thread&.join end + + private + + # Not `shutdown` straight after `Thread.new`: `GenericServer#shutdown` is + # a no-op until `#start` has set up its shutdown pipe, and a new thread + # is not guaranteed to have run by the time the example finishes (on + # Ruby 4.0 it usually has not). Shutting down such a server leaves it + # looping forever, and the join in `#shutdown` never returns. + def wait_until_running + deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 5 + + until @server.status == :Running + if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline + raise "The stub development server did not start within 5 seconds" + end + + sleep 0.01 + end + end end def null_server