class ServiceDouble::Manager

Attributes

config[R]
log_file[R]
name[R]
root_url[R]
server[R]
started_at[R]
timeout[R]

Public Class Methods

new(config) click to toggle source
# File lib/service_double/manager.rb, line 23
def initialize(config)
  @config   = config
  @root_url = config.url
  @log_file = config.log_file
  @timeout  = config.timeout.to_f
  @server   = config.server
  @name     = config.name
end

Public Instance Methods

connection() click to toggle source
# File lib/service_double/manager.rb, line 84
def connection
  @connection ||= Faraday.new(url: root_url) { |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  }
end
env() click to toggle source
# File lib/service_double/manager.rb, line 92
def env
  if config.disable_bundler
    {"RUBYOPT" => "", "BUNDLE_BIN_PATH" => "", "BUNDLE_GEM_PATH" => ""}
  else
    {}
  end
end
port() click to toggle source
# File lib/service_double/manager.rb, line 52
def port
  URI(root_url).port.to_s
end
reset() click to toggle source
# File lib/service_double/manager.rb, line 41
def reset
  delete("/")
end
running?() click to toggle source
# File lib/service_double/manager.rb, line 76
def running?
  get("/").status == 200 rescue false
end
start() click to toggle source
# File lib/service_double/manager.rb, line 32
def start
  FileUtils.mkdir_p(File.dirname(log_file)) if log_file.is_a?(String)
  args = %w(ruby -r sinatra -r json) << server << "-p" << port
  args << { :out => log_file, :err => log_file }
  @pid = Process.spawn(env, *args)
  @started_at = Time.now
  wait until up?
end
started?() click to toggle source
# File lib/service_double/manager.rb, line 68
def started?
  if @pid
    Process.getpgid(@pid) rescue false
  else
    raise NotStarted, "The fake #{name} hasn't been started yet."
  end
end
stop() click to toggle source
# File lib/service_double/manager.rb, line 45
def stop
  if started?
    Process.kill("TERM", @pid)
    Process.waitpid(@pid)
  end
end
timed_out?() click to toggle source
# File lib/service_double/manager.rb, line 80
def timed_out?
  Time.now - started_at > timeout
end
up?() click to toggle source
# File lib/service_double/manager.rb, line 64
def up?
  started? and running?
end
wait() click to toggle source
# File lib/service_double/manager.rb, line 56
def wait
  unless timed_out?
    sleep 0.2
  else
    raise Timeout, "It took more than #{timeout} seconds to start #{name}. Check the logs at #{log_file} to see why."
  end
end