Skip to content

Commit b89f49b

Browse files
authored
xdsclient: deflake Test/LDSWatch_PartialValid (#5552)
1 parent 9bc72de commit b89f49b

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

xds/internal/xdsclient/e2e_test/lds_watchers_test.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ import (
4646
_ "google.golang.org/grpc/xds/internal/xdsclient/controller/version/v3" // Register the v3 xDS API client.
4747
)
4848

49-
func overrideFedEnvVar(t *testing.T) func() {
49+
func overrideFedEnvVar(t *testing.T) {
5050
oldFed := envconfig.XDSFederation
5151
envconfig.XDSFederation = true
52-
return func() { envconfig.XDSFederation = oldFed }
52+
t.Cleanup(func() { envconfig.XDSFederation = oldFed })
5353
}
5454

5555
type s struct {
@@ -148,7 +148,7 @@ func verifyListenerUpdate(ctx context.Context, updateCh *testutils.Channel, want
148148
//
149149
// The test is run for old and new style names.
150150
func (s) TestLDSWatch(t *testing.T) {
151-
defer overrideFedEnvVar(t)()
151+
overrideFedEnvVar(t)
152152
mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, nil)
153153
defer cleanup()
154154

@@ -268,7 +268,7 @@ func (s) TestLDSWatch(t *testing.T) {
268268
//
269269
// The test is run for old and new style names.
270270
func (s) TestLDSWatch_TwoWatchesForSameResourceName(t *testing.T) {
271-
defer overrideFedEnvVar(t)()
271+
overrideFedEnvVar(t)
272272
mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, nil)
273273
defer cleanup()
274274

@@ -402,7 +402,7 @@ func (s) TestLDSWatch_TwoWatchesForSameResourceName(t *testing.T) {
402402
//
403403
// The test is run with both old and new style names.
404404
func (s) TestLDSWatch_ThreeWatchesForDifferentResourceNames(t *testing.T) {
405-
defer overrideFedEnvVar(t)()
405+
overrideFedEnvVar(t)
406406
mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, nil)
407407
defer cleanup()
408408

@@ -474,7 +474,7 @@ func (s) TestLDSWatch_ThreeWatchesForDifferentResourceNames(t *testing.T) {
474474
// watch callback is invoked with the contents from the cache, instead of a
475475
// request being sent to the management server.
476476
func (s) TestLDSWatch_ResourceCaching(t *testing.T) {
477-
defer overrideFedEnvVar(t)()
477+
overrideFedEnvVar(t)
478478
firstRequestReceived := false
479479
firstAckReceived := grpcsync.NewEvent()
480480
secondRequestReceived := grpcsync.NewEvent()
@@ -574,7 +574,7 @@ func (s) TestLDSWatch_ResourceCaching(t *testing.T) {
574574
//
575575
// The test is run with both old and new style names.
576576
func (s) TestLDSWatch_ResourceRemoved(t *testing.T) {
577-
defer overrideFedEnvVar(t)()
577+
overrideFedEnvVar(t)
578578
mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, nil)
579579
defer cleanup()
580580

@@ -682,7 +682,7 @@ func (s) TestLDSWatch_ResourceRemoved(t *testing.T) {
682682
// server is NACK'ed by the xdsclient. The test verifies that the error is
683683
// propagated to the watcher.
684684
func (s) TestLDSWatch_NACKError(t *testing.T) {
685-
defer overrideFedEnvVar(t)()
685+
overrideFedEnvVar(t)
686686
mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, nil)
687687
defer cleanup()
688688

@@ -695,9 +695,11 @@ func (s) TestLDSWatch_NACKError(t *testing.T) {
695695

696696
// Register a watch for a listener resource and have the watch
697697
// callback push the received update on to a channel.
698+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
699+
defer cancel()
698700
updateCh := testutils.NewChannel()
699701
ldsCancel := client.WatchListener(ldsName, func(u xdsresource.ListenerUpdate, err error) {
700-
updateCh.Send(xdsresource.ListenerUpdateErrTuple{Update: u, Err: err})
702+
updateCh.SendContext(ctx, xdsresource.ListenerUpdateErrTuple{Update: u, Err: err})
701703
})
702704
defer ldsCancel()
703705

@@ -708,8 +710,6 @@ func (s) TestLDSWatch_NACKError(t *testing.T) {
708710
Listeners: []*v3listenerpb.Listener{badListenerResource(ldsName)},
709711
SkipValidation: true,
710712
}
711-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
712-
defer cancel()
713713
if err := mgmtServer.Update(ctx, resources); err != nil {
714714
t.Fatalf("Failed to update management server with resources: %v, err: %v", resources, err)
715715
}
@@ -731,7 +731,7 @@ func (s) TestLDSWatch_NACKError(t *testing.T) {
731731
// to the valid resource receive the update, while watchers corresponding to the
732732
// invalid resource receive an error.
733733
func (s) TestLDSWatch_PartialValid(t *testing.T) {
734-
defer overrideFedEnvVar(t)()
734+
overrideFedEnvVar(t)
735735
mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, nil)
736736
defer cleanup()
737737

@@ -745,15 +745,18 @@ func (s) TestLDSWatch_PartialValid(t *testing.T) {
745745
// Register two watches for listener resources. The first watch is expected
746746
// to receive an error because the received resource is NACKed. The second
747747
// watch is expected to get a good update.
748+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
749+
defer cancel()
750+
badResourceName := ldsName
748751
updateCh1 := testutils.NewChannel()
749-
ldsCancel1 := client.WatchListener(ldsName, func(u xdsresource.ListenerUpdate, err error) {
750-
updateCh1.Send(xdsresource.ListenerUpdateErrTuple{Update: u, Err: err})
752+
ldsCancel1 := client.WatchListener(badResourceName, func(u xdsresource.ListenerUpdate, err error) {
753+
updateCh1.SendContext(ctx, xdsresource.ListenerUpdateErrTuple{Update: u, Err: err})
751754
})
752755
defer ldsCancel1()
753-
resourceName2 := ldsNameNewStyle
756+
goodResourceName := ldsNameNewStyle
754757
updateCh2 := testutils.NewChannel()
755-
ldsCancel2 := client.WatchListener(resourceName2, func(u xdsresource.ListenerUpdate, err error) {
756-
updateCh2.Send(xdsresource.ListenerUpdateErrTuple{Update: u, Err: err})
758+
ldsCancel2 := client.WatchListener(goodResourceName, func(u xdsresource.ListenerUpdate, err error) {
759+
updateCh2.SendContext(ctx, xdsresource.ListenerUpdateErrTuple{Update: u, Err: err})
757760
})
758761
defer ldsCancel2()
759762

@@ -762,13 +765,11 @@ func (s) TestLDSWatch_PartialValid(t *testing.T) {
762765
resources := e2e.UpdateOptions{
763766
NodeID: nodeID,
764767
Listeners: []*v3listenerpb.Listener{
765-
badListenerResource(ldsName),
766-
e2e.DefaultClientListener(resourceName2, rdsName),
768+
badListenerResource(badResourceName),
769+
e2e.DefaultClientListener(goodResourceName, rdsName),
767770
},
768771
SkipValidation: true,
769772
}
770-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
771-
defer cancel()
772773
if err := mgmtServer.Update(ctx, resources); err != nil {
773774
t.Fatalf("Failed to update management server with resources: %v, err: %v", resources, err)
774775
}

0 commit comments

Comments
 (0)