Sha256: 3c46d6d307fafdc9abbd2b5b8bdc73212738eca1002d3cee7df6ad5971ee614f

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require "net/http"

module FakeSQS
  class TestIntegration

    attr_reader :options

    def initialize(options = {})
      @options = options
    end

    def host
      option :sqs_endpoint
    end

    def port
      option :sqs_port
    end

    def start
      start! unless up?
      reset
    end

    def start!
      @pid = Process.spawn(binfile, "-p", port.to_s, :out => out, :err => out)
      wait_until_up
    end

    def stop
      if @pid
        Process.kill("INT", @pid)
        Process.waitpid(@pid)
        @pid = nil
      else
        $stderr.puts "FakeSQS is not running"
      end
    end

    def reset
      connection.delete("/")
    end

    def expire
      connection.put("/", "")
    end

    def url
      "http://#{host}:#{port}"
    end

    def up?
      @pid && connection.get("/").code.to_s == "200"
    rescue Errno::ECONNREFUSED
      false
    end

    private

    def option(key)
      options.fetch(key) { AWS.config.public_send(key) }
    end


    def wait_until_up(deadline = Time.now + 2)
      fail "FakeSQS didn't start in time" if Time.now > deadline
      unless up?
        sleep 0.01
        wait_until_up(deadline)
      end
    end

    def binfile
      File.expand_path("../../../bin/fake_sqs", __FILE__)
    end

    def out
      if debug?
        :out
      else
        "/dev/null"
      end
    end

    def connection
      @connection ||= Net::HTTP.new(host, port)
    end

    def debug?
      ENV["DEBUG"].to_s == "true"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fake_sqs-0.0.9 lib/fake_sqs/test_integration.rb
fake_sqs-0.0.8 lib/fake_sqs/test_integration.rb
fake_sqs-0.0.7 lib/fake_sqs/test_integration.rb