...

Source file src/google.golang.org/grpc/xds/internal/balancer/clusterresolver/configbuilder_childname_test.go

Documentation: google.golang.org/grpc/xds/internal/balancer/clusterresolver

     1  /*
     2   *
     3   * Copyright 2022 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  package clusterresolver
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"google.golang.org/grpc/xds/internal"
    25  	"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
    26  )
    27  
    28  func Test_nameGenerator_generate(t *testing.T) {
    29  	tests := []struct {
    30  		name   string
    31  		prefix uint64
    32  		input1 [][]xdsresource.Locality
    33  		input2 [][]xdsresource.Locality
    34  		want   []string
    35  	}{
    36  		{
    37  			name:   "init, two new priorities",
    38  			prefix: 3,
    39  			input1: nil,
    40  			input2: [][]xdsresource.Locality{
    41  				{{ID: internal.LocalityID{Zone: "L0"}}},
    42  				{{ID: internal.LocalityID{Zone: "L1"}}},
    43  			},
    44  			want: []string{"priority-3-0", "priority-3-1"},
    45  		},
    46  		{
    47  			name:   "one new priority",
    48  			prefix: 1,
    49  			input1: [][]xdsresource.Locality{
    50  				{{ID: internal.LocalityID{Zone: "L0"}}},
    51  			},
    52  			input2: [][]xdsresource.Locality{
    53  				{{ID: internal.LocalityID{Zone: "L0"}}},
    54  				{{ID: internal.LocalityID{Zone: "L1"}}},
    55  			},
    56  			want: []string{"priority-1-0", "priority-1-1"},
    57  		},
    58  		{
    59  			name:   "merge two priorities",
    60  			prefix: 4,
    61  			input1: [][]xdsresource.Locality{
    62  				{{ID: internal.LocalityID{Zone: "L0"}}},
    63  				{{ID: internal.LocalityID{Zone: "L1"}}},
    64  				{{ID: internal.LocalityID{Zone: "L2"}}},
    65  			},
    66  			input2: [][]xdsresource.Locality{
    67  				{{ID: internal.LocalityID{Zone: "L0"}}, {ID: internal.LocalityID{Zone: "L1"}}},
    68  				{{ID: internal.LocalityID{Zone: "L2"}}},
    69  			},
    70  			want: []string{"priority-4-0", "priority-4-2"},
    71  		},
    72  		{
    73  			name: "swap two priorities",
    74  			input1: [][]xdsresource.Locality{
    75  				{{ID: internal.LocalityID{Zone: "L0"}}},
    76  				{{ID: internal.LocalityID{Zone: "L1"}}},
    77  				{{ID: internal.LocalityID{Zone: "L2"}}},
    78  			},
    79  			input2: [][]xdsresource.Locality{
    80  				{{ID: internal.LocalityID{Zone: "L1"}}},
    81  				{{ID: internal.LocalityID{Zone: "L0"}}},
    82  				{{ID: internal.LocalityID{Zone: "L2"}}},
    83  			},
    84  			want: []string{"priority-0-1", "priority-0-0", "priority-0-2"},
    85  		},
    86  		{
    87  			name: "split priority",
    88  			input1: [][]xdsresource.Locality{
    89  				{{ID: internal.LocalityID{Zone: "L0"}}, {ID: internal.LocalityID{Zone: "L1"}}},
    90  				{{ID: internal.LocalityID{Zone: "L2"}}},
    91  			},
    92  			input2: [][]xdsresource.Locality{
    93  				{{ID: internal.LocalityID{Zone: "L0"}}},
    94  				{{ID: internal.LocalityID{Zone: "L1"}}}, // This gets a newly generated name, sice "0-0" was already picked.
    95  				{{ID: internal.LocalityID{Zone: "L2"}}},
    96  			},
    97  			want: []string{"priority-0-0", "priority-0-2", "priority-0-1"},
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			ng := newNameGenerator(tt.prefix)
   103  			got1 := ng.generate(tt.input1)
   104  			t.Logf("%v", got1)
   105  			got := ng.generate(tt.input2)
   106  			if diff := cmp.Diff(got, tt.want); diff != "" {
   107  				t.Errorf("generate() = got: %v, want: %v, diff (-got +want): %s", got, tt.want, diff)
   108  			}
   109  		})
   110  	}
   111  }
   112  

View as plain text