Class: Pact::MockService::AppManager

Inherits:
Object
  • Object
show all
Includes:
Logging, Singleton
Defined in:
lib/pact/mock_service/app_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeAppManager

Returns a new instance of AppManager.



16
17
18
19
# File 'lib/pact/mock_service/app_manager.rb', line 16

def initialize
  @apps_spawned = false
  @app_registrations = []
end

Instance Method Details

#app_registered_on?(port) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/pact/mock_service/app_manager.rb', line 64

def app_registered_on?(port)
  app_registrations.any? { |app_registration| app_registration.port == port }
end

#clear_allObject



54
55
56
57
# File 'lib/pact/mock_service/app_manager.rb', line 54

def clear_all
  kill_all
  @app_registrations = []
end

#kill_allObject



49
50
51
52
# File 'lib/pact/mock_service/app_manager.rb', line 49

def kill_all
  app_registrations.find_all(&:spawned?).collect(&:kill)
  @apps_spawned = false
end

#register(app, host, port = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/pact/mock_service/app_manager.rb', line 35

def register(app, host, port = nil)
  if port
    existing = existing_app_on_host_and_port(host, port)
    raise "Port #{port} is already being used by #{existing}" if existing and not existing == app
  end
  app_registration = register_app(app, host, port)
  app_registration.spawn
  app_registration.port
end

#register_mock_service_for(name, url, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pact/mock_service/app_manager.rb', line 21

def register_mock_service_for(name, url, options = {})
  uri = URI(url)
  raise "Currently only http is supported" unless uri.scheme == 'http'
  uri.port = nil if options[:find_available_port]

  app = Pact::MockService.new(
    name: name,
    log_file: create_log_file(name),
    pact_dir: pact_dir,
    pact_specification_version: options.fetch(:pact_specification_version)
  )
  register(app, uri.host, uri.port)
end

#spawn_allObject



59
60
61
62
# File 'lib/pact/mock_service/app_manager.rb', line 59

def spawn_all
  app_registrations.find_all(&:not_spawned?).collect(&:spawn)
  @apps_spawned = true
end

#urls_of_mock_servicesObject



45
46
47
# File 'lib/pact/mock_service/app_manager.rb', line 45

def urls_of_mock_services
  app_registrations.find_all(&:is_a_mock_service?).collect{ |ar| "http://#{ar.host}:#{ar.port}" }
end