refactor(worker): Move tcp_proxy into its own package (#1458)

* 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>
This commit is contained in:
Louis RuchandJeff Mitchell authored and GitHub committed 2021-08-18 15:30:55 +02:00
1 parent c79daab68c
commit 31af49e76d
29 files changed
+1229 -773

No files matched your search

@@ -0,0 +1,47 @@
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()
}