google.golang.org/grpc@v1.62.1/xds/internal/testutils/fakeclient/client.go (about) 1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 // Package fakeclient provides a fake implementation of an xDS client. 20 package fakeclient 21 22 import ( 23 "context" 24 25 "google.golang.org/grpc/internal/testutils" 26 "google.golang.org/grpc/xds/internal/xdsclient" 27 "google.golang.org/grpc/xds/internal/xdsclient/bootstrap" 28 "google.golang.org/grpc/xds/internal/xdsclient/load" 29 ) 30 31 // Client is a fake implementation of an xds client. It exposes a bunch of 32 // channels to signal the occurrence of various events. 33 type Client struct { 34 // Embed XDSClient so this fake client implements the interface, but it's 35 // never set (it's always nil). This may cause nil panic since not all the 36 // methods are implemented. 37 xdsclient.XDSClient 38 39 name string 40 loadReportCh *testutils.Channel 41 lrsCancelCh *testutils.Channel 42 loadStore *load.Store 43 bootstrapCfg *bootstrap.Config 44 } 45 46 // ReportLoadArgs wraps the arguments passed to ReportLoad. 47 type ReportLoadArgs struct { 48 // Server is the name of the server to which the load is reported. 49 Server *bootstrap.ServerConfig 50 } 51 52 // ReportLoad starts reporting load about clusterName to server. 53 func (xdsC *Client) ReportLoad(server *bootstrap.ServerConfig) (loadStore *load.Store, cancel func()) { 54 xdsC.loadReportCh.Send(ReportLoadArgs{Server: server}) 55 return xdsC.loadStore, func() { 56 xdsC.lrsCancelCh.Send(nil) 57 } 58 } 59 60 // WaitForCancelReportLoad waits for a load report to be cancelled and returns 61 // context.DeadlineExceeded otherwise. 62 func (xdsC *Client) WaitForCancelReportLoad(ctx context.Context) error { 63 _, err := xdsC.lrsCancelCh.Receive(ctx) 64 return err 65 } 66 67 // LoadStore returns the underlying load data store. 68 func (xdsC *Client) LoadStore() *load.Store { 69 return xdsC.loadStore 70 } 71 72 // WaitForReportLoad waits for ReportLoad to be invoked on this client and 73 // returns the arguments passed to it. 74 func (xdsC *Client) WaitForReportLoad(ctx context.Context) (ReportLoadArgs, error) { 75 val, err := xdsC.loadReportCh.Receive(ctx) 76 if err != nil { 77 return ReportLoadArgs{}, err 78 } 79 return val.(ReportLoadArgs), nil 80 } 81 82 // BootstrapConfig returns the bootstrap config. 83 func (xdsC *Client) BootstrapConfig() *bootstrap.Config { 84 return xdsC.bootstrapCfg 85 } 86 87 // SetBootstrapConfig updates the bootstrap config. 88 func (xdsC *Client) SetBootstrapConfig(cfg *bootstrap.Config) { 89 xdsC.bootstrapCfg = cfg 90 } 91 92 // Name returns the name of the xds client. 93 func (xdsC *Client) Name() string { 94 return xdsC.name 95 } 96 97 // NewClient returns a new fake xds client. 98 func NewClient() *Client { 99 return NewClientWithName("") 100 } 101 102 // NewClientWithName returns a new fake xds client with the provided name. This 103 // is used in cases where multiple clients are created in the tests and we need 104 // to make sure the client is created for the expected balancer name. 105 func NewClientWithName(name string) *Client { 106 return &Client{ 107 name: name, 108 loadReportCh: testutils.NewChannel(), 109 lrsCancelCh: testutils.NewChannel(), 110 loadStore: load.NewStore(), 111 bootstrapCfg: &bootstrap.Config{ClientDefaultListenerResourceNameTemplate: "%s"}, 112 } 113 }