go test -timeout 99999
Run Code Online (Sandbox Code Playgroud)
抛出这个无意义的错误
invalid value "99999" for flag -test.timeout:
time: missing unit in duration 99999
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?我正在使用go版本go1.3
"帮助"cli也没用.它说 -test.timeout=0: if positive, sets an aggregate time limit for all tests.但是,如果你去测试-test.timeout 99999,你会得到同样的错误
-test.timeout=0: if positive, sets an aggregate time limit for all tests
Run Code Online (Sandbox Code Playgroud)
pet*_*rSO 55
使用有效的time.ParseDuration输入.例如,
$ go test -timeout 300ms
$ go test -timeout 99999s
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)-timeout t如果测试运行的时间超过
t,panic.持续时间标志接受任何有效的输入
time.ParseDuration.Run Code Online (Sandbox Code Playgroud)func ParseDuration(s string) (Duration, error)ParseDuration解析持续时间字符串.持续时间字符串是可能签名的十进制数字序列,每个都带有可选的分数和单位后缀,例如"
300ms","-1.5h"或"2h45m".有效时间单位为"ns","us"(或"µs"),"ms","s","m","h".
Tig*_*ine 11
如果您只需要在一个测试中使用它,而您希望在超时时轻松失败,那么有一种简单的方法就是使用超时通道。
如果我怀疑测试会超时并且我仍然希望它失败,则使用超时通道。
因此,假设您的代码怀疑某些 goroutine 会死锁,并且您想确保您的测试失败。
为此,我在 goroutine 中运行实际测试,然后主 goroutine 坐在那里等待done通道完成或timeout完成。
func TestWithTimeOut(t *testing.T) {
timeout := time.After(3 * time.Second)
done := make(chan bool)
go func() {
// do your testing
time.Sleep(5 * time.Second)
done <- true
}()
select {
case <-timeout:
t.Fatal("Test didn't finish in time")
case <-done:
}
}
Run Code Online (Sandbox Code Playgroud)