Class: Sbmt::Pact::Consumer::MockServer
- Inherits:
-
Object
- Object
- Sbmt::Pact::Consumer::MockServer
- 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
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Class Method Summary collapse
- .create_for_grpc!(pact:, host: "127.0.0.1", port: 0) ⇒ Object
- .create_for_http!(pact:, host: "127.0.0.1", port: 0) ⇒ Object
Instance Method Summary collapse
- #cleanup ⇒ Object
-
#initialize(pact:, transport:, host:, port:) ⇒ MockServer
constructor
A new instance of MockServer.
- #matched? ⇒ Boolean
- #mismatches ⇒ Object
- #write_pacts!(dir) ⇒ Object
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
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
9 10 11 |
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9 def handle @handle end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 9 def port @port end |
#transport ⇒ Object (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
#cleanup ⇒ Object
76 77 78 |
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 76 def cleanup PactFfi::MockServer.cleanup(@handle) end |
#matched? ⇒ Boolean
68 69 70 |
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 68 def matched? PactFfi::MockServer.matched(@handle) end |
#mismatches ⇒ Object
72 73 74 |
# File 'lib/sbmt/pact/consumer/mock_server.rb', line 72 def mismatches PactFfi::MockServer.mismatches(@handle) end |
#write_pacts!(dir) ⇒ Object
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 |