From: Nathaniel Talbott Date: 2004-01-09T01:11:35+09:00 Subject: Re: testunit and infinite loop On Jan 8, 2004, at 10:49, Bil Kleb wrote: > My searching abilities are failing me today... > > How does one detect an infinite loop bug with testunit? > > We are writing code test-first and we have found a test which causes > an (erroneous) infinite loop, but we do not know how to write an > *automated* test to expose it -- i.e., we do not have the luxury > of using ctrl-c when this piece of testing code gets inserted into > our build/test robot. > > We have been considering the following hack: > > a) spawn two threads, one calling the code with the infinite > loop bug and another that merely sleeps for a period of > time much longer that the first task should ever take if > the bug weren't there. > > b) raise an exception if the sleeping thread returns before > the other one. Could you just use timeout? require 'test/unit' require 'timeout' class TC < Test::Unit::TestCase def test_it assert_nothing_raised do timeout(5) do loop{sleep(1)} end end end end ntalbott@jacob:~/sandbox$ ruby t.rb Loaded suite t Started F Finished in 5.186834 seconds. 1) Failure: test_it(TC) [t.rb:10]: Exception raised: Class: Message: <"execution expired"> ---Backtrace--- /usr/local/lib/ruby/1.9/timeout.rb:42:in `test_it' t.rb:8:in `loop' t.rb:8:in `test_it' t.rb:7:in `timeout' /usr/local/lib/ruby/1.9/timeout.rb:55:in `timeout' t.rb:7:in `test_it' t.rb:6:in `assert_nothing_raised' t.rb:10:in `test_it' --------------- 1 tests, 1 assertions, 1 failures, 0 errors Please let me know if I'm misunderstanding your question. HTH, Nathaniel <:((><