Files
threshold/internal/servers/worker/proxy/testing_test.go
T
Johan Brandhorst-Satzkorn f2fa1142d3 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
2022-04-07 09:26:34 -04:00

56 lines
1.3 KiB
Go

package proxy
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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)
ctx, cancelCtx := context.WithCancel(context.Background())
clientConn, proxyConn := TestWsConn(t, ctx)
// 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)
readChan <- connMsg{msg, err}
}()
err := clientConn.Write(ctx, websocket.MessageBinary, []byte("client to proxy"))
require.NoError(err)
// Wait for read to verify success
msg := <-readChan
require.NoError(msg.err)
assert.Equal("client to proxy", string(msg.msg))
go func() {
_, msg, err := clientConn.Read(ctx)
readChan <- connMsg{msg, err}
}()
err = proxyConn.Write(ctx, websocket.MessageBinary, []byte("proxy to client"))
require.NoError(err)
// Wait for read to verify success
msg = <-readChan
require.NoError(msg.err)
assert.Equal("proxy to client", string(msg.msg))
cancelCtx()
}