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.



20
21
22
23
# File 'lib/pact/mock_service/app_manager.rb', line 20

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

Instance Method Details

#app_registered_on?(port) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#clear_allObject



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

def clear_all
  kill_all
  @app_registrations = []
end

#kill_allObject



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

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

#ports_of_mock_servicesObject



41
42
43
# File 'lib/pact/mock_service/app_manager.rb', line 41

def ports_of_mock_services
  app_registrations.find_all(&:is_a_mock_service?).collect(&:port)
end

#register(app, port = FindAPort.available_port) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/pact/mock_service/app_manager.rb', line 33

def register(app, port = FindAPort.available_port)
  existing = existing_app_on_port port
  raise "Port #{port} is already being used by #{existing}" if existing and not existing == app
  app_registration = register_app app, port
  app_registration.spawn if @apps_spawned
  port
end

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



25
26
27
28
29
30
31
# File 'lib/pact/mock_service/app_manager.rb', line 25

def register_mock_service_for name, url, options = {}
  uri = URI(url)
  raise "Currently only http is supported" unless uri.scheme == 'http'
  raise "Currently only services on localhost are supported" unless uri.host == 'localhost'

  register(Pact::MockService.new(log_file: create_log_file(name), name: name, pact_dir: pact_dir, pact_specification_version: options[:pact_specification_version]), uri.port)
end

#spawn_allObject



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

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