@@ -11,7 +11,6 @@ import (
1111 "net/url"
1212 "os"
1313 "strings"
14- "sync"
1514 "testing"
1615 "time"
1716
@@ -142,24 +141,33 @@ func TestQUICServer(t *testing.T) {
142141 },
143142 }
144143
145- for _ , test := range tests {
144+ for i , test := range tests {
145+ test := test // capture range variable
146146 t .Run (test .desc , func (t * testing.T ) {
147147 ctx , cancel := context .WithCancel (context .Background ())
148- var wg sync.WaitGroup
149- wg .Add (1 )
148+
149+ quicListener , err := quic .Listen (udpListener , testTLSServerConfig , testQUICConfig )
150+ require .NoError (t , err )
151+
152+ serverDone := make (chan struct {})
150153 go func () {
151- defer wg .Done ()
152154 quicServer (
153- t , udpListener , testTLSServerConfig , testQUICConfig ,
154- test .dest , test .connectionType , test .metadata , test .message , test .expectedResponse ,
155+ ctx , t , quicListener , test .dest , test .connectionType , test .metadata , test .message , test .expectedResponse ,
155156 )
157+ close (serverDone )
156158 }()
157159
158- qc := testQUICConnection (udpListener .LocalAddr (), t )
159- go qc .Serve (ctx )
160+ qc := testQUICConnection (udpListener .LocalAddr (), t , uint8 (i ))
161+
162+ connDone := make (chan struct {})
163+ go func () {
164+ qc .Serve (ctx )
165+ close (connDone )
166+ }()
160167
161- wg . Wait ()
168+ <- serverDone
162169 cancel ()
170+ <- connDone
163171 })
164172 }
165173}
@@ -177,23 +185,16 @@ func (fakeControlStream) IsStopped() bool {
177185}
178186
179187func quicServer (
188+ ctx context.Context ,
180189 t * testing.T ,
181- conn net.PacketConn ,
182- tlsConf * tls.Config ,
183- config * quic.Config ,
190+ listener quic.Listener ,
184191 dest string ,
185192 connectionType quicpogs.ConnectionType ,
186193 metadata []quicpogs.Metadata ,
187194 message []byte ,
188195 expectedResponse []byte ,
189196) {
190- ctx , cancel := context .WithCancel (context .Background ())
191- defer cancel ()
192-
193- earlyListener , err := quic .Listen (conn , tlsConf , config )
194- require .NoError (t , err )
195-
196- session , err := earlyListener .Accept (ctx )
197+ session , err := listener .Accept (ctx )
197198 require .NoError (t , err )
198199
199200 quicStream , err := session .OpenStreamSync (context .Background ())
@@ -482,6 +483,7 @@ func TestBuildHTTPRequest(t *testing.T) {
482483
483484 log := zerolog .Nop ()
484485 for _ , test := range tests {
486+ test := test // capture range variable
485487 t .Run (test .name , func (t * testing.T ) {
486488 req , err := buildHTTPRequest (context .Background (), test .connectRequest , test .body , & log )
487489 assert .NoError (t , err )
@@ -519,7 +521,8 @@ func TestServeUDPSession(t *testing.T) {
519521 edgeQUICSessionChan <- edgeQUICSession
520522 }()
521523
522- qc := testQUICConnection (val , t )
524+ // Random index to avoid reusing port
525+ qc := testQUICConnection (val , t , 28 )
523526 go qc .Serve (ctx )
524527
525528 edgeQUICSession := <- edgeQUICSessionChan
@@ -703,7 +706,7 @@ func (s mockSessionRPCServer) UnregisterUdpSession(ctx context.Context, sessionI
703706 return nil
704707}
705708
706- func testQUICConnection (udpListenerAddr net.Addr , t * testing.T ) * QUICConnection {
709+ func testQUICConnection (udpListenerAddr net.Addr , t * testing.T , index uint8 ) * QUICConnection {
707710 tlsClientConfig := & tls.Config {
708711 InsecureSkipVerify : true ,
709712 NextProtos : []string {"argotunnel" },
@@ -713,7 +716,7 @@ func testQUICConnection(udpListenerAddr net.Addr, t *testing.T) *QUICConnection
713716 qc , err := NewQUICConnection (
714717 testQUICConfig ,
715718 udpListenerAddr ,
716- 0 ,
719+ index ,
717720 tlsClientConfig ,
718721 & mockOrchestrator {originProxy : & mockOriginProxyWithRequest {}},
719722 & tunnelpogs.ConnectionOptions {},
0 commit comments