go - time.After 在指定时间后没有被触发
问题描述
我正在使用go1.10 darwin/amd64并尝试使用以下代码片段在运行时为我的测试设置自定义超时。
我正在使用命令执行测试,将 Go 的超时设置为 11m 并将测试的恐慌计时器设置为 5m,并观察到 Go 超时值被触发而不是我的。
go test github.com/foo -run TestMod -v --race -timeout 11m
...
...
*** Test killed with quit: ran too long (12m0s).
代码片段:
// Returns time.Duration from given string
// Default return value: "2h0m0s"
func GetDuration(timeoutStr string) time.Duration {
// Default timeout to 2 hours
durationToReturn := (2 * time.Hour)
pattern, _ := regexp.Compile("^([0-9]+)([mhd])$")
// Calculates only if valid pattern exists
if pattern.MatchString(timeoutStr) {
match := pattern.FindStringSubmatch(timeoutStr)
timeoutVal, err := strconv.Atoi(match[1])
if err != nil {
return durationToReturn
}
timeoutDuration := time.Duration(timeoutVal)
switch match[2] {
case "m":
durationToReturn = timeoutDuration * time.Minute
case "h":
durationToReturn = timeoutDuration * time.Hour
case "d":
durationToReturn = timeoutDuration * (time.Hour * 24)
}
}
return durationToReturn
}
// Starts timeout trigger based on given value in suiteData.Timeout
func StartTimeoutTimer() {
timeoutStr := "5m"
go func() {
timeoutDuration := GetDuration(timeoutStr)
fmt.Printf("Setting timeout of %v from %v\n", timeoutDuration, time.Now())
<-time.After(timeoutDuration)
fmt.Printf("Timeout happened at %v\n", time.Now())
panic("Test timed out..")
}()
}
func TestMain(m *testing.M) {
StartTimeoutTimer()
for {
}
}
解决方案
不能保证新的 goroutineStartTimeoutTimer()
会被执行。StartTimeoutTimer()
创建新的 goroutine 后立即返回。
我想你想要的是
done := make(chan struct{})
go func(){
testFunc()
close(done)
}()
select {
case <- time.After(duration):
panic("...")
case <- done:
}
推荐阅读
- json - 我将如何打印 JSON 值
- xml - 为什么 Firefox 显示:XML Parsing Error: junk after document element?
- c++ - 如何解决:无法获取集合中元素的索引
- html - 类型错误时不存在 Angular 属性
- arrays - 如何在 TypeScript 中组合两个不同类型的对象?
- docusignapi - 收件人交付失败 - Webhook 和通过 API 更正/重新发送
- javascript - Google Chrome 控制台中 XPath $x() 的替代品
- ruby-on-rails - 使用 Apple 登录不会重定向回我在 Google Chrome 上的网站
- python - while 循环永远不会结束,我不知道如何停止它
- reactjs - 我不断收到错误:找不到模块'webpack-CLI/bin/config-yargs'