Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6194921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:22:39+00:00 2026-05-24T03:22:39+00:00

I’m doing some unit tests where essentially I need the input stream to block

  • 0

I’m doing some unit tests where essentially I need the input stream to block forever. Right now I’m using this to construct the input stream

InputStream in = new ByteArrayInputStream("".getBytes());

While it works some of the time, other times the input stream is read before the output stream (what I’m testing) is finished, causing all sorts of havoc.

Essentially I need this input stream to block forever when read. The only solution I can think of is to setup the InputStream with a massive buffer so that the other threads finish, but thats a really hackish and brittle solution. I do have mockito but I’m very new at it and not sure if I can get away with only mocking read without mocking anything else.

Does anyone know of a better solution?


EDIT:

This is my new attempt. It works most of the time, but other times the input thread dies early which causes the Output Thread to die (that behavior is intentional). I can’t seem to figure out though why this would sometimes fail.

This is the general test under TestNG simplified for clarity.

    protected CountDownLatch inputLatch;

    @BeforeMethod
    public void botSetup() throws Exception {
            //Setup streams for bot
            PipedOutputStream out = new PipedOutputStream();
            //Create an input stream that we'll kill later
            inputLatch = new CountDownLatch(1);
            in = new AutoCloseInputStream(new ByteArrayInputStream("".getBytes()) {
                    @Override
                    public synchronized int read() {
                            try {
                                    //Block until were killed
                                    inputLatch.await();
                            } catch (InterruptedException ex) {
                                    //Wrap in an RuntimeException so whatever was using this fails
                                    throw new RuntimeException("Interrupted while waiting for input", ex);
                            }
                            //No more input
                            return -1;
                    }
            });
            Socket socket = mock(Socket.class);
            when(socket.getInputStream()).thenReturn(in);
            when(socket.getOutputStream()).thenReturn(out);

            //Setup ability to read from bots output
            botOut = new BufferedReader(new InputStreamReader(new PipedInputStream(out)));
            ...
    }

    @AfterMethod
    public void cleanUp() {
            inputLatch.countDown();
            bot.dispose();
    }

For the test I use readLine() from botOut to get the appropriate number of lines. The issue though is that when the output thread dies, readLine() blocks forever which hangs up TestNG. I’ve tried a timeout with mixed results: most of the time it would work but others it would kill tests that just took a little longer than normal to test.

My only other option is to just not use streams for this kind of work. The output thread relies on an output queue, so I could just run off of that. The issue though is that I’m not actually testing writing to the stream, just what is going to be sent, which does bother me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:22:40+00:00Added an answer on May 24, 2026 at 3:22 am

    There doesn’t seem to be any reliable way to do this. My code in the question only works sometimes, @Moe’s doesn’t work at all, @Ed’s suggesting is what I was origionally doing, and @SJuan’s is sort of what I’m already doing.

    There just seems to be too much stuff going on. The input stream I give to the class is wrapped in a InputStreamReader, then a Buffered reader. Suggestions for other streams inside of other streams just further complicate the issue.

    To fix the problem I did what I should of done origionally: Create a factory method for the InputThread (the thread that actually does the reading), then override in my testing. Simple, effective, and 100% reliable.

    I suggest anyone that runs into this problem to first try and override the part of your program that does the reading. If you can’t then the code I posted is the only semi-reliable code that works in my situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.