Search code examples
javajunitinputstreamretry-logic

How to unit test that a new InputStream was used during a retry?


I have a couple of methods that send HTTP requests to upload an input stream to S3. If the request to S3 fails, there is a retry strategy to attempt to send the request again on certain status codes. Since we cannot guarantee that the input stream has not been partially read from a previous request, we must provide a new input stream when retrying.

The object class containing the input stream looks something like this:

public class CustomRequest {
private InputStream stream;
private InputStreamProvider inputStreamProvider;
}

where InputStreamProvider is a functional interface created for the purpose of supplying a brand new input stream in the case of a retry and looks something like this:

public interface InputStreamProvider {
InputStream getInputStream();
}

Each CustomRequest can either have an InputStream which means we do not support retrying OR an InputStreamProvider for which we do support retries.

My question is how would I unit test to ensure I am in fact providing a new input stream when retrying with InputStreamProvider.getInputStream()?


Solution

  • You're asking the wrong question.

    By this I mean that you're attempting to test the wrong thing. It doesn't matter if an input stream is reused, it only matters whether the input stream provides the correct data. That is what you need to test.

    Read the stream and verify the output, i.e. the content. You don't care, or test, how the stream is implemented, only whether the stream acts according to the API contract.

    What you seem concerned about testing is that actions on one stream doesn't affect the data returned by another stream. So test that. Get a stream, read some of it and verify content. Get another stream, read a different amount from it and verify content. Read some more from first stream, then from second stream. Keep multiplexing and simply verify that read of one stream never affects the data returned by the other stream.