Assert that outbound client method is called with X while serving concurrent inbound requests #5753
-
BackgroundRecently, we shipped a bug to one of our APIs. While serving concurrent requests, one of the classes within the server was sharing state across requests/responses and asking the backend for incorrect data. More concretely, the server gets concurrent requests for X, Y, and Z. Because of the shared state in the client class making outbound calls, for the request for X, we could see that sometimes we were asking the backend for Y instead of X. The fix was to remove the shared state, but I can't help but think there is a property-based test to be had here. Things triedI've got fast-check and supertest working with jest, but I'm trying to figure out how to property-test this such that for a single request/response, we are calling the relevant client method with the appropriate arguments given what the request to the server was for and given concurrent requests. Of course, with jest, you can say that the mocked client method was (at some point) called with the correct argument, but I'm trying to figure out how to correlate a request with one of those mocked client method calls. Since we're using otel tracing, I thought I could use baggage, but it turns out jest uses a different global store than otel, so the baggage is not propagating as it does outside of a test context. Any ideas? Thanks for the great tool! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I got it to work via a more narrow unit test. I just wanted to see it work for the whole request/response cycle. |
Beta Was this translation helpful? Give feedback.
-
I don't quite understand your architecture and the roles of client, backend, server and API. Presumably, you have some JavaScript application since you want to test it with fast-check. But it's not running in the browser, since it's accepting HTTP requests. So your application is some NodeJS server, right? But when you say "backend" you mean some other service, that this NodeJS server is talking to, right? But what do mean with client/client class? |
Beta Was this translation helpful? Give feedback.
To be honest, given how easy it was to write the test at the client class scope, I think I could go back and figure it out now.