mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
testing: add -test.timeout option.
Since Go code can deadlock, this lets a testsuite driver set a time limit for the test to run. This is simple but imperfect, in that it only catches deadlocks in Go code, not in the runtime scheduler. R=r, rsc, iant2 CC=golang-dev https://golang.org/cl/4326048
This commit is contained in:
parent
3907031358
commit
554082d6b1
4 changed files with 32 additions and 2 deletions
|
|
@ -61,6 +61,7 @@ var (
|
|||
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 @@ func Main(matchString func(pat, str string) (bool, os.Error), tests []InternalTe
|
|||
flag.Parse()
|
||||
|
||||
before()
|
||||
startAlarm()
|
||||
RunTests(matchString, tests)
|
||||
stopAlarm()
|
||||
RunBenchmarks(matchString, benchmarks)
|
||||
after()
|
||||
}
|
||||
|
|
@ -241,3 +244,24 @@ func after() {
|
|||
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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue