-
Notifications
You must be signed in to change notification settings - Fork 1.7k
suite: fix deadlock in suite.Require()/Assert() #1535
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change.
Instead, you can fix the deadlock by making two versions of the T
method:
// T retrieves the current *testing.T context.
func (suite *Suite) T() *testing.T {
suite.mu.RLock()
defer suite.mu.RUnlock()
return suite._t()
}
func (suite *Suite) _t() *testing.T {
return suite.t
}
And then calling the internal version from Require
and Assert
, since they are already protected by the lock.
How? This would have never worked in the previous version right?
But just like
|
At v1.0 (which is difficult to require from a modern module) package kata_test
import (
"os"
"testing"
"github.com/stretchr/testify/suite"
)
type mySuite struct {
suite.Suite
}
func (suite *mySuite) TestSomething() {
require := suite.Require()
require.True(true)
}
func TestSuite(t *testing.T) {
m := &mySuite{}
m.SetT(t)
f, err := os.Create("testfile")
m.Require().NoError(err)
defer f.Close()
suite.Run(t, m)
} This code works today because So I think you are right, I can't contrive an example that is broken by this change, it's not a breaking change. Perhaps though the panic message should encompass something like: "Require must not be called before Run or SetT". |
As pointed out in issue stretchr#1520, if the suite is not initialised properly (buy calling the Run function), then calling suite.Require() or suite.Assert() will result in a deadlock. This commit fixes that by panicking if the suite is not initialised properly. This is justified because, the suite is intended to be triggered in the right way. If the user does not do that, this panic will nudge them in the right direction. It has to be a panic because, at this point, we don't have access to any testing.T context to gracefully call a t.Fail(). Also, these two functions are not expected to return an error. Fixes stretchr#1520
81e32a8
to
c41592b
Compare
Ohh got it. Thanks for the context! |
As pointed out in issue #1520, if the suite is not initialised properly (by calling the
Run
function), then callingsuite.Require()
orsuite.Assert()
will result in a deadlock.Changes
This commit fixes that by panicking if the suite is not initialised properly. This is justified because the suite is intended to be triggered in the right way. If the user does not do that, this panic will nudge them in the right direction.
It has to be a panic because, at this point, we don't have access to any
testing.T
context to gracefully call at.Fail()
. Also, these two functions are not expected to return an error.Related issues
Fixes #1520