LukeMathWalker / LukeMathWalker/wiremock-rs

Multiple mock servers initialisation glitch

Open
#156 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
799
Forks
91
PR merge metrics
No merged PRs in 30d

Description

When using multiple mock servers, the first one should still be referenced when the second one is to be built. Otherwise, the second one may take the same port as the first one.

Here's a simple example demonstrating the issue:

```rust
use wiremock::MockServer;

#[tokio::main]
async fn main() {
mock_server().await;
mock_server().await;
}

async fn mock_server() {
let server = MockServer::start().await;
println!("{}", server.uri());
}
```
When executed, it prints the following output:
```
http://127.0.0.1:53089
http://127.0.0.1:53089
```
Note that both servers use the same port.

However, if the `mock_server` function returns the server itself and this data is now own by the `main` function, then the ports are different:
```rust
use wiremock::MockServer;

#[tokio::main]
async fn main() {
let server1 = mock_server().await;
let server2 = mock_server().await;
}

async fn mock_server() -> MockServer {
let server = MockServer::start().await;
println!("{}", server.uri());

server
}
```
=>
```
http://127.0.0.1:53109
http://127.0.0.1:53111
```

As per my tests, this is always reproduced on my machine (Windows 10). I'm not sure whether this is a bug, or if [the documentation](https://docs.rs/wiremock/0.6.3/wiremock/struct.MockServer.html#method.start) lacks this information. It simply states the following:
> Each instance of MockServer is fully isolated: start takes care of finding a random port available on your local machine which is assigned to the new MockServer.
>
> You should use one instance of MockServer for each REST API that your application interacts with and needs mocking for testing purposes.

If this is the desired behavior, then it should be included that all mounted servers should still be referenced for the random port finding code to work.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with MockServer::start and the linked API documentation, then reproduce both examples from the issue to compare server lifetimes and assigned ports. Determine whether the behavior requires an implementation change or clearer documentation, and verify that the chosen outcome is covered by the documented behavior or a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.