I am doing some unit tests where essentially I need an input stream to lock forever. Right now i'm using this to create input stream
InputStream in = new ByteArrayInputStream("".getBytes());
While it works for some time, while the input stream is read before the output stream (what I'm testing) is finished, causing all kinds of chaos.
Essentially I need this input stream to lock forever while reading. The only solution I can think of is to set up an InputStream with a massive buffer so that the other streams end, but this is really a hacky and fragile solution. I have a mockito, but I'm very new to this, and not sure if I can get away from mocking reading without mocking anything else.
Does anyone know of a better solution?
EDIT:
This is my new attempt. It works most of the time, but in other cases, the input stream dies early, which causes the output thread to die (this behavior is intentional). I canβt understand why this sometimes fails.
This is a general test in TestNG for simplicity.
protected CountDownLatch inputLatch; @BeforeMethod public void botSetup() throws Exception {
For the test, I use readLine() from botOut to get the appropriate number of lines. The problem is that when the output stream dies, readLine() blocks forever, which TestNG hangs. I tried a timeout with mixed results: most of the time it worked, but others could kill the tests, which took a little longer than usual, to check.
My only option is to simply not use threads for this kind of work. The output stream depends on the output queue, so I could just escape from it. The problem is that I am not actually testing writing to the stream, just what will be sent is what bothers me.