* refactor(worker): Move tcp_proxy into its own package This refactors: - the tcp_proxy.go into worker/proxy/tcp - add a missing unit test for the HandleTcpProxyV1 - refactors session.go into worker/session, needed to avoid import cycle - adds a baseline for a mock controller session client - go lint fixes * Add sleep between spinning listener goroutine and dialing Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"nhooyr.io/websocket"
|
|
)
|
|
|
|
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)
|
|
|
|
successfulRead := make(chan struct{})
|
|
go func() {
|
|
_, msg, err := proxyConn.Read(ctx)
|
|
require.NoError(err)
|
|
assert.Equal("client to proxy", string(msg))
|
|
successfulRead <- struct{}{}
|
|
}()
|
|
|
|
err := clientConn.Write(ctx, websocket.MessageBinary, []byte("client to proxy"))
|
|
require.NoError(err)
|
|
|
|
// Wait for read to verify success
|
|
<-successfulRead
|
|
|
|
go func() {
|
|
_, msg, err := clientConn.Read(ctx)
|
|
require.NoError(err)
|
|
assert.Equal("proxy to client", string(msg))
|
|
successfulRead <- struct{}{}
|
|
}()
|
|
|
|
err = proxyConn.Write(ctx, websocket.MessageBinary, []byte("proxy to client"))
|
|
require.NoError(err)
|
|
|
|
// Wait for read to verify success
|
|
<-successfulRead
|
|
|
|
cancelCtx()
|
|
}
|