timeout

3,083 views
Skip to first unread message

Eisenberger Tamás

unread,
Mar 16, 2012, 10:57:17 AM3/16/12
to Google C++ Testing Framework
Hy!

Can I use googletest to test for infinite loops?
Defining a timeout may be a solution, but no such function exists in
googletest...

Any ideas how to write such test?

Thanks!
--
Eisenberger Tam�s <ta...@eisenberger.hu>

Manuel Klimek

unread,
Mar 18, 2012, 11:13:36 AM3/18/12
to Eisenberger Tamás, Google C++ Testing Framework
Hi Tamas,

in general you cannot test that a loop is infinite
(http://en.wikipedia.org/wiki/Halting_problem).

Cheers,
/Manuel

On Fri, Mar 16, 2012 at 3:57 PM, Eisenberger Tamás <ta...@eisenberger.hu> wrote:
> Hy!
>
> Can I use googletest to test for infinite loops?
> Defining a timeout may be a solution, but no such function exists in
> googletest...
>
> Any ideas how to write such test?
>
> Thanks!
> --

> Eisenberger Tamás <ta...@eisenberger.hu>

Joey Oravec

unread,
Mar 27, 2012, 10:22:55 AM3/27/12
to Manuel Klimek, Eisenberger Tamás, Google C++ Testing Framework
I think it's reasonable to reduce that problem to an ASSERT_RUNTIME() and say "the function must return with X seconds, or else it's broken". But that's challenging to implement. I see two problems:

a. Once a path of execution makes an API call, you're busy running that function (possibly an infinite loop) and can't do anything until the function returns. So you'd need two separate paths of execution: one to monitor a timeout, the other to execute the code-under-test. Gtest does not have anything built-in for this today.

b. If the timeout expires then what do you do? If you ran the test in a separate thread you could try killing the thread, but that's generally unsafe and may leave your state trashed within the process. If you ran the test in a separate process then each test can be truly isolated. Gtest does not have anything built-in for this today.

I do timeout tests today with an out-of-tree patch that runs each test within a separate process. This way I'm able to monitor the time, terminate the entire process if a timeout is exceeded, and have no impact on subsequent tests. The separate-process portion is posted here:


The patch has been discussed but has not been accepted. Maybe it's time to reconsider, because it could enable several features like safely terminating a test that exceeds some timeout.

-joey

2012/3/18 Manuel Klimek <kli...@google.com>

Robert Weber

unread,
Jul 9, 2013, 5:18:46 PM7/9/13
to googletes...@googlegroups.com
There is a semi-simple way to do this with std::future and std::thread..  See example :

   ClassWithLoop object();
   std::promise<bool> promisedFinished;
   auto futureResult = promisedFinished.get_future();
   std::thread([](std::promise<bool>& finished, ClassWithLoop & obj) {
      obj.LoopUnderTest();
      finished.set_value(true);
   }, std::ref(promisedFinished), std::ref(object)).detach();
  
   EXPECT_TRUE(futureResult.wait_for(std::chrono::milliseconds(100))!= std::future_status::timeout);

Simple!


On Friday, 16 March 2012 08:57:17 UTC-6, u-foka wrote:
Hy!

Can I use googletest to test for infinite loops?
Defining a timeout may be a solution, but no such function exists in
googletest...

Any ideas how to write such test?

Thanks!
--
Eisenberger Tam�s <ta...@eisenberger.hu>

Robotic Brain

unread,
Jun 25, 2014, 11:56:33 AM6/25/14
to googletes...@googlegroups.com
Hi,

I would really like to see "Test Sandboxing" implemented in google test.
I have to call some library functions, which spawn some junk threads I can't get rid off, thus trashing my environment. (gotta love Cocoa, eh?) The perfect solution would be to run the tests in question in another process.

The sandbox would make checking for timeouts very easy too.

Reply all
Reply to author
Forward
0 new messages