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