fix(test): Remove uses of t.FailNow in goroutines (#1978)
It is documented as illegal to use t.FailNow in a goroutine. Uses of require assertions call t.FailNow. This may have led to inconsistent test results as some assertions were subject to undefined behavior. Note that it is still allowed to call assert assertions in goroutines. See: https://pkg.go.dev/testing#T.FailNow
This commit is contained in:
1 parent
1be55b50c7
commit
f2fa1142d3
6 files changed
+86
-41
No files matched your search
@@ -9,6 +9,11 @@ import (
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type connMsg struct {
|
||||
msg []byte
|
||||
err error
|
||||
}
|
||||
|
||||
func Test_TestWsConn(t *testing.T) {
|
||||
t.Parallel()
|
||||
require, assert := require.New(t), assert.New(t)
|
||||
@@ -16,32 +21,35 @@ func Test_TestWsConn(t *testing.T) {
|
||||
ctx, cancelCtx := context.WithCancel(context.Background())
|
||||
clientConn, proxyConn := TestWsConn(t, ctx)
|
||||
|
||||
successfulRead := make(chan struct{})
|
||||
// Use msg channel so that we can use test assertions on the returned content.
|
||||
// It is illegal to call `t.FailNow()` from a goroutine.
|
||||
// https://pkg.go.dev/testing#T.FailNow
|
||||
readChan := make(chan connMsg)
|
||||
go func() {
|
||||
_, msg, err := proxyConn.Read(ctx)
|
||||
require.NoError(err)
|
||||
assert.Equal("client to proxy", string(msg))
|
||||
successfulRead <- struct{}{}
|
||||
readChan <- connMsg{msg, err}
|
||||
}()
|
||||
|
||||
err := clientConn.Write(ctx, websocket.MessageBinary, []byte("client to proxy"))
|
||||
require.NoError(err)
|
||||
|
||||
// Wait for read to verify success
|
||||
<-successfulRead
|
||||
msg := <-readChan
|
||||
require.NoError(msg.err)
|
||||
assert.Equal("client to proxy", string(msg.msg))
|
||||
|
||||
go func() {
|
||||
_, msg, err := clientConn.Read(ctx)
|
||||
require.NoError(err)
|
||||
assert.Equal("proxy to client", string(msg))
|
||||
successfulRead <- struct{}{}
|
||||
readChan <- connMsg{msg, err}
|
||||
}()
|
||||
|
||||
err = proxyConn.Write(ctx, websocket.MessageBinary, []byte("proxy to client"))
|
||||
require.NoError(err)
|
||||
|
||||
// Wait for read to verify success
|
||||
<-successfulRead
|
||||
msg = <-readChan
|
||||
require.NoError(msg.err)
|
||||
assert.Equal("proxy to client", string(msg.msg))
|
||||
|
||||
cancelCtx()
|
||||
}
|
||||
Reference in new issue
Block a user