Skip to content

Commit

Permalink
xds/resolver: test xds client closed by resolver Close (#4509)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley committed Jun 8, 2021
1 parent 656cad9 commit d30e2c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
14 changes: 14 additions & 0 deletions xds/internal/resolver/xds_resolver_test.go
Expand Up @@ -281,6 +281,20 @@ func (s) TestXDSResolverWatchCallbackAfterClose(t *testing.T) {
}
}

// TestXDSResolverCloseClosesXDSClient tests that the XDS resolver's Close
// method closes the XDS client.
func (s) TestXDSResolverCloseClosesXDSClient(t *testing.T) {
xdsC := fakeclient.NewClient()
xdsR, _, cancel := testSetup(t, setupOpts{
xdsClientFunc: func() (xdsClient, error) { return xdsC, nil },
})
defer cancel()
xdsR.Close()
if !xdsC.Closed.HasFired() {
t.Fatalf("xds client not closed by xds resolver Close method")
}
}

// TestXDSResolverBadServiceUpdate tests the case the xdsClient returns a bad
// service update.
func (s) TestXDSResolverBadServiceUpdate(t *testing.T) {
Expand Down
17 changes: 6 additions & 11 deletions xds/internal/testutils/fakeclient/client.go
Expand Up @@ -22,6 +22,7 @@ package fakeclient
import (
"context"

"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/xds/internal/xdsclient"
"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
Expand All @@ -41,14 +42,15 @@ type Client struct {
cdsCancelCh *testutils.Channel
edsCancelCh *testutils.Channel
loadReportCh *testutils.Channel
closeCh *testutils.Channel
loadStore *load.Store
bootstrapCfg *bootstrap.Config

ldsCb func(xdsclient.ListenerUpdate, error)
rdsCb func(xdsclient.RouteConfigUpdate, error)
cdsCbs map[string]func(xdsclient.ClusterUpdate, error)
edsCb func(xdsclient.EndpointsUpdate, error)

Closed *grpcsync.Event // fired when Close is called.
}

// WatchListener registers a LDS watch.
Expand Down Expand Up @@ -228,16 +230,9 @@ func (xdsC *Client) WaitForReportLoad(ctx context.Context) (ReportLoadArgs, erro
return val.(ReportLoadArgs), err
}

// Close closes the xds client.
// Close fires xdsC.Closed, indicating it was called.
func (xdsC *Client) Close() {
xdsC.closeCh.Send(nil)
}

// WaitForClose waits for Close to be invoked on this client and returns
// context.DeadlineExceeded otherwise.
func (xdsC *Client) WaitForClose(ctx context.Context) error {
_, err := xdsC.closeCh.Receive(ctx)
return err
xdsC.Closed.Fire()
}

// BootstrapConfig returns the bootstrap config.
Expand Down Expand Up @@ -275,8 +270,8 @@ func NewClientWithName(name string) *Client {
cdsCancelCh: testutils.NewChannelWithSize(10),
edsCancelCh: testutils.NewChannel(),
loadReportCh: testutils.NewChannel(),
closeCh: testutils.NewChannel(),
loadStore: load.NewStore(),
cdsCbs: make(map[string]func(xdsclient.ClusterUpdate, error)),
Closed: grpcsync.NewEvent(),
}
}

0 comments on commit d30e2c9

Please sign in to comment.