Class: Sbmt::Pact::Provider::ProviderServerRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/sbmt/pact/provider/provider_server_runner.rb

Constant Summary collapse

SETUP_PROVIDER_STATE_PATH =
"/setup-provider"
VERIFY_MESSAGE_PATH =
"/verify-message"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: 9001, host: "127.0.0.1", logger: nil) ⇒ ProviderServerRunner

Returns a new instance of ProviderServerRunner.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 14

def initialize(port: 9001, host: "127.0.0.1", logger: nil)
  @host = host
  @port = port
  @provider_setup_states = {}
  @provider_teardown_states = {}
  @logger = logger || Logger.new($stdout)

  @state_servlet = ProviderStateServlet.new(logger: @logger)
  @message_servlet = MessageProviderServlet.new(logger: @logger)
  @thread = nil
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 9

def logger
  @logger
end

Instance Method Details

#add_message_handler(state_name, &block) ⇒ Object



67
68
69
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 67

def add_message_handler(state_name, &block)
  @message_servlet.add_message_handler(state_name, &block)
end

#add_setup_state(state_name, use_before_setup_hook = true, &block) ⇒ Object



71
72
73
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 71

def add_setup_state(state_name, use_before_setup_hook = true, &block)
  @state_servlet.add_setup_state(state_name, use_before_setup_hook, &block)
end

#add_teardown_state(state_name, use_after_teardown_hook = true, &block) ⇒ Object



75
76
77
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 75

def add_teardown_state(state_name, use_after_teardown_hook = true, &block)
  @state_servlet.add_teardown_state(state_name, use_after_teardown_hook, &block)
end

#message_setup_urlObject



30
31
32
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 30

def message_setup_url
  "http://#{@host}:#{@port}#{VERIFY_MESSAGE_PATH}"
end

#runObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 56

def run
  start

  yield
rescue => e
  logger.fatal("FATAL ERROR: #{e.message} #{e.backtrace.join("\n")}")
  raise
ensure
  stop
end

#set_after_teardown_hook(&block) ⇒ Object



83
84
85
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 83

def set_after_teardown_hook(&block)
  @state_servlet.after_teardown(&block)
end

#set_before_setup_hook(&block) ⇒ Object



79
80
81
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 79

def set_before_setup_hook(&block)
  @state_servlet.before_setup(&block)
end

#startObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 34

def start
  raise "server already running, stop server before starting new one" if @thread

  @server = WEBrick::HTTPServer.new({BindAddress: @host, Port: @port}, WEBrick::Config::HTTP)
  @server.mount(SETUP_PROVIDER_STATE_PATH, @state_servlet)
  @server.mount(VERIFY_MESSAGE_PATH, @message_servlet)

  @thread = Thread.new do
    Rails.logger.debug "starting provider setup server"
    @server.start
  end
end

#state_setup_urlObject



26
27
28
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 26

def state_setup_url
  "http://#{@host}:#{@port}#{SETUP_PROVIDER_STATE_PATH}"
end

#stopObject



47
48
49
50
51
52
53
54
# File 'lib/sbmt/pact/provider/provider_server_runner.rb', line 47

def stop
  @logger.info("stopping provider setup server")

  @server&.shutdown
  @thread&.join

  @logger.info("provider setup server stopped")
end