Skip to content

Commit 30597fe

Browse files
committed
test to verify both update and error are sent
1 parent c007e8a commit 30597fe

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

xds/internal/xdsclient/authority.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,19 @@ func (a *authority) watchResource(rType xdsresource.Type, resourceName string, w
633633
// Always add the new watcher to the set of watchers.
634634
state.watchers[watcher] = true
635635

636-
// If we have a cached copy of the resource, notify the new watcher.
636+
// If we have a cached copy of the resource, notify the new watcher
637+
// immediately.
637638
if state.cache != nil {
638639
if a.logger.V(2) {
639640
a.logger.Infof("Resource type %q with resource name %q found in cache: %s", rType.TypeName(), resourceName, state.cache.ToJSON())
640641
}
641642
resource := state.cache
642643
a.watcherCallbackSerializer.TrySchedule(func(context.Context) { watcher.OnUpdate(resource, func() {}) })
644+
// If last update was NACK'd, notify the new watcher of error
645+
// immediately as well.
646+
if state.md.Status == xdsresource.ServiceStatusNACKed && state.md.ErrState != nil {
647+
a.watcherCallbackSerializer.TrySchedule(func(context.Context) { watcher.OnError(state.md.ErrState.Err, func() {}) })
648+
}
643649
}
644650
cleanup = a.unwatchResource(rType, resourceName, watcher)
645651
}, func() {

xds/internal/xdsclient/tests/lds_watchers_test.go

+45-12
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ func (cw *listenerWatcher) OnResourceDoesNotExist(onDone xdsresource.OnDoneFunc)
9090
onDone()
9191
}
9292

93+
type listenerWatcherMultiple struct {
94+
updateCh *testutils.Channel
95+
}
96+
97+
func newListenerWatcherMultiple() *listenerWatcherMultiple {
98+
return &listenerWatcherMultiple{updateCh: testutils.NewChannelWithSize(2)}
99+
}
100+
101+
func (cw *listenerWatcherMultiple) OnUpdate(update *xdsresource.ListenerResourceData, onDone xdsresource.OnDoneFunc) {
102+
cw.updateCh.Send(listenerUpdateErrTuple{update: update.Resource})
103+
onDone()
104+
}
105+
106+
func (cw *listenerWatcherMultiple) OnError(err error, onDone xdsresource.OnDoneFunc) {
107+
// When used with a go-control-plane management server that continuously
108+
// resends resources which are NACKed by the xDS client, using a `Replace()`
109+
// here and in OnResourceDoesNotExist() simplifies tests which will have
110+
// access to the most recently received error.
111+
cw.updateCh.Send(listenerUpdateErrTuple{err: err})
112+
onDone()
113+
}
114+
115+
func (cw *listenerWatcherMultiple) OnResourceDoesNotExist(onDone xdsresource.OnDoneFunc) {
116+
cw.updateCh.Send(listenerUpdateErrTuple{err: xdsresource.NewErrorf(xdsresource.ErrorTypeResourceNotFound, "Listener not found in received response")})
117+
onDone()
118+
}
119+
93120
// badListenerResource returns a listener resource for the given name which does
94121
// not contain the `RouteSpecifier` field in the HTTPConnectionManager, and
95122
// hence is expected to be NACKed by the client.
@@ -547,7 +574,7 @@ func (s) TestLDSWatch_ThreeWatchesForDifferentResourceNames(t *testing.T) {
547574
// a resource which is already present in the cache. The test verifies that the
548575
// watch callback is invoked with the contents from the cache, instead of a
549576
// request being sent to the management server.
550-
func (s) TestLDSWatch_ResourceCaching(t *testing.T) {
577+
func TestLDSWatch_ResourceCaching(t *testing.T) {
551578
firstRequestReceived := false
552579
firstAckReceived := grpcsync.NewEvent()
553580
secondRequestReceived := grpcsync.NewEvent()
@@ -926,7 +953,7 @@ func (s) TestLDSWatch_NACKError(t *testing.T) {
926953
// good update and latest NACK error. The test verifies that new watcher
927954
// receives both good update and error without request being sent to the
928955
// management server.
929-
func (s) TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
956+
func TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
930957
firstRequestReceived := false
931958
firstAckReceived := grpcsync.NewEvent()
932959
secondRequestReceived := grpcsync.NewEvent()
@@ -943,6 +970,13 @@ func (s) TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
943970
firstAckReceived.Fire()
944971
return nil
945972
}
973+
// If the request version remains "1" while the nonce keeps
974+
// increasing, it indicates the client is repeatedly NACKing
975+
// updates from the server but not sending any new resource
976+
// request.
977+
if req.GetVersionInfo() == "1" {
978+
return nil
979+
}
946980
// Any requests after the first request and ack, are not expected.
947981
secondRequestReceived.Fire()
948982
return nil
@@ -1018,21 +1052,12 @@ func (s) TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
10181052

10191053
// Register another watch for the same resource. This should get the update
10201054
// and error from the cache.
1021-
lw2 := newListenerWatcher()
1055+
lw2 := newListenerWatcherMultiple()
10221056
ldsCancel2 := xdsresource.WatchListener(client, ldsName, lw2)
10231057
defer ldsCancel2()
10241058
if err := verifyListenerUpdate(ctx, lw2.updateCh, wantUpdate); err != nil {
10251059
t.Fatal(err)
10261060
}
1027-
u, err = lw2.updateCh.Receive(ctx)
1028-
if err != nil {
1029-
t.Fatalf("timeout when waiting for a listener resource from the management server: %v", err)
1030-
}
1031-
gotErr = u.(listenerUpdateErrTuple).err
1032-
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
1033-
t.Fatalf("update received with error: %v, want %q", gotErr, wantListenerNACKErr)
1034-
}
1035-
10361061
// No request should get sent out as part of this watch.
10371062
sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
10381063
defer sCancel()
@@ -1041,6 +1066,14 @@ func (s) TestLDSWatch_ResourceCaching_NACKError(t *testing.T) {
10411066
case <-secondRequestReceived.Done():
10421067
t.Fatal("xdsClient sent out request instead of using update from cache")
10431068
}
1069+
u, err = lw2.updateCh.Receive(ctx)
1070+
if err != nil {
1071+
t.Fatalf("timeout when waiting for a listener resource from the management server: %v", err)
1072+
}
1073+
gotErr = u.(listenerUpdateErrTuple).err
1074+
if gotErr == nil || !strings.Contains(gotErr.Error(), wantListenerNACKErr) {
1075+
t.Fatalf("update received with error: %v, want %q", gotErr, wantListenerNACKErr)
1076+
}
10441077
}
10451078

10461079
// TestLDSWatch_PartialValid covers the case where a response from the

0 commit comments

Comments
 (0)