Uncovering infinite loop in test

One of best working practice to improve unit tests coverage is to create failing test for each bug you encounter. It has some benefits, so I do this.

Here’s the deal: I’m working now on quite complex migration in my system and in some specific cases I got infinite loop. How can I detect infinite loop? So I could do something like:

test "should not fall in infitite loop" do
  // prepare conditions to infinite loop
  assert_no_infinite_loop do

I don’t know the solution, this is a open question.

Only thing I can think of is to create positive test with such conditions to, for instance, return nil. When everything is ok – I’ll have green test, when something is wrong – I’ll have never ending test ;)

ps. PhpUnit has something like @fast, @medium and @slow annotations which fails even successful tests if they take too long. But I guess it waits for the test to end…
pps. Maybe I should run it in separate thread and after some time kill it and fail test? Need to investigate this.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *