Closed zbintliff closed 6 years ago
In my defense of not handling if they pass a used port: the GetFreePort()
function will return error if it grabs a used port but there is no retry logic embedded in.
To resolve #46
Thanks @zbintliff, and sorry I wasn't able to provide guidance on how to achieve this last week.
I think this is better controlled by the consumer DSL (via API), not via the daemon itself. The daemon is multi-threaded, and so it is able to spin up and manage multiple mock servers in its lifetime.
Having this specified on the command-line will limit it to just a single mock server at any point in time.
I'd look to add a new field to pact.go
, perhaps something like MockServerPort
to take an optional new argument for the port when creating a pact
struct. You can then add a check to pact.Setup(...)
to select a default port using utils.GetFreePort()
if none is specified using the same mechanism that mock_service.go
currently does, and remove the default --port
flag it's adding. Essentially, moving that logic from the daemon to the DSL.
Ok thanks for the direction. Just for my own understanding (still new to pact). When does the daemon spin up a new mock server? For every client or ever interaction?
@mefellows just trying to get a better understanding before I adjust the PR. Can you comment on the above question?
The mock service is started once per client, as in it will stay up for all the interactions. It will shutdown the mock service (and write the pact for the consumer/provider pair) when pact.Teardown()
is called or its process finishes. In theory, you could run multiple test suites in parallel and each could have its own mock.
Sorry for delay. I still want to specify a range of ports. We will have a few mock clients with different ports and I think putting that logic in the daemon makes more sense.
ENV variable PORTS_RANGE x-y
(can be comma separated or range). Then instead of having Find port to grab any random port, it then goes to that range to find an unused port.
with this change we can also add some retry logic on getting the port, because we have already ran into cases where it has grabbed a used port
Edit: Nevermind, starting to grok what you said
Since i started from scratch agian I opened a new pr: https://github.com/pact-foundation/pact-go/pull/49
When running pact-go in containers we want to expose minimal number of ports. Currently it randomly selects any port. This makes it impossible to expose ports in a container.
This change will add a CLI option to specify a port to deploy the mocking service on. This way we know exactly which port to expose.