Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: testing: consider having timeouts in test binaries be recoverable/handled #44929

Open
dprotaso opened this issue Mar 10, 2021 · 2 comments
Labels
Projects
Milestone

Comments

@dprotaso
Copy link

@dprotaso dprotaso commented Mar 10, 2021

The way timeouts are thrown cause the binary to exit immediately

go/src/testing/testing.go

Lines 1740 to 1744 in 4d608eb

m.timer = time.AfterFunc(*timeout, func() {
m.after()
debug.SetTraceback("all")
panic(fmt.Sprintf("test timed out after %v", *timeout))
})

This circumvents T.Cleanup and any cleanup logic that might have been in TestMain. In order to guarantee setup/teardown occurs it has to be done outside the go test process. Thus either a testing script or the use of go test -exec xprog

The challenge with these workarounds is you lose the simplicity of invoking go test.

Thus my ask is

  1. when a timeout occurs run T.Cleanup functions
  2. considering surfacing the timeout as a panic but allow the ability to handle that panic

My second point could be accomplished by having the panic be surfaced by M.Run() and could be handled in the following way

func TestMain(m *testing.M) {
   os.Exit(run(m))
}

func run(m *testing.M) (exitCode int) {
  setup()
  defer cleanup()
  exitCode = m.Run()
}
@dprotaso dprotaso changed the title consider having timeouts in test binaries exit gracefully consider having timeouts in test binaries be recoverable/handled Mar 10, 2021
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 10, 2021

Note that the t.Deadline method was introduced specifically so that tests could arrange their own, earlier, timeouts.

@ianlancetaylor ianlancetaylor changed the title consider having timeouts in test binaries be recoverable/handled testing: consider having timeouts in test binaries be recoverable/handled Mar 10, 2021
@ianlancetaylor ianlancetaylor changed the title testing: consider having timeouts in test binaries be recoverable/handled proposal: testing: consider having timeouts in test binaries be recoverable/handled Mar 10, 2021
@gopherbot gopherbot added this to the Proposal milestone Mar 10, 2021
@gopherbot gopherbot added the Proposal label Mar 10, 2021
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Mar 10, 2021
@dprotaso
Copy link
Author

@dprotaso dprotaso commented Mar 10, 2021

Yeah - I'm aware of the deadline per test.

My main concern is the setup and teardown that's called out in the TestMain docs

It is sometimes necessary for a test program to do extra setup or teardown before or after testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Incoming
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants