4

How could I time out hanging tests in a Perl test harness?

I tried using the Test::Timer module, but I can't seem to make it link up nicely with the TAP::Harness in order to have an embedded timeout function for each test. Plus, I don't want to test if a bit of code takes x time to complete, I just want to run my tests and timeout in case they hung for any reason.

hmatt1
  • 4,939
  • 3
  • 30
  • 51

1 Answers1

4

There was a similar question on PerlMonks recently.

Install Time::Limit.

This module will allow you to set time limits for individual test files:

use Test::More;
use Time::Limit "30";   # 30 seconds, quote marks are necessary!

Or set an overall time limit for running the entire test suite:

prove -MTime::Limit=120 t/*.t

If you're using forkprove instead of prove, then you need the time limiter to kill the entire process group:

forkprove -MTime::Limit=-group,120 t/*.t
tobyink
  • 13,478
  • 1
  • 23
  • 35
  • That doesn't seem to work for me. I should mention I'm trying to use it on a windows machine. What I've got so far is `sub test_with_timeout { my $current_test=shift || die "Must provide a test"; my $harness = shift || die "Must provide a harness"; my $timeout=shift || -1; $SIG{ALRM} = sub { print "Timeout after $timeout seconds\n"; die; }; # print "Timeout is $timeout seconds\n\n"; alarm($timeout); my $result=$harness->runtests($current_test); alarm(0); return $result; }` – Cristian Vasile Jul 28 '14 at 10:00