TUN-6864: Don't reuse port in quic unit tests

This commit is contained in:
cthuang 2022-10-18 09:31:16 +01:00 committed by Chung-Ting Huang
parent 442af9ee38
commit 60a12fcb27
1 changed files with 26 additions and 23 deletions

View File

@ -11,7 +11,6 @@ import (
"net/url" "net/url"
"os" "os"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@ -142,24 +141,33 @@ func TestQUICServer(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
test := test // capture range variable
t.Run(test.desc, func(t *testing.T) { t.Run(test.desc, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1) quicListener, err := quic.Listen(udpListener, testTLSServerConfig, testQUICConfig)
require.NoError(t, err)
serverDone := make(chan struct{})
go func() { go func() {
defer wg.Done()
quicServer( quicServer(
t, udpListener, testTLSServerConfig, testQUICConfig, ctx, t, quicListener, test.dest, test.connectionType, test.metadata, test.message, test.expectedResponse,
test.dest, test.connectionType, test.metadata, test.message, test.expectedResponse,
) )
close(serverDone)
}() }()
qc := testQUICConnection(udpListener.LocalAddr(), t) qc := testQUICConnection(udpListener.LocalAddr(), t, uint8(i))
go qc.Serve(ctx)
wg.Wait() connDone := make(chan struct{})
go func() {
qc.Serve(ctx)
close(connDone)
}()
<-serverDone
cancel() cancel()
<-connDone
}) })
} }
} }
@ -177,23 +185,16 @@ func (fakeControlStream) IsStopped() bool {
} }
func quicServer( func quicServer(
ctx context.Context,
t *testing.T, t *testing.T,
conn net.PacketConn, listener quic.Listener,
tlsConf *tls.Config,
config *quic.Config,
dest string, dest string,
connectionType quicpogs.ConnectionType, connectionType quicpogs.ConnectionType,
metadata []quicpogs.Metadata, metadata []quicpogs.Metadata,
message []byte, message []byte,
expectedResponse []byte, expectedResponse []byte,
) { ) {
ctx, cancel := context.WithCancel(context.Background()) session, err := listener.Accept(ctx)
defer cancel()
earlyListener, err := quic.Listen(conn, tlsConf, config)
require.NoError(t, err)
session, err := earlyListener.Accept(ctx)
require.NoError(t, err) require.NoError(t, err)
quicStream, err := session.OpenStreamSync(context.Background()) quicStream, err := session.OpenStreamSync(context.Background())
@ -482,6 +483,7 @@ func TestBuildHTTPRequest(t *testing.T) {
log := zerolog.Nop() log := zerolog.Nop()
for _, test := range tests { for _, test := range tests {
test := test // capture range variable
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
req, err := buildHTTPRequest(context.Background(), test.connectRequest, test.body, &log) req, err := buildHTTPRequest(context.Background(), test.connectRequest, test.body, &log)
assert.NoError(t, err) assert.NoError(t, err)
@ -519,7 +521,8 @@ func TestServeUDPSession(t *testing.T) {
edgeQUICSessionChan <- edgeQUICSession edgeQUICSessionChan <- edgeQUICSession
}() }()
qc := testQUICConnection(val, t) // Random index to avoid reusing port
qc := testQUICConnection(val, t, 28)
go qc.Serve(ctx) go qc.Serve(ctx)
edgeQUICSession := <-edgeQUICSessionChan edgeQUICSession := <-edgeQUICSessionChan
@ -703,7 +706,7 @@ func (s mockSessionRPCServer) UnregisterUdpSession(ctx context.Context, sessionI
return nil return nil
} }
func testQUICConnection(udpListenerAddr net.Addr, t *testing.T) *QUICConnection { func testQUICConnection(udpListenerAddr net.Addr, t *testing.T, index uint8) *QUICConnection {
tlsClientConfig := &tls.Config{ tlsClientConfig := &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
NextProtos: []string{"argotunnel"}, NextProtos: []string{"argotunnel"},
@ -713,7 +716,7 @@ func testQUICConnection(udpListenerAddr net.Addr, t *testing.T) *QUICConnection
qc, err := NewQUICConnection( qc, err := NewQUICConnection(
testQUICConfig, testQUICConfig,
udpListenerAddr, udpListenerAddr,
0, index,
tlsClientConfig, tlsClientConfig,
&mockOrchestrator{originProxy: &mockOriginProxyWithRequest{}}, &mockOrchestrator{originProxy: &mockOriginProxyWithRequest{}},
&tunnelpogs.ConnectionOptions{}, &tunnelpogs.ConnectionOptions{},