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:
Johan Brandhorst-Satzkorn authored and GitHub committed 2022-04-07 09:26:34 -04:00
1 parent 1be55b50c7
commit f2fa1142d3
6 files changed
+86 -41

No files matched your search

+11 -4
View File
@@ -31,10 +31,10 @@ func TestHandleTcpProxyV1(t *testing.T) {
defer l.Close()
var endpointConn net.Conn
var endpointErr error
ready := make(chan struct{})
go func() {
endpointConn, err = l.Accept()
require.NoError(err)
endpointConn, endpointErr = l.Accept()
defer endpointConn.Close()
ready <- struct{}{}
@@ -71,13 +71,20 @@ func TestHandleTcpProxyV1(t *testing.T) {
UserClientIp: net.ParseIP("127.0.0.1"),
}
// Use error channel so that we can use test assertions on the returned error.
// It is illegal to call `t.FailNow()` from a goroutine.
// https://pkg.go.dev/testing#T.FailNow
errChan := make(chan error)
go func() {
err = handleProxy(ctx, conf)
require.NoError(err)
errChan <- handleProxy(ctx, conf)
}()
t.Cleanup(func() {
require.NoError(<-errChan)
})
// wait for HandleTcpProxyV1 to dial endpoint
<-ready
require.NoError(endpointErr)
netConn := websocket.NetConn(ctx, clientConn, websocket.MessageBinary)
// Write from endpoint to client
+17 -9
View File
@@ -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()
}