This patch to libgo adds a timeout for the libgo tests.  The default is
60 seconds.  It can be changed by an argument to gotest, which would
normally be used as, e.g.,
  make GOTESTFLAGS="--timeout=120" check-target-libgo

Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline.  This is PR go/48242.

Ian

diff -r 86dce29de0ad libgo/go/testing/testing.go
--- a/libgo/go/testing/testing.go	Thu Mar 31 15:18:20 2011 -0700
+++ b/libgo/go/testing/testing.go	Thu Mar 31 15:33:09 2011 -0700
@@ -61,6 +61,7 @@
 	memProfile     = flag.String("test.memprofile", "", "write a memory profile to the named file after execution")
 	memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate")
 	cpuProfile     = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution")
+	timeout        = flag.Int64("test.timeout", 0, "if > 0, sets time limit for tests in seconds")
 )
 
 // Short reports whether the -test.short flag is set.
@@ -158,7 +159,9 @@
 	flag.Parse()
 
 	before()
+	startAlarm()
 	RunTests(matchString, tests)
+	stopAlarm()
 	RunBenchmarks(matchString, benchmarks)
 	after()
 }
@@ -241,3 +244,24 @@
 		f.Close()
 	}
 }
+
+var timer *time.Timer
+
+// startAlarm starts an alarm if requested.
+func startAlarm() {
+	if *timeout > 0 {
+		timer = time.AfterFunc(*timeout*1e9, alarm)
+	}
+}
+
+// stopAlarm turns off the alarm.
+func stopAlarm() {
+	if *timeout > 0 {
+		timer.Stop()
+	}
+}
+
+// alarm is called if the timeout expires.
+func alarm() {
+	panic("test timed out")
+}
diff -r 86dce29de0ad libgo/testsuite/gotest
--- a/libgo/testsuite/gotest	Thu Mar 31 15:18:20 2011 -0700
+++ b/libgo/testsuite/gotest	Thu Mar 31 15:33:09 2011 -0700
@@ -32,6 +32,7 @@
 keep=false
 prefix=
 dejagnu=no
+timeout=60
 while $loop; do
 	case "x$1" in
         x--srcdir)
@@ -83,6 +84,15 @@
 		dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
 		shift
 		;;
+	x--timeout)
+		timeout=$2
+		shift
+		shift
+		;;
+	x--timeout=*)
+		timeout=`echo $1 | sed -e 's/^--timeout=//'`
+		shift
+		;;
 	x-*)
 		loop=false
 		;;
@@ -357,7 +367,7 @@
 xno)
 	${GC} -g -c _testmain.go
 	${GL} *.o ${GOLIBS}
-	./a.out -test.short "$@"
+	./a.out -test.short -test.timeout=$timeout "$@"
 	;;
 xyes)
 	rm -rf ../testsuite/*.o

Reply via email to