Sha256: 17300fcbf0264e33ffa71a619b6e6edeacbd135bff0bdf9d9c4d1f080c93f435

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

require 'net/http'

module HTTPHelpers
  def async_get(uri, headers = {}, retries: 10, &block)
    uri = URI(uri.to_s)
    queue = Queue.new
    thread = Thread.start do
      begin
        Net::HTTP.start(uri.host, uri.port, open_timeout: 10) do |http|
          request = Net::HTTP::Get.new uri.request_uri
          headers.each do |name, value|
            request.add_field(name, value)
          end

          begin
            http.request request do |response|
              response.read_body do |chunk|
                queue.push(chunk) unless chunk.empty?
              end
            end
          rescue Errno::EINVAL, Errno::ECONNREFUSED # Happen once in a while when the server is not 100% ready
            if retries > 0
              retries -= 1
              sleep 0.5
              retry
            else
              raise
            end
          end
        end
      rescue Errno::ECONNRESET, Errno::EPIPE
        # The server closed the connection
      end
    end

    return queue, thread
  end

  ROOT_PATH = File.join(__dir__, '../..')
  LOG_DIR = File.join(ROOT_PATH, 'tmp')
  LOG_PATH = File.join(ROOT_PATH, 'tmp/puma.log')
  def with_background_server
    Dir.mkdir(LOG_DIR) unless Dir.exist?(LOG_DIR)
    server_pid = Process.spawn('bin/server', chdir: ROOT_PATH, out: LOG_PATH, err: LOG_PATH)
    begin
      yield
    ensure
      Process.kill('TERM', server_pid)
      Process.wait(server_pid)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pubsubstub-0.3.0 spec/support/http_helpers.rb
pubsubstub-0.2.2 spec/support/http_helpers.rb