Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice when it comes to testing "infinite loops"?

My basic logic is to have an infinite loop running somewhere and test it as best as possible. The reason for having an infinite loop is not important (main loop for games, daemon-like logic...) and I'm more asking about best practices regarding a situation like that.

Let's take this code for example:

module Blah
  extend self

  def run
     some_initializer_method
     loop do
        some_other_method
        yet_another_method
     end
  end
end

I want to test the method Blah.run using Rspec (also I use RR, but plain rspec would be an acceptable answer).

I figure the best way to do it would be to decompose a bit more, like separating the loop into another method or something:

module Blah
  extend self

  def run
     some_initializer_method
     do_some_looping
  end

 def do_some_looping
   loop do
     some_other_method
     yet_another_method
   end
 end
end

... this allows us to test run and mock the loop... but at some point the code inside the loop needs to be tested.

So what would you do in such a situation?

Simply not testing this logic, meaning test some_other_method & yet_another_method but not do_some_looping ?

Have the loop break at some point via a mock?

... something else?

like image 733
marcgg Avatar asked Apr 19 '11 14:04

marcgg


People also ask

How do you diagnose an infinite loop?

While most infinite loops can be found by close inspection of the code, there is no general method to determine whether a given program will ever halt or will run forever; this is the undecidability of the halting problem.

How can you avoid infinite loops in repetition structure?

Like other loops, to avoid an infinite loop, loop body must contain a statement that makes expression false. Statement can be simple or compound (multiple statements); if compound, they must be in braces. ***REMEMBER, posttest loops (do... while) always iterate at least once (unlike for and while)

How do you do infinite loops?

We can create an infinite loop using while statement. If the condition of while loop is always True , we get an infinite loop.


3 Answers

What might be more practical is to execute the loop in a separate thread, assert that everything is working correctly, and then terminate the thread when it is no longer required.

thread = Thread.new do   Blah.run end  assert_equal 0, Blah.foo  thread.kill 
like image 148
tadman Avatar answered Oct 14 '22 08:10

tadman


in rspec 3.3, add this line

allow(subject).to receive(:loop).and_yield 

to your before hook will simple yield to the block without any looping

like image 33
Rundong Gao Avatar answered Oct 14 '22 06:10

Rundong Gao


How about having the body of the loop in a separate method, like calculateOneWorldIteration? That way you can spin the loop in the test as needed. And it doesn’t hurt the API, it’s quite a natural method to have in the public interface.

like image 20
zoul Avatar answered Oct 14 '22 06:10

zoul