LukeMathWalker / wiremock-rs

HTTP mocking to test Rust applications.
Apache License 2.0
607 stars 69 forks source link

Cannot stop the server #142

Open zvolin opened 4 months ago

zvolin commented 4 months ago

Hey, as in issue title. I wanted to test that a service gracefully handles networking issues like server being temporarily unreachable and wanted to do that by dropping the MockServer, however this loop actually never finishes:

use std::time::Duration;

use tokio::{net::TcpStream, time::sleep};
use wiremock::MockServer;

#[tokio::main]
async fn main() {
    let server = MockServer::start().await;
    let addr = *server.address();

    drop(server);

    loop {
        if TcpStream::connect(addr).await.is_err() {
            break;
        }
        sleep(Duration::from_millis(200)).await;
        println!("retrying");
    }
}
zvolin commented 4 months ago

Wanted to work on this issue and it turned out that this bug is actually an undocumented feature. I found this comment too

/// `wiremock`'s pooling is designed to be an invisible optimisation: users of the crate, if
/// we are successful, should never have to reason about it.

Would you consider a PR which mentions about the difference in behavior on MockServer::start and MockServer::builder? Builder avoids pooling and was what I was actually looking for in this particular test, but I had to find it out by reading code