xds: Improve xds_resolver_test.go (#3259)

Also get rid of the testutils.XDSClient type. We have a new
testutils/fakexds/Client type which will be used in tests going forward.
This commit is contained in:
Easwar Swaminathan 2019-12-19 09:21:11 -08:00 коммит произвёл GitHub
Родитель d44b1112a2
Коммит 9819552c93
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 9 добавлений и 75 удалений

Просмотреть файл

@ -34,6 +34,7 @@ import (
xdsclient "google.golang.org/grpc/xds/internal/client" xdsclient "google.golang.org/grpc/xds/internal/client"
"google.golang.org/grpc/xds/internal/client/bootstrap" "google.golang.org/grpc/xds/internal/client/bootstrap"
"google.golang.org/grpc/xds/internal/testutils" "google.golang.org/grpc/xds/internal/testutils"
"google.golang.org/grpc/xds/internal/testutils/fakexds"
corepb "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" corepb "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
) )
@ -97,7 +98,7 @@ func getXDSClientMakerFunc(wantOpts xdsclient.Options) func(xdsclient.Options) (
if len(gotOpts.DialOpts) != len(wantOpts.DialOpts) { if len(gotOpts.DialOpts) != len(wantOpts.DialOpts) {
return nil, fmt.Errorf("got len(DialOpts): %v, want: %v", len(gotOpts.DialOpts), len(wantOpts.DialOpts)) return nil, fmt.Errorf("got len(DialOpts): %v, want: %v", len(gotOpts.DialOpts), len(wantOpts.DialOpts))
} }
return testutils.NewXDSClient(), nil return fakexds.NewClient(), nil
} }
} }
@ -231,7 +232,7 @@ func testSetup(t *testing.T, opts setupOpts) (*xdsResolver, *testClientConn, fun
// waitForWatchService waits for the WatchService method to be called on the // waitForWatchService waits for the WatchService method to be called on the
// xdsClient within a reasonable amount of time, and also verifies that the // xdsClient within a reasonable amount of time, and also verifies that the
// watch is called with the expected target. // watch is called with the expected target.
func waitForWatchService(t *testing.T, xdsC *testutils.XDSClient, wantTarget string) { func waitForWatchService(t *testing.T, xdsC *fakexds.Client, wantTarget string) {
t.Helper() t.Helper()
gotTarget, err := xdsC.WaitForWatchService() gotTarget, err := xdsC.WaitForWatchService()
@ -246,7 +247,7 @@ func waitForWatchService(t *testing.T, xdsC *testutils.XDSClient, wantTarget str
// TestXDSResolverWatchCallbackAfterClose tests the case where a service update // TestXDSResolverWatchCallbackAfterClose tests the case where a service update
// from the underlying xdsClient is received after the resolver is closed. // from the underlying xdsClient is received after the resolver is closed.
func TestXDSResolverWatchCallbackAfterClose(t *testing.T) { func TestXDSResolverWatchCallbackAfterClose(t *testing.T) {
xdsC := testutils.NewXDSClient() xdsC := fakexds.NewClient()
xdsR, tcc, cancel := testSetup(t, setupOpts{ xdsR, tcc, cancel := testSetup(t, setupOpts{
config: &validConfig, config: &validConfig,
xdsClientFunc: func(_ xdsclient.Options) (xdsClientInterface, error) { return xdsC, nil }, xdsClientFunc: func(_ xdsclient.Options) (xdsClientInterface, error) { return xdsC, nil },
@ -258,7 +259,7 @@ func TestXDSResolverWatchCallbackAfterClose(t *testing.T) {
// Call the watchAPI callback after closing the resolver, and make sure no // Call the watchAPI callback after closing the resolver, and make sure no
// update is triggerred on the ClientConn. // update is triggerred on the ClientConn.
xdsR.Close() xdsR.Close()
xdsC.InvokeWatchServiceCb(cluster, nil) xdsC.InvokeWatchServiceCallback(cluster, nil)
if gotVal, gotErr := tcc.stateCh.Receive(); gotErr != testutils.ErrRecvTimeout { if gotVal, gotErr := tcc.stateCh.Receive(); gotErr != testutils.ErrRecvTimeout {
t.Fatalf("ClientConn.UpdateState called after xdsResolver is closed: %v", gotVal) t.Fatalf("ClientConn.UpdateState called after xdsResolver is closed: %v", gotVal)
} }
@ -267,7 +268,7 @@ func TestXDSResolverWatchCallbackAfterClose(t *testing.T) {
// TestXDSResolverBadServiceUpdate tests the case the xdsClient returns a bad // TestXDSResolverBadServiceUpdate tests the case the xdsClient returns a bad
// service update. // service update.
func TestXDSResolverBadServiceUpdate(t *testing.T) { func TestXDSResolverBadServiceUpdate(t *testing.T) {
xdsC := testutils.NewXDSClient() xdsC := fakexds.NewClient()
xdsR, tcc, cancel := testSetup(t, setupOpts{ xdsR, tcc, cancel := testSetup(t, setupOpts{
config: &validConfig, config: &validConfig,
xdsClientFunc: func(_ xdsclient.Options) (xdsClientInterface, error) { return xdsC, nil }, xdsClientFunc: func(_ xdsclient.Options) (xdsClientInterface, error) { return xdsC, nil },
@ -282,7 +283,7 @@ func TestXDSResolverBadServiceUpdate(t *testing.T) {
// Invoke the watchAPI callback with a bad service update and wait for the // Invoke the watchAPI callback with a bad service update and wait for the
// ReportError method to be called on the ClientConn. // ReportError method to be called on the ClientConn.
suErr := errors.New("bad serviceupdate") suErr := errors.New("bad serviceupdate")
xdsC.InvokeWatchServiceCb("", suErr) xdsC.InvokeWatchServiceCallback("", suErr)
if gotErrVal, gotErr := tcc.errorCh.Receive(); gotErr != nil || gotErrVal != suErr { if gotErrVal, gotErr := tcc.errorCh.Receive(); gotErr != nil || gotErrVal != suErr {
t.Fatalf("ClientConn.ReportError() received %v, want %v", gotErrVal, suErr) t.Fatalf("ClientConn.ReportError() received %v, want %v", gotErrVal, suErr)
} }
@ -291,7 +292,7 @@ func TestXDSResolverBadServiceUpdate(t *testing.T) {
// TestXDSResolverGoodServiceUpdate tests the happy case where the resolver // TestXDSResolverGoodServiceUpdate tests the happy case where the resolver
// gets a good service update from the xdsClient. // gets a good service update from the xdsClient.
func TestXDSResolverGoodServiceUpdate(t *testing.T) { func TestXDSResolverGoodServiceUpdate(t *testing.T) {
xdsC := testutils.NewXDSClient() xdsC := fakexds.NewClient()
xdsR, tcc, cancel := testSetup(t, setupOpts{ xdsR, tcc, cancel := testSetup(t, setupOpts{
config: &validConfig, config: &validConfig,
xdsClientFunc: func(_ xdsclient.Options) (xdsClientInterface, error) { return xdsC, nil }, xdsClientFunc: func(_ xdsclient.Options) (xdsClientInterface, error) { return xdsC, nil },
@ -305,7 +306,7 @@ func TestXDSResolverGoodServiceUpdate(t *testing.T) {
// Invoke the watchAPI callback with a good service update and wait for the // Invoke the watchAPI callback with a good service update and wait for the
// UpdateState method to be called on the ClientConn. // UpdateState method to be called on the ClientConn.
xdsC.InvokeWatchServiceCb(cluster, nil) xdsC.InvokeWatchServiceCallback(cluster, nil)
gotState, err := tcc.stateCh.Receive() gotState, err := tcc.stateCh.Receive()
if err != nil { if err != nil {
t.Fatalf("ClientConn.UpdateState returned error: %v", err) t.Fatalf("ClientConn.UpdateState returned error: %v", err)

Просмотреть файл

@ -1,67 +0,0 @@
/*
*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package testutils
import (
xdsclient "google.golang.org/grpc/xds/internal/client"
)
// XDSClient is a fake implementation of an xds client. It exposes a bunch of
// channels to signal the occurrence of various events.
type XDSClient struct {
serviceCb func(xdsclient.ServiceUpdate, error)
suWatchCh *Channel
closeCh *Channel
suCancelCh *Channel
}
// WatchService registers a LDS/RDS watch.
func (xdsC *XDSClient) WatchService(target string, callback func(xdsclient.ServiceUpdate, error)) func() {
xdsC.serviceCb = callback
xdsC.suWatchCh.Send(target)
return func() {
xdsC.suCancelCh.Send(nil)
}
}
// WaitForWatchService waits for WatchService to be invoked on this client
// within a reasonable timeout.
func (xdsC *XDSClient) WaitForWatchService() (string, error) {
val, err := xdsC.suWatchCh.Receive()
return val.(string), err
}
// InvokeWatchServiceCb invokes the registered service watch callback.
func (xdsC *XDSClient) InvokeWatchServiceCb(cluster string, err error) {
xdsC.serviceCb(xdsclient.ServiceUpdate{Cluster: cluster}, err)
}
// Close closes the xds client.
func (xdsC *XDSClient) Close() {
xdsC.closeCh.Send(nil)
}
// NewXDSClient returns a new fake xds client.
func NewXDSClient() *XDSClient {
return &XDSClient{
suWatchCh: NewChannel(),
closeCh: NewChannel(),
suCancelCh: NewChannel(),
}
}