github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/resolver/serviceconfig_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 resolver
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"regexp"
    25  	"testing"
    26  
    27  	xxhash "github.com/cespare/xxhash/v2"
    28  	"github.com/google/go-cmp/cmp"
    29  	iresolver "github.com/hxx258456/ccgo/grpc/internal/resolver"
    30  	"github.com/hxx258456/ccgo/grpc/metadata"
    31  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/balancer/cdsbalancer" // To parse LB config
    32  	"github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/xdsresource"
    33  )
    34  
    35  func (s) TestPruneActiveClusters(t *testing.T) {
    36  	r := &xdsResolver{activeClusters: map[string]*clusterInfo{
    37  		"zero":        {refCount: 0},
    38  		"one":         {refCount: 1},
    39  		"two":         {refCount: 2},
    40  		"anotherzero": {refCount: 0},
    41  	}}
    42  	want := map[string]*clusterInfo{
    43  		"one": {refCount: 1},
    44  		"two": {refCount: 2},
    45  	}
    46  	r.pruneActiveClusters()
    47  	if d := cmp.Diff(r.activeClusters, want, cmp.AllowUnexported(clusterInfo{})); d != "" {
    48  		t.Fatalf("r.activeClusters = %v; want %v\nDiffs: %v", r.activeClusters, want, d)
    49  	}
    50  }
    51  
    52  func (s) TestGenerateRequestHash(t *testing.T) {
    53  	cs := &configSelector{
    54  		r: &xdsResolver{
    55  			cc: &testClientConn{},
    56  		},
    57  	}
    58  	tests := []struct {
    59  		name            string
    60  		hashPolicies    []*xdsresource.HashPolicy
    61  		requestHashWant uint64
    62  		rpcInfo         iresolver.RPCInfo
    63  	}{
    64  		// TestGenerateRequestHashHeaders tests generating request hashes for
    65  		// hash policies that specify to hash headers.
    66  		{
    67  			name: "test-generate-request-hash-headers",
    68  			hashPolicies: []*xdsresource.HashPolicy{{
    69  				HashPolicyType:    xdsresource.HashPolicyTypeHeader,
    70  				HeaderName:        ":path",
    71  				Regex:             func() *regexp.Regexp { return regexp.MustCompile("/products") }(), // Will replace /products with /new-products, to test find and replace functionality.
    72  				RegexSubstitution: "/new-products",
    73  			}},
    74  			requestHashWant: xxhash.Sum64String("/new-products"),
    75  			rpcInfo: iresolver.RPCInfo{
    76  				Context: metadata.NewOutgoingContext(context.Background(), metadata.Pairs(":path", "/products")),
    77  				Method:  "/some-method",
    78  			},
    79  		},
    80  		// TestGenerateHashChannelID tests generating request hashes for hash
    81  		// policies that specify to hash something that uniquely identifies the
    82  		// ClientConn (the pointer).
    83  		{
    84  			name: "test-generate-request-hash-channel-id",
    85  			hashPolicies: []*xdsresource.HashPolicy{{
    86  				HashPolicyType: xdsresource.HashPolicyTypeChannelID,
    87  			}},
    88  			requestHashWant: xxhash.Sum64String(fmt.Sprintf("%p", &cs.r.cc)),
    89  			rpcInfo:         iresolver.RPCInfo{},
    90  		},
    91  		// TestGenerateRequestHashEmptyString tests generating request hashes
    92  		// for hash policies that specify to hash headers and replace empty
    93  		// strings in the headers.
    94  		{
    95  			name: "test-generate-request-hash-empty-string",
    96  			hashPolicies: []*xdsresource.HashPolicy{{
    97  				HashPolicyType:    xdsresource.HashPolicyTypeHeader,
    98  				HeaderName:        ":path",
    99  				Regex:             func() *regexp.Regexp { return regexp.MustCompile("") }(),
   100  				RegexSubstitution: "e",
   101  			}},
   102  			requestHashWant: xxhash.Sum64String("eaebece"),
   103  			rpcInfo: iresolver.RPCInfo{
   104  				Context: metadata.NewOutgoingContext(context.Background(), metadata.Pairs(":path", "abc")),
   105  				Method:  "/some-method",
   106  			},
   107  		},
   108  	}
   109  	for _, test := range tests {
   110  		t.Run(test.name, func(t *testing.T) {
   111  			requestHashGot := cs.generateHash(test.rpcInfo, test.hashPolicies)
   112  			if requestHashGot != test.requestHashWant {
   113  				t.Fatalf("requestHashGot = %v, requestHashWant = %v", requestHashGot, test.requestHashWant)
   114  			}
   115  		})
   116  	}
   117  }