|
| 1 | +/* |
| 2 | + * Copyright 2025 RPCPlatform Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package picker |
| 18 | + |
| 19 | +import ( |
| 20 | + "slices" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/nexcode/rpcplatform/internal/attributes" |
| 24 | + "github.com/nexcode/rpcplatform/internal/config" |
| 25 | + "github.com/nexcode/rpcplatform/internal/grpcattrs" |
| 26 | + "google.golang.org/grpc/balancer" |
| 27 | + "google.golang.org/grpc/balancer/endpointsharding" |
| 28 | + "google.golang.org/grpc/connectivity" |
| 29 | + "google.golang.org/grpc/resolver" |
| 30 | +) |
| 31 | + |
| 32 | +func TestNew(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + childStates := []endpointsharding.ChildState{{ |
| 36 | + State: balancer.State{ |
| 37 | + ConnectivityState: connectivity.Ready, |
| 38 | + Picker: &namedPicker{name: 1}, |
| 39 | + }, |
| 40 | + Endpoint: resolver.Endpoint{ |
| 41 | + Attributes: grpcattrs.SetAttributes(nil, &attributes.Attributes{ |
| 42 | + BalancerWeight: 1, |
| 43 | + BalancerPriority: 1, |
| 44 | + }), |
| 45 | + }, |
| 46 | + }, { |
| 47 | + State: balancer.State{ |
| 48 | + ConnectivityState: connectivity.Ready, |
| 49 | + Picker: &namedPicker{name: 2}, |
| 50 | + }, |
| 51 | + Endpoint: resolver.Endpoint{ |
| 52 | + Attributes: grpcattrs.SetAttributes(nil, &attributes.Attributes{ |
| 53 | + BalancerWeight: 7, |
| 54 | + BalancerPriority: 2, |
| 55 | + }), |
| 56 | + }, |
| 57 | + }, { |
| 58 | + State: balancer.State{ |
| 59 | + ConnectivityState: connectivity.Ready, |
| 60 | + Picker: &namedPicker{name: 3}, |
| 61 | + }, |
| 62 | + Endpoint: resolver.Endpoint{ |
| 63 | + Attributes: grpcattrs.SetAttributes(nil, &attributes.Attributes{ |
| 64 | + BalancerWeight: 5, |
| 65 | + BalancerPriority: 2, |
| 66 | + }), |
| 67 | + }, |
| 68 | + }, { |
| 69 | + State: balancer.State{ |
| 70 | + ConnectivityState: connectivity.Ready, |
| 71 | + Picker: &namedPicker{name: 4}, |
| 72 | + }, |
| 73 | + Endpoint: resolver.Endpoint{ |
| 74 | + Attributes: grpcattrs.SetAttributes(nil, &attributes.Attributes{ |
| 75 | + BalancerWeight: 3, |
| 76 | + BalancerPriority: 2, |
| 77 | + }), |
| 78 | + }, |
| 79 | + }, { |
| 80 | + State: balancer.State{ |
| 81 | + ConnectivityState: connectivity.Ready, |
| 82 | + Picker: &namedPicker{name: 5}, |
| 83 | + }, |
| 84 | + Endpoint: resolver.Endpoint{ |
| 85 | + Attributes: grpcattrs.SetAttributes(nil, &attributes.Attributes{ |
| 86 | + BalancerWeight: 0, |
| 87 | + BalancerPriority: 3, |
| 88 | + }), |
| 89 | + }, |
| 90 | + }, { |
| 91 | + State: balancer.State{ |
| 92 | + ConnectivityState: connectivity.TransientFailure, |
| 93 | + Picker: &namedPicker{name: 6}, |
| 94 | + }, |
| 95 | + Endpoint: resolver.Endpoint{ |
| 96 | + Attributes: grpcattrs.SetAttributes(nil, &attributes.Attributes{ |
| 97 | + BalancerWeight: 1, |
| 98 | + BalancerPriority: 3, |
| 99 | + }), |
| 100 | + }, |
| 101 | + }} |
| 102 | + |
| 103 | + config := &config.Client{ |
| 104 | + MaxActiveServers: 3, |
| 105 | + } |
| 106 | + |
| 107 | + picker := New(childStates, config).(*picker) |
| 108 | + |
| 109 | + actualSequence := make([]int, len(picker.pickers)) |
| 110 | + for i, picker := range picker.pickers { |
| 111 | + actualSequence[i] = picker.(*namedPicker).name |
| 112 | + } |
| 113 | + |
| 114 | + expectedSequence := []int{2, 3, 4, 2, 3, 2, 2, 3, 4, 2, 3, 2, 2, 3, 4} |
| 115 | + |
| 116 | + if !slices.Equal(actualSequence, expectedSequence) { |
| 117 | + t.Errorf("picker sequence = %v, want: %v", actualSequence, expectedSequence) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +type namedPicker struct { |
| 122 | + name int |
| 123 | +} |
| 124 | + |
| 125 | +func (namedPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) { |
| 126 | + return balancer.PickResult{}, nil |
| 127 | +} |
0 commit comments