cjstehno / ersatz

🤖 A simulated HTTP server for testing client code with configurable responses.
https://cjstehno.github.io/ersatz
Apache License 2.0
47 stars 5 forks source link

Verify does not block for ordinary requests #99

Closed radarsh closed 4 years ago

radarsh commented 6 years ago

Calling server.verify(60, TimeUnit.SECONDS) doesn't have any effect if we're dealing with non-WebSocket requests. This is actually documented in the manual but in my opinion a misleading API.

Can Ersatz be enhanced to wait for expectations even in case of ordinary requests?

cjstehno commented 6 years ago

This feature was added in the last release specifically for web sockets support. Do you see some reason for this behavior with standard requests?

radarsh commented 6 years ago

So one scenario I was trying to test today was of a service which makes an asynchronous call to a backend API (which is mocked in my case using Ersatz). So if the verify was a blocking call with an optional timeout, I could wait until the expectation was eventually met. Otherwise, I have to resort to ugly sleeps after invoking my service. For example:

def "test service"() {
    given:
        server.expectations {
            post('/v0/backend') {
                called 1
                header CONTENT_TYPE, APPLICATION_JSON
                body(data, APPLICATION_JSON)
                responder {
                    code 202
                }
            }
        }
    and:
        def service = new Service(url: server.httpUrl)
    when:
        service.testMethod() // This method would return immediately, after triggering an async call to /v0/backend
    then:
        sleep 5000 // I have to resort to sleeping as server.verify() wouldn't block
        server.verify()
}
cjstehno commented 6 years ago

Honestly, I don't really support asynchronous calls in ersatz - it was a simplification I was able to make since I didn't really need it.

I know what needs to change (mostly), but I won't be able to look at this for a couple weeks.

radarsh commented 6 years ago

Thanks, there is no rush. Once again appreciate what you’re doing with this awesome mocking library.

cjstehno commented 6 years ago

There are issues with implementing this under the current architecture, so I am going to have to put this off until v2.0, which either be a rewrite or a major refactoring.

cjstehno commented 4 years ago

I have added this blocking timeout to v1.10, which will be released soon.