lib/teaspoon/server.rb in teaspoon-0.7.9 vs lib/teaspoon/server.rb in teaspoon-0.8.0

- old
+ new

@@ -3,55 +3,54 @@ require "webrick" module Teaspoon class Server + attr_accessor :port + def initialize - @port = find_available_port - if defined?(Thin) - if Teaspoon.configuration.suppress_log - Thin::Logging.silent = true - else - Thin::Logging.trace = false - end - end + @port = Teaspoon.configuration.server_port || find_available_port end def start - STDOUT.print "Starting the Teaspoon server...\n" unless Teaspoon.configuration.suppress_log - @thread = Thread.new do + thread = Thread.new do + disable_logging server = Rack::Server.new(rack_options) server.start end - wait_until_started + wait_until_started(thread) rescue => e - raise "Cannot start server: #{e.message}" + raise Teaspoon::ServerException, "Cannot start server: #{e.message}" end - def wait_until_started - Timeout.timeout(Teaspoon.configuration.server_timeout.to_i) { @thread.join(0.1) until responsive? } - rescue Timeout::Error - raise "Server failed to start. You may need to increase the timeout configuration." - end - def responsive? - return false if @thread && @thread.join(0) TCPSocket.new("127.0.0.1", port).close - return true + true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH - return false + false end def url "http://127.0.0.1:#{port}" end - def port - @port + protected + + def wait_until_started(thread) + Timeout.timeout(Teaspoon.configuration.server_timeout.to_i) { thread.join(0.1) until responsive? } + rescue Timeout::Error + raise Teaspoon::ServerException, "Server failed to start. You may need to increase the timeout configuration." end - protected + def disable_logging + return unless defined?(Thin) + if Teaspoon.configuration.suppress_log + Thin::Logging.silent = true + else + Thin::Logging.trace = false + end + end def rack_options { app: Rails.application, Port: port, @@ -61,10 +60,9 @@ server: Teaspoon.configuration.server } end def find_available_port - return Teaspoon.configuration.server_port if Teaspoon.configuration.server_port server = TCPServer.new("127.0.0.1", 0) server.addr[1] ensure server.close if server end