@@ -178,10 +178,12 @@ func (s) TestServerCredsHandshake_XDSHandshakeInfoError(t *testing.T) {
178178 if err != nil {
179179 t .Fatalf ("NewServerCredentials(%v) failed: %v" , opts , err )
180180 }
181+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
182+ defer cancel ()
181183
182184 // Create a test server which uses the xDS server credentials created above
183185 // to perform TLS handshake on incoming connections.
184- ts := newTestServerWithHandshakeFunc (func (rawConn net.Conn ) handshakeResult {
186+ ts := newTestServerWithHandshakeFunc (ctx , func (rawConn net.Conn ) handshakeResult {
185187 // Create a wrapped conn which returns a nil HandshakeInfo and a non-nil error.
186188 conn := newWrappedConn (rawConn , nil , time .Now ().Add (defaultTestTimeout ))
187189 hiErr := errors .New ("xdsHandshakeInfo error" )
@@ -208,8 +210,6 @@ func (s) TestServerCredsHandshake_XDSHandshakeInfoError(t *testing.T) {
208210
209211 // Read handshake result from the testServer which will return an error if
210212 // the handshake succeeded.
211- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
212- defer cancel ()
213213 val , err := ts .hsResult .Receive (ctx )
214214 if err != nil {
215215 t .Fatalf ("testServer failed to return handshake result: %v" , err )
@@ -229,10 +229,12 @@ func (s) TestServerCredsHandshakeTimeout(t *testing.T) {
229229 if err != nil {
230230 t .Fatalf ("NewServerCredentials(%v) failed: %v" , opts , err )
231231 }
232+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
233+ defer cancel ()
232234
233235 // Create a test server which uses the xDS server credentials created above
234236 // to perform TLS handshake on incoming connections.
235- ts := newTestServerWithHandshakeFunc (func (rawConn net.Conn ) handshakeResult {
237+ ts := newTestServerWithHandshakeFunc (ctx , func (rawConn net.Conn ) handshakeResult {
236238 hi := xdsinternal .NewHandshakeInfo (makeRootProvider (t , "x509/client_ca_cert.pem" ), makeIdentityProvider (t , "x509/server2_cert.pem" , "x509/server2_key.pem" ), nil , true )
237239
238240 // Create a wrapped conn which can return the HandshakeInfo created
@@ -258,8 +260,6 @@ func (s) TestServerCredsHandshakeTimeout(t *testing.T) {
258260 defer rawConn .Close ()
259261
260262 // Read handshake result from the testServer and expect a failure result.
261- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
262- defer cancel ()
263263 val , err := ts .hsResult .Receive (ctx )
264264 if err != nil {
265265 t .Fatalf ("testServer failed to return handshake result: %v" , err )
@@ -279,10 +279,12 @@ func (s) TestServerCredsHandshakeFailure(t *testing.T) {
279279 if err != nil {
280280 t .Fatalf ("NewServerCredentials(%v) failed: %v" , opts , err )
281281 }
282+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
283+ defer cancel ()
282284
283285 // Create a test server which uses the xDS server credentials created above
284286 // to perform TLS handshake on incoming connections.
285- ts := newTestServerWithHandshakeFunc (func (rawConn net.Conn ) handshakeResult {
287+ ts := newTestServerWithHandshakeFunc (ctx , func (rawConn net.Conn ) handshakeResult {
286288 // Create a HandshakeInfo which has a root provider which does not match
287289 // the certificate sent by the client.
288290 hi := xdsinternal .NewHandshakeInfo (makeRootProvider (t , "x509/server_ca_cert.pem" ), makeIdentityProvider (t , "x509/client2_cert.pem" , "x509/client2_key.pem" ), nil , true )
@@ -314,8 +316,6 @@ func (s) TestServerCredsHandshakeFailure(t *testing.T) {
314316
315317 // Read handshake result from the testServer which will return an error if
316318 // the handshake succeeded.
317- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
318- defer cancel ()
319319 val , err := ts .hsResult .Receive (ctx )
320320 if err != nil {
321321 t .Fatalf ("testServer failed to return handshake result: %v" , err )
@@ -361,10 +361,12 @@ func (s) TestServerCredsHandshakeSuccess(t *testing.T) {
361361 if err != nil {
362362 t .Fatalf ("NewServerCredentials(%v) failed: %v" , opts , err )
363363 }
364+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
365+ defer cancel ()
364366
365367 // Create a test server which uses the xDS server credentials
366368 // created above to perform TLS handshake on incoming connections.
367- ts := newTestServerWithHandshakeFunc (func (rawConn net.Conn ) handshakeResult {
369+ ts := newTestServerWithHandshakeFunc (ctx , func (rawConn net.Conn ) handshakeResult {
368370 // Create a HandshakeInfo with information from the test table.
369371 hi := xdsinternal .NewHandshakeInfo (test .rootProvider , test .identityProvider , nil , test .requireClientCert )
370372
@@ -406,8 +408,6 @@ func (s) TestServerCredsHandshakeSuccess(t *testing.T) {
406408 // Read the handshake result from the testServer which contains the
407409 // TLS connection state on the server-side and compare it with the
408410 // one received on the client-side.
409- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
410- defer cancel ()
411411 val , err := ts .hsResult .Receive (ctx )
412412 if err != nil {
413413 t .Fatalf ("testServer failed to return handshake result: %v" , err )
@@ -433,14 +433,16 @@ func (s) TestServerCredsProviderSwitch(t *testing.T) {
433433 if err != nil {
434434 t .Fatalf ("NewServerCredentials(%v) failed: %v" , opts , err )
435435 }
436+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
437+ defer cancel ()
436438
437439 // The first time the handshake function is invoked, it returns a
438440 // HandshakeInfo which is expected to fail. Further invocations return a
439441 // HandshakeInfo which is expected to succeed.
440442 cnt := 0
441443 // Create a test server which uses the xDS server credentials created above
442444 // to perform TLS handshake on incoming connections.
443- ts := newTestServerWithHandshakeFunc (func (rawConn net.Conn ) handshakeResult {
445+ ts := newTestServerWithHandshakeFunc (ctx , func (rawConn net.Conn ) handshakeResult {
444446 cnt ++
445447 var hi * xdsinternal.HandshakeInfo
446448 if cnt == 1 {
@@ -501,8 +503,6 @@ func (s) TestServerCredsProviderSwitch(t *testing.T) {
501503 // Read the handshake result from the testServer which contains the
502504 // TLS connection state on the server-side and compare it with the
503505 // one received on the client-side.
504- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
505- defer cancel ()
506506 val , err := ts .hsResult .Receive (ctx )
507507 if err != nil {
508508 t .Fatalf ("testServer failed to return handshake result: %v" , err )
0 commit comments