Class: Sbmt::Pact::Consumer::MockServer

Inherits:
Object
  • Object
show all
Defined in:
lib/sbmt/pact/consumer/mock_server.rb

Defined Under Namespace

Classes: MockServerCreateError, WritePactsError

Constant Summary collapse

TRANSPORT_HTTP =
"http"
TRANSPORT_GRPC =
"grpc"
TRANSPORTS =
[TRANSPORT_HTTP, TRANSPORT_GRPC].freeze
CREATE_TRANSPORT_ERRORS =
{
  -1 => {reason: :invalid_handle, status: -1, description: "An invalid handle was received. Handles should be created with pactffi_new_pact"},
  -2 => {reason: :invalid_transport_json, status: -2, description: "Transport_config is not valid JSON"},
  -3 => {reason: :mock_server_not_started, status: -3, description: "The mock server could not be started"},
  -4 => {reason: :internal_error, status: -4, description: "The method panicked"},
  -5 => {reason: :invalid_host, status: -5, description: "The address is not valid"}
}.freeze
WRITE_PACT_FILE_ERRORS =
{
  1 => {reason: :internal_error, status: 1, description: "A general panic was caught"},
  2 => {reason: :file_not_accessible, status: 2, description: "The pact file was not able to be written"},
  3 => {reason: :mock_server_not_found, status: 3, description: "A mock server with the provided port was not found"}
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pact:, transport:, host:, port:) ⇒ MockServer

Returns a new instance of MockServer.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 44

def initialize(pact:, transport:, host:, port:)
  raise "Transport #{transport} is not supported yet, available transports are: #{TRANSPORTS.join(",")}" unless TRANSPORTS.include?(transport)

  @pact = pact
  @transport = transport
  @host = host
  @port = port

  @handle = init_transport!

  # TODO: handle auto-GC of native memory
  # ObjectSpace.define_finalizer(self, proc do
  #   cleanup
  # end)
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



9
10
11
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9

def handle
  @handle
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9

def port
  @port
end

#transportObject (readonly)

Returns the value of attribute transport.



9
10
11
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9

def transport
  @transport
end

Class Method Details

.create_for_grpc!(pact:, host: "127.0.0.1", port: 0) ⇒ Object



36
37
38
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 36

def self.create_for_grpc!(pact:, host: "127.0.0.1", port: 0)
  new(pact: pact, transport: TRANSPORT_GRPC, host: host, port: port)
end

.create_for_http!(pact:, host: "127.0.0.1", port: 0) ⇒ Object



40
41
42
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 40

def self.create_for_http!(pact:, host: "127.0.0.1", port: 0)
  new(pact: pact, transport: TRANSPORT_HTTP, host: host, port: port)
end

Instance Method Details

#cleanupObject



76
77
78
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 76

def cleanup
  PactFfi::MockServer.cleanup(@handle)
end

#matched?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 68

def matched?
  PactFfi::MockServer.matched(@handle)
end

#mismatchesObject



72
73
74
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 72

def mismatches
  PactFfi::MockServer.mismatches(@handle)
end

#write_pacts!(dir) ⇒ Object

Raises:



60
61
62
63
64
65
66
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 60

def write_pacts!(dir)
  result = PactFfi::MockServer.write_pact_file(@handle, dir, false)
  return result if WRITE_PACT_FILE_ERRORS[result].blank?

  error = WRITE_PACT_FILE_ERRORS[result]
  raise WritePactsError.new("There was an error while trying to write pact file to #{dir}", error[:reason], error[:status])
end