diff --git a/xds/internal/xdsclient/loadreport_test.go b/xds/internal/xdsclient/loadreport_test.go index 90e29f65a80a..afdb7c607485 100644 --- a/xds/internal/xdsclient/loadreport_test.go +++ b/xds/internal/xdsclient/loadreport_test.go @@ -132,8 +132,20 @@ func (s) TestLRSClient(t *testing.T) { // Cancel this load reporting stream, server should see error canceled. lrsCancel2() - // Server should receive a stream canceled error. - if u, err := fs2.LRSRequestChan.Receive(ctx); err != nil || status.Code(u.(*fakeserver.Request).Err) != codes.Canceled { - t.Errorf("unexpected LRS request: %v, %v, want error canceled", u, err) + // Server should receive a stream canceled error. There may be additional + // load reports from the client in the channel. + for { + u, err := fs2.LRSRequestChan.Receive(ctx) + if err != nil { + t.Fatalf("unexpected error while reading LRS request: %v", err) + } + // Ignore load reports sent before the stream was cancelled. + if u.(*fakeserver.Request).Err == nil { + continue + } + if status.Code(u.(*fakeserver.Request).Err) != codes.Canceled { + t.Errorf("unexpected LRS request: %v, want error canceled", u) + } + break } }