Class: ServiceDouble::Manager
- Inherits:
-
Object
- Object
- ServiceDouble::Manager
- Extended by:
- Forwardable
- Defined in:
- lib/service_double/manager.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root_url ⇒ Object
readonly
Returns the value of attribute root_url.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #connection ⇒ Object
- #env ⇒ Object
-
#initialize(config) ⇒ Manager
constructor
A new instance of Manager.
- #port ⇒ Object
- #reset ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #timed_out? ⇒ Boolean
- #up? ⇒ Boolean
- #wait ⇒ Object
Constructor Details
#initialize(config) ⇒ Manager
Returns a new instance of Manager.
23 24 25 26 27 28 29 30 |
# 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 |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def config @config end |
#log_file ⇒ Object (readonly)
Returns the value of attribute log_file.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def log_file @log_file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def name @name end |
#root_url ⇒ Object (readonly)
Returns the value of attribute root_url.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def root_url @root_url end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def server @server end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def started_at @started_at end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
21 22 23 |
# File 'lib/service_double/manager.rb', line 21 def timeout @timeout end |
Instance Method Details
#connection ⇒ Object
84 85 86 87 88 89 90 |
# 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 ⇒ Object
92 93 94 95 96 97 98 |
# 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 ⇒ Object
52 53 54 |
# File 'lib/service_double/manager.rb', line 52 def port URI(root_url).port.to_s end |
#reset ⇒ Object
41 42 43 |
# File 'lib/service_double/manager.rb', line 41 def reset delete("/") end |
#running? ⇒ Boolean
76 77 78 |
# File 'lib/service_double/manager.rb', line 76 def running? get("/").status == 200 rescue false end |
#start ⇒ Object
32 33 34 35 36 37 38 39 |
# 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? ⇒ Boolean
68 69 70 71 72 73 74 |
# 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 ⇒ Object
45 46 47 48 49 50 |
# File 'lib/service_double/manager.rb', line 45 def stop if started? Process.kill("TERM", @pid) Process.waitpid(@pid) end end |
#timed_out? ⇒ Boolean
80 81 82 |
# File 'lib/service_double/manager.rb', line 80 def timed_out? Time.now - started_at > timeout end |
#up? ⇒ Boolean
64 65 66 |
# File 'lib/service_double/manager.rb', line 64 def up? started? and running? end |
#wait ⇒ Object
56 57 58 59 60 61 62 |
# 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 |