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:
1 parent
c79daab68c
commit
31af49e76d
29 files changed
+1229
-773
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/sdk/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
// TestWsConn creates a websocket client and handler simulating the local websocket proxy
|
||||
// created during Boundary connect. The proxyConn returned should be used as the connection
|
||||
// passed into the worker proxy handler, while the clientConn can be used to simulate
|
||||
// the local end user connection.
|
||||
func TestWsConn(t *testing.T, ctx context.Context) (clientConn, proxyConn *websocket.Conn) {
|
||||
t.Helper()
|
||||
require, assert := require.New(t), assert.New(t)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
port := testutil.TestFreePort(t)
|
||||
go func() {
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
|
||||
proxyConn, err = websocket.Accept(w, r, nil)
|
||||
require.NoError(err)
|
||||
|
||||
wg.Done()
|
||||
// block waiting for test to complete
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}))
|
||||
assert.NoError(err)
|
||||
}()
|
||||
|
||||
time.Sleep(time.Duration(1.5 * float64(time.Second)))
|
||||
clientConn, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://localhost:%d", port), nil)
|
||||
require.NoError(err)
|
||||
wg.Wait()
|
||||
return
|
||||
}
|
||||
Reference in new issue
Block a user