What Is the Best Practice When It Comes to Testing "Infinite Loops"

rspec unit testing a method which has a infinite loop

Maybe you can move everything inside the while except the sleep to another method, then you write specs only for that method, I don't think you really need to test that your method has a while loop and a sleep (you don't need to test EVERY line of your code :P) but if you want to do that check this question, it has some things you can try What is the best practice when it comes to testing "infinite loops"?

How to unit test a method that runs into an infinite loop for some input?


How to unit test a method that runs into an infinite loop for some input?

You can test the nearly opposite: "How to unit test a method so that the method will not run longer than Xxxx milliseconds for some input". If this test fails you may have found a candidate with an infinite loop.

NUnit 2.5 has a TimeoutAttribute that makes a test fail if the test takes longer than the given amount of milliseconds.

Idom for infinite loops in PHP?

Monitoring applications

If you have a background process that monitors the state of your servers and sends an email if trouble occurs. It would have an infinite loop to repeatably check the servers (with some pause between iterations.)

Server listening for Clients

If you have a server script that listens to a socket for connections, it will loop infinitely, blocking while waiting for new clients to connect.

Video Games

Games usually have a "game loop" that runs once a frame, indefinitely.

Or... anything else that needs to keep running in the background with periodic checks.

How to debug, and protect against, infinite loops in PHP?

You could use a debugger such as xdebug, and walk through your code that you suspect contains the infinite loop.

Xdebug - Debugger and Profiler Tool for PHP

You can also set

max_execution_time

to limit the time the infinite loop will burn before crashing.

Integration test with infinite loop

In general a program should have a "natural" way to exit, infinite loops are a code smell per se... I would look for some approach like this:

boolean keepRunning = true; //even better as private field, then your class can get a "public void stop() { keepRunning = false; } method or something similar...
while(keepRunning) {
//do whatever you need
}

This can be controlled by your test, and in the productive code, it can be used to kill the app when some kill-event arrives cleaning up everything...

Is while (true) with break bad programming practice?

There is a discrepancy between the two examples. The first will execute the "do something" at least once every time even if the statement is never true. The second will only "do something" when the statement evaluates to true.

I think what you are looking for is a do-while loop. I 100% agree that while (true) is not a good idea because it makes it hard to maintain this code and the way you are escaping the loop is very goto esque which is considered bad practice.

Try:

do {
//do something
} while (!something);

Check your individual language documentation for the exact syntax. But look at this code, it basically does what is in the do, then checks the while portion to see if it should do it again.



Related Topics



Leave a reply



Submit