1

My deployed server has sometimes long response times, while working and developing at localhost all calls are really fast.

This has made my application enter unexpected behaviour once deployed a few times due to problems with resource loading taking too long.

I'd like to simulate in my local tests the bad connection with my real server, therefore I want to add a random delay to every request-response and my first thought was to use Thread.sleep in the servlet:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    //add delay before processing request
    if (DELAY > 0){
        int delay;
        if (RANDOMIZE){
            delay = Random.nextInt(DELAY);
        } else {
            delay = DELAY;
        }
        try {
            Thread.sleep(delay);
        } catch (InterruptedException e1) {
            logger.error(e1);
        }
    }
...

However I have read that one should not use Thread.sleep() inside a servlet, but the context of such discouragement and their solutions are drastically different from my case, can I use thread.sleep() in this context?

EDIT: This is of course only for local and for the client to be strained a bit in the local tests... I just want to simulate the bad network I've encountered in reality!

Daren
  • 3,337
  • 4
  • 21
  • 35
  • 1
    Based on your edit, you might be interested in this similar question [Network tools that simulate slow network connection](http://stackoverflow.com/questions/1094760/network-tools-that-simulate-slow-network-connection) – Roman Jan 11 '13 at 13:41
  • I'd accept your answer if it had been an answer, thanks. – Daren Jan 29 '13 at 14:27
  • Glad I could help. If you think your question is similar enough to the question I linked to, it would be better to start a vote to close it as a duplicate. – Roman Jan 29 '13 at 14:35
  • good enough. flagged as duplicate. – Daren Jan 29 '13 at 16:10

2 Answers2

4

I think this whole approach is flawed. I wouldn't introduce a random delay (how are you going to repeat test cases?). You can introduce a Thread.sleep(), but I wouldn't. Would this be in your production code ? Is it configurable ? What happens if it's accidentlally turned on in production ?

I would rather set up a test server with the exact characteristics of your production environment. That way you can not only debug effectively, but build a regression test suite that will allow you to develop effectively, knowing how the application will perform in production.

Perhaps the one concession to the above is to introduce network delays (as appropriate) between client and server if your users are geographically diverse. That's often done using a hardware device on the network and wouldn't affect your code or configuration.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • This is of course a LOCAL test configuration, the production configuration file has delay = 0 and this feature will probably be removed in the future, it is just to be kept up untill the client can handle varied response times smoothly. Your other solutions involve extra money (over 1500€ for the device!) and time, and heck! I'm sure this can be simulated via software, and I'm wondering about the best way to do this. – Daren Jan 11 '13 at 11:47
  • 1
    Your condescending answer has blocked any other possible real answer to my problem, I guess my crime was being in a small (2-man developer team) start-up with very little budget. Maybe for next time I need to be more clear and either tell my live or explicitly ask for real answers inside the described scenario. – Daren Jan 11 '13 at 12:09
  • Thread.sleep is discouraged for the very reason you want to use it, it will lock up and slow down your server / requests. As this is what you want to simulate, by all means go ahead. – md_5 Jan 11 '13 at 12:11
  • Ok, thanks, I want to simulate a bad network, not a bad server... but this seems the best choice so far without using up huge resources to solve the problem. I'm sure there are better solutions without reaching the 1,5k€ hardware device for localhost testing inside a private network, but I certainly don't know them. – Daren Jan 11 '13 at 12:27
  • @Daren - the above isn't condescending and it certainly doesn't block any further answers. I'm sure the SO community will provide other answers as/when they see fit and you can vote/accept as appropriate – Brian Agnew Jan 11 '13 at 13:26
  • @BrianAgnew It does not block further answers, but this question will not be seen by many anymore so the chances of getting an answer I can use have drastically diminished. It is condescending because simulating random network latency has no impact on test cases unless you try hard to reflect it (tests might run slower). It is specified in my question that this will be used in my local(host) tests, and he comes and asks me (tone there?) if this will be turned on in production... and then goes to excellent professional solutions well outside the scope of the question or my means. – Daren Jan 11 '13 at 14:14
0

I did this to get delay :

response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
    out.println("<meta http-equiv=\"Refresh\" content=\"3;url=home.jsp/\">");
}

Remember that in content=\"3;url=home.jsp/\", 3 is the delay seconds and home.jsp is the page you want to go to after the given seconds.

yashas123
  • 270
  • 5
  • 23