Sha256: c300a98bf03463f32f8052081aecd0048f15ed525cc99334cd0460dc57098c2e

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require "net/http"

module CukeQHelper

  MASTER_ARGS = %w[--scm git://github.com/jarib/cukeq.git --report-to http://localhost:1212]

  def cleanup
    pids.each { |pid| Process.kill(:KILL, pid) }
    Process.wait
  end

  def start_master
    pids << fork { CukeQ::Master.execute(MASTER_ARGS) }
    ensure_running
  end

  def start_slave
    pids << fork { CukeQ::Slave.execute }
    ensure_running
  end

  def start_report_app
    app = report_app()

    pids << fork { app.start }
    ensure_running
  end

  def post(url, data)
    uri = URI.parse(url)
    req = Net::HTTP::Post.new(uri.path)
    req.body = data

    execute_request uri, req
  end

  def get(url)
    uri = URI.parse(url)
    execute_request uri, Net::HTTP::Get.new(uri.path)
  end

  def master_url
    "http://localhost:9292/"
  end

  def ensure_running
    3.times do
      pid, status = Process.waitpid2(pids.last, Process::WNOHANG)
      raise "process died: #{status.inspect}" if pid
      sleep 0.5
    end
  end

  def report_app
    @report_app ||= ReportApp.new
  end

  def pids
    @pids ||= []
  end

  def execute_request(url, req)
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }

    case res
    when Net::HTTPSuccess
      res.body
    else
      res.error!
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cukeq-0.0.1.dev features/support/cukeq_helper.rb