Sha256: f907883f583b8a6dfc18e3d9c5799803707b5b41357f5bd50b6ecfef781b818b

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8

module SpecHelper
  def async_sleep(seconds)
    fiber = Fiber.current
    EM.add_timer(seconds) { fiber.resume }
    Fiber.yield
  end
  module_function :async_sleep

  def tick(ticks = 1)
    fiber = Fiber.current
    EM.next_tick { fiber.resume }
    Fiber.yield

    tick(ticks - 1) if ticks > 1
  end
  module_function :tick

  def defer
    EM.next_tick do
      Fiber.new { yield }.resume
    end
  end
  module_function :defer

  def open_request(http_method = :get, uri = '/', headers = {})
    Hatetepe::Request.new(http_method, uri, headers, Hatetepe::Body.new)
  end

  def closed_request(*args)
    open_request(*args).tap(&:close)
  end

  def open_response(status = 200, headers = {})
    Hatetepe::Response.new(status, headers, Hatetepe::Body.new)
  end

  def closed_response(*args)
    open_response(*args).tap(&:close)
  end

  def localhost
    '127.0.0.1'
  end

  def random_port
    server = TCPServer.new(localhost, 0)
    server.addr[1]
  ensure
    server.shutdown
  end

  def server_client_pair(server_config, client_config, interceptor)
    server = nil
    inspect_server(server_config, interceptor) { |_, s, _| server = s }

    config = { address: localhost, port: random_port }
    Hatetepe::Server.start(config.merge(server_config))
    client = Hatetepe::Client.new(config.merge(client_config))

    tick
    [server, client]
  end

  def inspect_server(server_config, interceptor, &block)
    klass = double
    allow(klass).to       receive(:new) { interceptor }
    allow(interceptor).to receive(:setup, &block)

    server_config[:handlers].unshift(klass)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatetepe-0.6.0.pre.2 spec/support/helper.rb