gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/xdsclient/watchers_endpoints_test.go (about) 1 /* 2 * 3 * Copyright 2020 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 xdsclient 20 21 import ( 22 "context" 23 "fmt" 24 "testing" 25 26 "gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource" 27 "github.com/google/go-cmp/cmp" 28 29 "gitee.com/ks-custle/core-gm/grpc/xds/internal" 30 ) 31 32 var ( 33 testLocalities = []xdsresource.Locality{ 34 { 35 Endpoints: []xdsresource.Endpoint{{Address: "addr1:314"}}, 36 ID: internal.LocalityID{SubZone: "locality-1"}, 37 Priority: 1, 38 Weight: 1, 39 }, 40 { 41 Endpoints: []xdsresource.Endpoint{{Address: "addr2:159"}}, 42 ID: internal.LocalityID{SubZone: "locality-2"}, 43 Priority: 0, 44 Weight: 1, 45 }, 46 } 47 ) 48 49 // TestEndpointsWatch covers the cases: 50 // - an update is received after a watch() 51 // - an update for another resource name (which doesn't trigger callback) 52 // - an update is received after cancel() 53 func (s) TestEndpointsWatch(t *testing.T) { 54 testWatch(t, xdsresource.EndpointsResource, xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[0]}}, testCDSName) 55 } 56 57 // TestEndpointsTwoWatchSameResourceName covers the case where an update is received 58 // after two watch() for the same resource name. 59 func (s) TestEndpointsTwoWatchSameResourceName(t *testing.T) { 60 testTwoWatchSameResourceName(t, xdsresource.EndpointsResource, xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[0]}}, testCDSName) 61 } 62 63 // TestEndpointsThreeWatchDifferentResourceName covers the case where an update is 64 // received after three watch() for different resource names. 65 func (s) TestEndpointsThreeWatchDifferentResourceName(t *testing.T) { 66 testThreeWatchDifferentResourceName(t, xdsresource.EndpointsResource, 67 xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[0]}}, testCDSName+"1", 68 xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[1]}}, testCDSName+"2", 69 ) 70 } 71 72 // TestEndpointsWatchAfterCache covers the case where watch is called after the update 73 // is in cache. 74 func (s) TestEndpointsWatchAfterCache(t *testing.T) { 75 testWatchAfterCache(t, xdsresource.EndpointsResource, xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[0]}}, testCDSName) 76 } 77 78 // TestEndpointsWatchExpiryTimer tests the case where the client does not receive 79 // an CDS response for the request that it sends out. We want the watch callback 80 // to be invoked with an error once the watchExpiryTimer fires. 81 func (s) TestEndpointsWatchExpiryTimer(t *testing.T) { 82 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 83 defer cancel() 84 client, _ := testClientSetup(t, true) 85 endpointsUpdateCh, _ := newWatch(t, client, xdsresource.EndpointsResource, testCDSName) 86 87 u, err := endpointsUpdateCh.Receive(ctx) 88 if err != nil { 89 t.Fatalf("timeout when waiting for endpoints update: %v", err) 90 } 91 gotUpdate := u.(xdsresource.EndpointsUpdateErrTuple) 92 if gotUpdate.Err == nil || !cmp.Equal(gotUpdate.Update, xdsresource.EndpointsUpdate{}) { 93 t.Fatalf("unexpected endpointsUpdate: (%v, %v), want: (EndpointsUpdate{}, nil)", gotUpdate.Update, gotUpdate.Err) 94 } 95 } 96 97 // TestEndpointsWatchNACKError covers the case that an update is NACK'ed, and 98 // the watcher should also receive the error. 99 func (s) TestEndpointsWatchNACKError(t *testing.T) { 100 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 101 defer cancel() 102 client, ctrlCh := testClientSetup(t, false) 103 endpointsUpdateCh, _ := newWatch(t, client, xdsresource.EndpointsResource, testCDSName) 104 _, updateHandler := getControllerAndPubsub(ctx, t, client, ctrlCh, xdsresource.EndpointsResource, testCDSName) 105 106 wantError := fmt.Errorf("testing error") 107 updateHandler.NewEndpoints(map[string]xdsresource.EndpointsUpdateErrTuple{testCDSName: {Err: wantError}}, xdsresource.UpdateMetadata{ErrState: &xdsresource.UpdateErrorMetadata{Err: wantError}}) 108 if err := verifyEndpointsUpdate(ctx, endpointsUpdateCh, xdsresource.EndpointsUpdate{}, wantError); err != nil { 109 t.Fatal(err) 110 } 111 } 112 113 // TestEndpointsWatchPartialValid covers the case that a response contains both 114 // valid and invalid resources. This response will be NACK'ed by the xdsclient. 115 // But the watchers with valid resources should receive the update, those with 116 // invalida resources should receive an error. 117 func (s) TestEndpointsWatchPartialValid(t *testing.T) { 118 testWatchPartialValid(t, xdsresource.EndpointsResource, xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[0]}}, testCDSName) 119 }