Sha256: 353ab1ae289695798f746e377c0ba9a02846f422062a018c467f5cf5c3d1c319

Contents?: true

Size: 861 Bytes

Versions: 21

Compression:

Stored size: 861 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, port
          if pidfile.can_start?
            if port_available? port
              pid = fork do
                yield
              end
              pidfile.pid = pid
              Process.detach(pid)
              Server::WaitForServerUp.(port)
              pidfile.write
            else
              raise PortUnavailableError.new("ERROR: Port #{port} already in use.")
            end
          end
        end

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

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
pact-mock_service-0.2.4 lib/pact/mock_service/server/spawn.rb