Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Simulate connect delay #19

Open
mre opened this issue Aug 20, 2020 · 4 comments
Open

Simulate connect delay #19

mre opened this issue Aug 20, 2020 · 4 comments

Comments

@mre
Copy link

mre commented Aug 20, 2020

Hey,

thanks for the crate.

I'm building a link checker and I need a way to simulate a connect delay.
Here's a little snippet to show what I tried:

    #[tokio::test]
    async fn test_timeout() {
        let mock_server = MockServer::start().await;
        let delay = Duration::from_secs(30);
        let template = ResponseTemplate::new(200).set_delay(delay.clone());
        Mock::given(method("GET"))
            .respond_with(template)
            .mount(&mock_server)
            .await;

        let res = get_checker(false, HeaderMap::new())
            .check(&Url::parse(&mock_server.uri()).unwrap())
            .await;
        assert!(matches!(res, Status::Timeout));
    }

get_checker is just a wrapper around reqwest.

This didn't work. It's obvious, in hindsight, because I was mixing up the response delay with a connect delay.
Any way I could test that right now? Maybe there could be another parameter set_connect_delay for that?

@LukeMathWalker
Copy link
Owner

Hey @mre!

Right now I don't believe there is a way for you to simulate a connection delay: I would imagine that being a set_connect_delay method on the MockServer itself more than on a specific Mock/ResponseTemplate as set_delay is right now.
To be honest, I am not even sure how to simulate it but I'd be keen to support it - any idea you have on the topic that I might go off and research a bit?

@mre
Copy link
Author

mre commented Aug 21, 2020

Hey, thanks for the feedback. You're right, the MockServer might not be the right place. I have to say working with the ResponseTemplate was really fun, so if you can I'd add it there.
I didn't find much, but this disussion on Tokio might be a starting point.

@pawroman
Copy link

pawroman commented Oct 16, 2020

This is a very useful feature and I'd like to help here.

I have done some digging into this, and I think we could simulate a connect delay quite easily. Basically the idea is to delay (e.g. by sleeping) each of the accept calls on the server socket. This would most likely be perceived as a "connect delay" by most HTTP clients, since the TCP connection would not be established for some time.

I would have to confirm this by poking around with a toy HTTP server. Tools like wget and curl let you specify the connect timeout, so the hypothesis can be confirmed experimentally.

@LukeMathWalker
Copy link
Owner

I was under the impression that adding a delay on accept calls would have not tricked HTTP clients, but, alas, I have not verified it. Would you like to give it a go @pawroman and let us know the outcome?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants