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.



18
19
20
21
# File 'lib/pact/mock_service/app_manager.rb', line 18

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

Instance Method Details

#app_registered_on?(port) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/pact/mock_service/app_manager.rb', line 66

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

#clear_allObject



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

def clear_all
  kill_all
  @app_registrations = []
end

#kill_allObject



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

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

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



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

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



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

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



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

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

#urls_of_mock_servicesObject



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

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