Sha256: b4bab6118bd9dcb1487253c9ecacef69115d52d71641114507016128a8cad678

Contents?: true

Size: 903 Bytes

Versions: 25

Compression:

Stored size: 903 Bytes

Contents

require 'pact/mock_service/server/wait_for_server_up'

module Pact
  module MockService
    module Server
      class Spawn

        class PortUnavailableError < StandardError; end

        def self.call pidfile, host, port, ssl = false
          if pidfile.can_start?
            if port_available? host, port
              pid = fork do
                yield
              end
              pidfile.pid = pid
              Process.detach(pid)
              Server::WaitForServerUp.(host, port, {ssl: ssl})
              pidfile.write
            else
              raise PortUnavailableError.new("ERROR: Port #{port} already in use.")
            end
          end
        end

        def self.port_available? host, port
          server = TCPServer.new(host, port)
          true
        rescue
          false
        ensure
          server.close if server
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
pact-mock_service-3.2.1 lib/pact/mock_service/server/spawn.rb
pact-mock_service-3.2.0 lib/pact/mock_service/server/spawn.rb
pact-mock_service-3.1.1 lib/pact/mock_service/server/spawn.rb
pact-mock_service-3.1.0 lib/pact/mock_service/server/spawn.rb
pact-mock_service-3.0.1 lib/pact/mock_service/server/spawn.rb