While passing through code in our project I came across a web method that had this code at the end of it:
thread.sleep(6000);
return true;
Now, this was done so the jQuery ajax call from the client gets delayed and the ajax animation will show for a little bit longer.
This is very wrong in my eyes. There shouldn't be this kind of connection between UI and server side. If he wants the animation to take longer he can use the setTimeOut
function in the client side.
Here is my problem: how can I explain to the programmer why this is so wrong? Not just because the client/server thing, but why ever call thread.sleep
on a website?
Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.
Use it whenever you need a long pause in your operations. If the spec says 'now wait at least 10 seconds before continuing', then call sleep(10000).
Using Thread. sleep() frequently in an automation framework is not a good practice. If the applied sleep is of 5 secs and the web element is displayed in 2 secs only, the extra 3 secs will increase the execution time. And if you use it more often in the framework, the execution time would increase drastically.
Alternative: use wait() and notify() Now we have to refactor our test as well. After we start the counter we need to block the thread running the test until the counting is over. Remember, the counting happens in a different thread.
While delaying:
these are all expensive resources on server
Because:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With